Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ProgressSemaphore.cpp
1 /*
2  * This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4  Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10  BIAS is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation; either version 2.1 of the License, or
13  (at your option) any later version.
14 
15  BIAS is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with BIAS; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 #include "ProgressSemaphore.hh"
26 
27 #include <Base/Debug/Error.hh>
28 
29 using namespace BIAS;
30 
32 {
33 #ifdef BIAS_HAVE_PTHREADS
34  pthread_mutex_init(&Lock_, NULL);
35 #endif
36  Reset();
37 }
38 
39 
41 {}
42 
43 
45 {
46  float ratio;
47 #ifdef BIAS_HAVE_PTHREADS
48  pthread_mutex_lock(&Lock_);
49 #endif
50  if (NumSteps_>0)
51  ratio = float(CurrentStep_)/float(NumSteps_);
52  else
53  ratio=0.0f;
54 #ifdef BIAS_HAVE_PTHREADS
55  pthread_mutex_unlock(&Lock_);
56 #endif
57  BIASASSERT(ratio>=0. && ratio<=1.);
58  return ratio;
59 }
60 
61 
62 void ProgressSemaphore::SetNumSteps(unsigned int steps)
63 {
64 #ifdef BIAS_HAVE_PTHREADS
65  pthread_mutex_lock(&Lock_);
66 #endif
67  NumSteps_ = steps;
68 #ifdef BIAS_HAVE_PTHREADS
69  pthread_mutex_unlock(&Lock_);
70 #endif
71 }
72 
73 
74 void ProgressSemaphore::SetCurrentStep(unsigned int currentstep)
75 {
76  BIASASSERT(currentstep<=NumSteps_);
77 #ifdef BIAS_HAVE_PTHREADS
78  pthread_mutex_lock(&Lock_);
79 #endif
80  CurrentStep_ = currentstep;
81 #ifdef BIAS_HAVE_PTHREADS
82  pthread_mutex_unlock(&Lock_);
83 #endif
84 }
85 
87 {
88 #ifdef BIAS_HAVE_PTHREADS
89  pthread_mutex_lock(&Lock_);
90 #endif
91  CurrentStep_++;
92 #ifdef BIAS_HAVE_PTHREADS
93  pthread_mutex_unlock(&Lock_);
94 #endif
95  BIASASSERT(CurrentStep_<=NumSteps_);
96 }
97 
98 
100 {
101 #ifdef BIAS_HAVE_PTHREADS
102  pthread_mutex_lock(&Lock_);
103 #endif
104  Terminate_ = true;
105 #ifdef BIAS_HAVE_PTHREADS
106  pthread_mutex_unlock(&Lock_);
107 #endif
108 }
109 
111 {
112 #ifdef BIAS_HAVE_PTHREADS
113  pthread_mutex_lock(&Lock_);
114 #endif
115  Terminate_ =false;
116 #ifdef BIAS_HAVE_PTHREADS
117  pthread_mutex_unlock(&Lock_);
118 #endif
119 }
120 
122 {
123  bool flag;
124 #ifdef BIAS_HAVE_PTHREADS
125  pthread_mutex_lock(&Lock_);
126 #endif
127  flag = Terminate_;
128 #ifdef BIAS_HAVE_PTHREADS
129  pthread_mutex_unlock(&Lock_);
130 #endif
131  return flag;
132 }
133 
135 {
136 #ifdef BIAS_HAVE_PTHREADS
137  pthread_mutex_lock(&Lock_);
138 #endif
139  NumSteps_ = 100;
140  CurrentStep_ = 0;
141  Terminate_ = false;
142 #ifdef BIAS_HAVE_PTHREADS
143  pthread_mutex_unlock(&Lock_);
144 #endif
145 }
void IncrementStep()
Increment the current step.
float GetProgress()
returns current progress state in standard intervall
void SetNumSteps(unsigned int steps)
Set the number of steps to progress will need to complete.
void Reset()
Resets all values.
void SetCurrentStep(unsigned int CurrentStep)
Set the current step.
void SetTerminateFlag()
Sets a termination flag to inform e.g.
bool IsTerminationRequested()
Tells whether the termination flag is set.
void ResetTerminateFlag()
Chancels the termination flag.