Fixed initialisation of tf in file_open(). Without setting the memory to 0,
[cinelerra_cv/mob.git] / guicast / condition.C
blobd3c8b3f04bce3da15a22b76755d0c1a0038b09fd
1 #ifndef NO_GUICAST
2 #include "bcsignals.h"
3 #endif
4 #include "condition.h"
6 #include <errno.h>
7 #include <stdio.h>
8 #include <sys/time.h>
10 Condition::Condition(int init_value, char *title, int is_binary)
12         this->is_binary = is_binary;
13         this->title = title;
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);
23 #ifndef NO_GUICAST
24         UNSET_ALL_LOCKS(this);
25 #endif
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);
34         value = init_value;
37 void Condition::lock(char *location)
39 #ifndef NO_GUICAST
40         SET_LOCK(this, title, location);
41 #endif
42     pthread_mutex_lock(&mutex);
43     while(value <= 0) pthread_cond_wait(&cond, &mutex);
44 #ifndef NO_GUICAST
45         UNSET_LOCK2
46 #endif
47         if(is_binary)
48                 value = 0;
49         else
50                 value--;
51     pthread_mutex_unlock(&mutex);
54 void Condition::unlock()
56 // The lock trace is created and removed by the acquirer
57 //#ifndef NO_GUICAST
58 //      UNSET_LOCK(this);
59 //#endif
60     pthread_mutex_lock(&mutex);
61     if(is_binary)
62                 value = 1;
63         else
64                 value++;
65     pthread_cond_signal(&cond);
66     pthread_mutex_unlock(&mutex);
69 int Condition::timed_lock(int microseconds, char *location)
71     struct timeval now;
72     struct timespec timeout;
73     int result = 0;
75 #ifndef NO_GUICAST
76         SET_LOCK(this, title, location);
77 #endif
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)
84         {
85                 result = pthread_cond_timedwait(&cond, &mutex, &timeout);
86     }
88     if(result == ETIMEDOUT) 
89         {
90 //printf("Condition::timed_lock 1 %s %s\n", title, location);
91 #ifndef NO_GUICAST
92                 UNSET_LOCK2
93 #endif
94                 result = 1;
95     } 
96         else 
97         {
98 //printf("Condition::timed_lock 2 %s %s\n", title, location);
99 #ifndef NO_GUICAST
100                 UNSET_LOCK2
101 #endif
102                 if(is_binary)
103                         value = 0;
104                 else
105                         value--;
106                 result = 0;
107     }
108     pthread_mutex_unlock(&mutex);
109         return result;
113 int Condition::get_value()
115         return value;