Merge branch 'ct' of git.pipapo.org:cinelerra-ct into ct
[cinelerra_cv/ct.git] / guicast / mutex.C
blob88f95b7714b182f6ab00fb84f6b3708527d06daa
1 #ifndef NO_GUICAST
2 #include "bcsignals.h"
3 #endif
4 #include "mutex.h"
6 Mutex::Mutex(char *title, int recursive)
8         this->title = title;
9         pthread_mutexattr_t attr;
10         pthread_mutexattr_init(&attr);
11         pthread_mutex_init(&mutex, &attr);
12         pthread_mutex_init(&recursive_lock, &attr);
13         count = 0;
14         this->recursive = recursive;
15         thread_id = 0;
16         thread_id_valid = 0;
19 Mutex::~Mutex()
21         pthread_mutex_destroy(&mutex);
22         pthread_mutex_destroy(&recursive_lock);
23 #ifndef NO_GUICAST
24         UNSET_ALL_LOCKS(this);
25 #endif
27         
28 int Mutex::lock(char *location)
30 // Test recursive owner and give up if we already own it
31         if(recursive)
32         {
33                 pthread_mutex_lock(&recursive_lock);
34                 if(thread_id_valid && pthread_self() == thread_id) 
35                 {
36                         count++;
37                         pthread_mutex_unlock(&recursive_lock);
38                         return 0;
39                 }
40                 pthread_mutex_unlock(&recursive_lock);
41         }
44 #ifndef NO_GUICAST
45         SET_LOCK(this, title, location);
46 #endif
47         if(pthread_mutex_lock(&mutex)) perror("Mutex::lock");
51 // Update recursive status for the first lock
52         if(recursive)
53         {
54                 pthread_mutex_lock(&recursive_lock);
55                 count = 1;
56                 thread_id = pthread_self();
57                 thread_id_valid = 1;
58                 pthread_mutex_unlock(&recursive_lock);
59         }
60         else
61         {
62                 count = 1;
63         }
66 #ifndef NO_GUICAST
67         SET_LOCK2
68 #endif
69         return 0;
72 int Mutex::unlock()
74 // Remove from recursive status
75         if(recursive)
76         {
77                 pthread_mutex_lock(&recursive_lock);
78                 count--;
79 // Still locked
80                 if(count > 0) 
81                 {
82                         pthread_mutex_unlock(&recursive_lock);
83                         return 0;
84                 }
85 // Not owned anymore
86                 thread_id = 0;
87                 thread_id_valid = 0;
88                 pthread_mutex_unlock(&recursive_lock);
89         }
90         else
91                 count = 0;
94 #ifndef NO_GUICAST
95         UNSET_LOCK(this);
96 #endif
98         if(pthread_mutex_unlock(&mutex)) perror("Mutex::unlock");
99         return 0;
102 int Mutex::trylock()
104         return pthread_mutex_trylock(&mutex);
107 int Mutex::is_locked()
109         return count;
112 int Mutex::reset()
114         pthread_mutex_destroy(&mutex);
115         pthread_mutexattr_t attr;
116         pthread_mutexattr_init(&attr);
117         pthread_mutex_init(&mutex, &attr);
118         count = 0;
119         thread_id = 0;
120         thread_id_valid = 0;
121 #ifndef NO_GUICAST
122         UNSET_ALL_LOCKS(this)
123 #endif
124         return 0;
127 //      Local Variables:
128 //      mode: C++
129 //      c-file-style: "linux"
130 //      End: