fix editpanel wdw lock issues, use undo bracketing to avoid stop playback deadlocks
[goodguy/cinelerra.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 <string.h>
24 #include <errno.h>
25
26 #include "bcsignals.h"
27 #include "mutex.h"
28
29
30 Mutex::Mutex(const char *title, int recursive)
31 {
32         this->title = title;
33         pthread_mutexattr_t attr;
34         pthread_mutexattr_init(&attr);
35         pthread_mutex_init(&mutex, &attr);
36         pthread_mutex_init(&recursive_lock, &attr);
37         count = 0;
38         this->recursive = recursive;
39         thread_id = 0;
40         thread_id_valid = 0;
41 }
42
43 Mutex::~Mutex()
44 {
45         pthread_mutex_destroy(&mutex);
46         pthread_mutex_destroy(&recursive_lock);
47         UNSET_ALL_LOCKS(this);
48 }
49
50 int Mutex::lock(const char *location)
51 {
52 // Test recursive owner and give up if we already own it
53         if(recursive)
54         {
55                 pthread_mutex_lock(&recursive_lock);
56                 if(thread_id_valid && pthread_self() == thread_id)
57                 {
58                         count++;
59                         pthread_mutex_unlock(&recursive_lock);
60                         return 0;
61                 }
62                 pthread_mutex_unlock(&recursive_lock);
63         }
64
65
66         SET_LOCK(this, title, location);
67         if(pthread_mutex_lock(&mutex)) perror("Mutex::lock");
68         set_owner();
69
70
71 // Update recursive status for the first lock
72         if(recursive)
73         {
74                 pthread_mutex_lock(&recursive_lock);
75                 count = 1;
76                 thread_id = pthread_self();
77                 thread_id_valid = 1;
78                 pthread_mutex_unlock(&recursive_lock);
79         }
80         else
81         {
82                 count = 1;
83         }
84
85
86         SET_LOCK2
87         return 0;
88 }
89
90 int Mutex::unlock()
91 {
92         if( count <= 0 ) {
93                 printf("Mutex::unlock not locked: %s\n", title);
94                 return 0;
95         }
96 // Remove from recursive status
97         if(recursive)
98         {
99                 pthread_mutex_lock(&recursive_lock);
100                 count--;
101 // Still locked
102                 if(count > 0)
103                 {
104                         pthread_mutex_unlock(&recursive_lock);
105                         return 0;
106                 }
107 // Not owned anymore
108                 thread_id = 0;
109                 thread_id_valid = 0;
110                 pthread_mutex_unlock(&recursive_lock);
111         }
112         else
113                 count = 0;
114
115
116         UNSET_LOCK(this);
117         unset_owner();
118
119         int ret = pthread_mutex_unlock(&mutex);
120         if( ret ) fprintf(stderr, "Mutex::unlock: %s\n",strerror(ret));
121         return 0;
122 }
123
124 int Mutex::trylock(const char *location)
125 {
126         if( count ) return EBUSY;
127         int ret = pthread_mutex_trylock(&mutex);
128         if( ret ) return ret;
129         set_owner();
130
131 // Update recursive status for the first lock
132         if(recursive) {
133                 pthread_mutex_lock(&recursive_lock);
134                 count = 1;
135                 thread_id = pthread_self();
136                 thread_id_valid = 1;
137                 pthread_mutex_unlock(&recursive_lock);
138         }
139         else
140                 count = 1;
141
142         SET_LOCK(this, title, location);
143         SET_LOCK2
144         return 0;
145 }
146
147 int Mutex::is_locked()
148 {
149         return count;
150 }
151
152 int Mutex::reset()
153 {
154         pthread_mutex_destroy(&mutex);
155         pthread_mutexattr_t attr;
156         pthread_mutexattr_init(&attr);
157         pthread_mutex_init(&mutex, &attr);
158         UNSET_ALL_LOCKS(this)
159         unset_owner();
160         trace = 0;
161         count = 0;
162         thread_id = 0;
163         thread_id_valid = 0;
164         return 0;
165 }