4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "bcsignals.h"
23 #include "condition.h"
31 Condition::Condition(int init_value, const char *title, int is_binary)
33 this->is_binary = is_binary;
36 pthread_mutex_init(&mutex, 0);
37 pthread_cond_init(&cond, NULL);
38 this->value = this->init_value = init_value;
41 Condition:: ~Condition()
43 pthread_cond_destroy(&cond);
44 pthread_mutex_destroy(&mutex);
45 UNSET_ALL_LOCKS(this);
48 void Condition::reset()
50 pthread_cond_destroy(&cond);
51 pthread_mutex_destroy(&mutex);
52 pthread_mutex_init(&mutex, 0);
53 pthread_cond_init(&cond, NULL);
54 UNSET_ALL_LOCKS(this);
60 void Condition::lock(const char *location)
62 SET_LOCK(this, title, location);
63 pthread_mutex_lock(&mutex);
64 while(value <= 0) pthread_cond_wait(&cond, &mutex);
72 pthread_mutex_unlock(&mutex);
75 void Condition::unlock()
77 // The lock trace is created and removed by the acquirer
79 pthread_mutex_lock(&mutex);
85 pthread_cond_signal(&cond);
86 pthread_mutex_unlock(&mutex);
89 int Condition::timed_lock(int microseconds, const char *location)
93 SET_LOCK(this, title, location);
94 pthread_mutex_lock(&mutex);
97 gettimeofday(&now, 0);
99 int64_t nsec = now.tv_usec * 1000 + (microseconds % 1000000) * 1000;
100 int64_t sec = nsec / 1000000000; nsec %= 1000000000;
101 sec += now.tv_sec + microseconds / 1000000;
102 struct timespec timeout; timeout.tv_sec = sec; timeout.tv_nsec = nsec;
103 while( value <= 0 && result != ETIMEDOUT ) {
104 result = pthread_cond_timedwait(&cond, &mutex, &timeout);
108 result = result == ETIMEDOUT ? 1 : -1;
111 struct timeval timeout;
112 int64_t timeout_msec = ((int64_t)microseconds / 1000);
113 // This is based on the most common frame rate since it's mainly used in
115 while( value <= 0 && !result ) {
116 pthread_mutex_unlock(&mutex);
118 gettimeofday(&timeout, 0);
119 timeout.tv_usec -= now.tv_usec;
120 timeout.tv_sec -= now.tv_sec;
121 pthread_mutex_lock(&mutex);
122 if( value > 0 ) break;
123 if( (int64_t)timeout.tv_sec * 1000 +
124 (int64_t)timeout.tv_usec / 1000 > timeout_msec )
130 //printf("Condition::timed_lock 2 %d %s %s\n", result, title, location);
139 pthread_mutex_unlock(&mutex);
144 int Condition::get_value()