Merge CV, ver=5.1; ops/methods from HV, and interface from CV where possible
[goodguy/history.git] / cinelerra-5.1 / guicast / mutex.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #include <stdio.h>
23 #include <errno.h>
24
25 #ifndef NO_GUICAST
26 #include "bcsignals.h"
27 #endif
28 #include "mutex.h"
29
30
31 Mutex::Mutex(const char *title, int recursive)
32 {
33         this->title = title;
34         pthread_mutexattr_t attr;
35         pthread_mutexattr_init(&attr);
36         pthread_mutex_init(&mutex, &attr);
37         pthread_mutex_init(&recursive_lock, &attr);
38         count = 0;
39         this->recursive = recursive;
40         thread_id = 0;
41         thread_id_valid = 0;
42 }
43
44 Mutex::~Mutex()
45 {
46         pthread_mutex_destroy(&mutex);
47         pthread_mutex_destroy(&recursive_lock);
48 #ifndef NO_GUICAST
49         UNSET_ALL_LOCKS(this);
50 #endif
51 }
52
53 int Mutex::lock(const char *location)
54 {
55 // Test recursive owner and give up if we already own it
56         if(recursive)
57         {
58                 pthread_mutex_lock(&recursive_lock);
59                 if(thread_id_valid && pthread_self() == thread_id)
60                 {
61                         count++;
62                         pthread_mutex_unlock(&recursive_lock);
63                         return 0;
64                 }
65                 pthread_mutex_unlock(&recursive_lock);
66         }
67
68
69 #ifndef NO_GUICAST
70         SET_LOCK(this, title, location);
71 #endif
72         if(pthread_mutex_lock(&mutex)) perror("Mutex::lock");
73
74
75
76 // Update recursive status for the first lock
77         if(recursive)
78         {
79                 pthread_mutex_lock(&recursive_lock);
80                 count = 1;
81                 thread_id = pthread_self();
82                 thread_id_valid = 1;
83                 pthread_mutex_unlock(&recursive_lock);
84         }
85         else
86         {
87                 count = 1;
88         }
89
90
91 #ifndef NO_GUICAST
92         SET_LOCK2
93 #endif
94         return 0;
95 }
96
97 int Mutex::unlock()
98 {
99 // Remove from recursive status
100         if(recursive)
101         {
102                 pthread_mutex_lock(&recursive_lock);
103                 count--;
104 // Still locked
105                 if(count > 0)
106                 {
107                         pthread_mutex_unlock(&recursive_lock);
108                         return 0;
109                 }
110 // Not owned anymore
111                 thread_id = 0;
112                 thread_id_valid = 0;
113                 pthread_mutex_unlock(&recursive_lock);
114         }
115         else
116                 count = 0;
117
118
119 #ifndef NO_GUICAST
120         UNSET_LOCK(this);
121 #endif
122
123         if(pthread_mutex_unlock(&mutex)) perror("Mutex::unlock");
124         return 0;
125 }
126
127 int Mutex::trylock(const char *location)
128 {
129         if( count ) return EBUSY;
130         int ret = pthread_mutex_trylock(&mutex);
131         if( ret ) return ret;
132
133 // Update recursive status for the first lock
134         if(recursive) {
135                 pthread_mutex_lock(&recursive_lock);
136                 count = 1;
137                 thread_id = pthread_self();
138                 thread_id_valid = 1;
139                 pthread_mutex_unlock(&recursive_lock);
140         }
141         else
142                 count = 1;
143
144 #ifndef NO_GUICAST
145         SET_LOCK(this, title, location);
146         SET_LOCK2
147 #endif
148         return 0;
149 }
150
151 int Mutex::is_locked()
152 {
153         return count;
154 }
155
156 int Mutex::reset()
157 {
158         pthread_mutex_destroy(&mutex);
159         pthread_mutexattr_t attr;
160         pthread_mutexattr_init(&attr);
161         pthread_mutex_init(&mutex, &attr);
162         count = 0;
163         thread_id = 0;
164         thread_id_valid = 0;
165 #ifndef NO_GUICAST
166         UNSET_ALL_LOCKS(this)
167 #endif
168         return 0;
169 }