6bbf279e78396f033460a1a8cfd20a3f150e3561
[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         if( count <= 0 ) {
100                 printf("Mutex::unlock not locked: %s\n", title);
101                 return 0;
102         }
103 // Remove from recursive status
104         if(recursive)
105         {
106                 pthread_mutex_lock(&recursive_lock);
107                 count--;
108 // Still locked
109                 if(count > 0)
110                 {
111                         pthread_mutex_unlock(&recursive_lock);
112                         return 0;
113                 }
114 // Not owned anymore
115                 thread_id = 0;
116                 thread_id_valid = 0;
117                 pthread_mutex_unlock(&recursive_lock);
118         }
119         else
120                 count = 0;
121
122
123 #ifndef NO_GUICAST
124         UNSET_LOCK(this);
125 #endif
126
127         if(pthread_mutex_unlock(&mutex)) perror("Mutex::unlock");
128         return 0;
129 }
130
131 int Mutex::trylock(const char *location)
132 {
133         if( count ) return EBUSY;
134         int ret = pthread_mutex_trylock(&mutex);
135         if( ret ) return ret;
136
137 // Update recursive status for the first lock
138         if(recursive) {
139                 pthread_mutex_lock(&recursive_lock);
140                 count = 1;
141                 thread_id = pthread_self();
142                 thread_id_valid = 1;
143                 pthread_mutex_unlock(&recursive_lock);
144         }
145         else
146                 count = 1;
147
148 #ifndef NO_GUICAST
149         SET_LOCK(this, title, location);
150         SET_LOCK2
151 #endif
152         return 0;
153 }
154
155 int Mutex::is_locked()
156 {
157         return count;
158 }
159
160 int Mutex::reset()
161 {
162         pthread_mutex_destroy(&mutex);
163         pthread_mutexattr_t attr;
164         pthread_mutexattr_init(&attr);
165         pthread_mutex_init(&mutex, &attr);
166         count = 0;
167         thread_id = 0;
168         thread_id_valid = 0;
169 #ifndef NO_GUICAST
170         UNSET_ALL_LOCKS(this)
171 #endif
172         return 0;
173 }