10 Condition::Condition(int init_value, char *title, int is_binary)
12 this->is_binary = is_binary;
14 pthread_mutex_init(&mutex, 0);
15 pthread_cond_init(&cond, NULL);
16 this->value = this->init_value = init_value;
19 Condition:: ~Condition()
21 pthread_cond_destroy(&cond);
22 pthread_mutex_destroy(&mutex);
24 UNSET_ALL_LOCKS(this);
28 void Condition::reset()
30 pthread_cond_destroy(&cond);
31 pthread_mutex_destroy(&mutex);
32 pthread_mutex_init(&mutex, 0);
33 pthread_cond_init(&cond, NULL);
37 void Condition::lock(char *location)
40 SET_LOCK(this, title, location);
42 pthread_mutex_lock(&mutex);
43 while(value <= 0) pthread_cond_wait(&cond, &mutex);
51 pthread_mutex_unlock(&mutex);
54 void Condition::unlock()
56 // The lock trace is created and removed by the acquirer
60 pthread_mutex_lock(&mutex);
65 pthread_cond_signal(&cond);
66 pthread_mutex_unlock(&mutex);
69 int Condition::timed_lock(int microseconds, char *location)
72 struct timespec timeout;
76 SET_LOCK(this, title, location);
78 pthread_mutex_lock(&mutex);
79 gettimeofday(&now, 0);
80 timeout.tv_sec = now.tv_sec + microseconds / 1000000;
81 timeout.tv_nsec = now.tv_usec * 1000 + (microseconds % 1000000) * 1000;
83 while(value <= 0 && result != ETIMEDOUT)
85 result = pthread_cond_timedwait(&cond, &mutex, &timeout);
88 if(result == ETIMEDOUT)
90 //printf("Condition::timed_lock 1 %s %s\n", title, location);
98 //printf("Condition::timed_lock 2 %s %s\n", title, location);
108 pthread_mutex_unlock(&mutex);
113 int Condition::get_value()
120 // c-file-style: "linux"