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);
59 void Condition::lock(const char *location)
61 SET_LOCK(this, title, location);
62 pthread_mutex_lock(&mutex);
63 while(value <= 0) pthread_cond_wait(&cond, &mutex);
69 pthread_mutex_unlock(&mutex);
72 void Condition::unlock()
74 // The lock trace is created and removed by the acquirer
76 pthread_mutex_lock(&mutex);
81 pthread_cond_signal(&cond);
82 pthread_mutex_unlock(&mutex);
85 int Condition::timed_lock(int microseconds, const char *location)
89 SET_LOCK(this, title, location);
90 pthread_mutex_lock(&mutex);
93 gettimeofday(&now, 0);
95 struct timespec timeout;
96 timeout.tv_sec = now.tv_sec + microseconds / 1000000;
97 timeout.tv_nsec = now.tv_usec * 1000 + (microseconds % 1000000) * 1000;
98 while(value <= 0 && result != ETIMEDOUT)
100 result = pthread_cond_timedwait(&cond, &mutex, &timeout);
104 result = result == ETIMEDOUT ? 1 : -1;
107 struct timeval timeout;
108 int64_t timeout_msec = ((int64_t)microseconds / 1000);
109 // This is based on the most common frame rate since it's mainly used in
111 while( value <= 0 && !result ) {
112 pthread_mutex_unlock(&mutex);
114 gettimeofday(&timeout, 0);
115 timeout.tv_usec -= now.tv_usec;
116 timeout.tv_sec -= now.tv_sec;
117 pthread_mutex_lock(&mutex);
118 if( value > 0 ) break;
119 if( (int64_t)timeout.tv_sec * 1000 +
120 (int64_t)timeout.tv_usec / 1000 > timeout_msec )
126 //printf("Condition::timed_lock 2 %d %s %s\n", result, title, location);
133 pthread_mutex_unlock(&mutex);
138 int Condition::get_value()