7 Condition::Condition(int init_value, char *title)
10 pthread_mutex_init(&mutex, 0);
11 pthread_cond_init(&cond, NULL);
12 this->value = this->init_value = init_value;
15 Condition:: ~Condition()
17 pthread_cond_destroy(&cond);
18 pthread_mutex_destroy(&mutex);
19 UNSET_ALL_LOCKS(this);
22 void Condition::reset()
24 pthread_cond_destroy(&cond);
25 pthread_mutex_destroy(&mutex);
26 pthread_mutex_init(&mutex, 0);
27 pthread_cond_init(&cond, NULL);
31 void Condition::lock(char *location)
33 SET_LOCK(this, title, location);
34 pthread_mutex_lock(&mutex);
35 while(value <= 0) pthread_cond_wait(&cond, &mutex);
37 pthread_mutex_unlock(&mutex);
40 void Condition::unlock()
43 pthread_mutex_lock(&mutex);
45 pthread_cond_signal(&cond);
46 pthread_mutex_unlock(&mutex);
49 int Condition::timed_lock(int microseconds, char *location)
52 struct timespec timeout;
55 SET_LOCK(this, title, location);
56 pthread_mutex_lock(&mutex);
57 gettimeofday(&now, 0);
58 timeout.tv_sec = now.tv_sec + microseconds / 1000000;
59 timeout.tv_nsec = now.tv_usec * 1000 + (microseconds % 1000000) * 1000;
61 while(value <= 0 && result != ETIMEDOUT)
63 result = pthread_cond_timedwait(&cond, &mutex, &timeout);
66 if(result == ETIMEDOUT)
75 pthread_mutex_unlock(&mutex);
80 int Condition::get_value()