6ac188d5e2c7222f1d3af68c02430ff2c6cfeba1
[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         this->trace = 0;
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         UNSET_ALL_LOCKS(this);
49 }
50
51 int Mutex::lock(const char *location)
52 {
53 // Test recursive owner and give up if we already own it
54         if(recursive)
55         {
56                 pthread_mutex_lock(&recursive_lock);
57                 if(thread_id_valid && pthread_self() == thread_id)
58                 {
59                         count++;
60                         pthread_mutex_unlock(&recursive_lock);
61                         return 0;
62                 }
63                 pthread_mutex_unlock(&recursive_lock);
64         }
65
66
67         SET_LOCK(this, title, location);
68         if(pthread_mutex_lock(&mutex)) perror("Mutex::lock");
69
70
71
72 // Update recursive status for the first lock
73         if(recursive)
74         {
75                 pthread_mutex_lock(&recursive_lock);
76                 count = 1;
77                 thread_id = pthread_self();
78                 thread_id_valid = 1;
79                 pthread_mutex_unlock(&recursive_lock);
80         }
81         else
82         {
83                 count = 1;
84         }
85
86
87         SET_LOCK2
88         return 0;
89 }
90
91 int Mutex::unlock()
92 {
93         if( count <= 0 ) {
94                 printf("Mutex::unlock not locked: %s\n", title);
95                 return 0;
96         }
97 // Remove from recursive status
98         if(recursive)
99         {
100                 pthread_mutex_lock(&recursive_lock);
101                 count--;
102 // Still locked
103                 if(count > 0)
104                 {
105                         pthread_mutex_unlock(&recursive_lock);
106                         return 0;
107                 }
108 // Not owned anymore
109                 thread_id = 0;
110                 thread_id_valid = 0;
111                 pthread_mutex_unlock(&recursive_lock);
112         }
113         else
114                 count = 0;
115
116
117         UNSET_LOCK(this);
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
130 // Update recursive status for the first lock
131         if(recursive) {
132                 pthread_mutex_lock(&recursive_lock);
133                 count = 1;
134                 thread_id = pthread_self();
135                 thread_id_valid = 1;
136                 pthread_mutex_unlock(&recursive_lock);
137         }
138         else
139                 count = 1;
140
141         SET_LOCK(this, title, location);
142         SET_LOCK2
143         return 0;
144 }
145
146 int Mutex::is_locked()
147 {
148         return count;
149 }
150
151 int Mutex::reset()
152 {
153         pthread_mutex_destroy(&mutex);
154         pthread_mutexattr_t attr;
155         pthread_mutexattr_init(&attr);
156         pthread_mutex_init(&mutex, &attr);
157         UNSET_ALL_LOCKS(this)
158         trace = 0;
159         count = 0;
160         thread_id = 0;
161         thread_id_valid = 0;
162         return 0;
163 }