Jack2  1.9.8
JackPosixMutex.h
1 /*
2  Copyright (C) 2006 Grame
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Lesser General Public
6  License as published by the Free Software Foundation; either
7  version 2.1 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Lesser General Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public
15  License along with this library; if not, write to the Free Software
16  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18  Grame Research Laboratory, 9 rue du Garet, 69001 Lyon - France
19  grame@grame.fr
20 */
21 
22 #ifndef __JackPosixMutex__
23 #define __JackPosixMutex__
24 
25 #include "JackError.h"
26 #include "JackException.h"
27 #include <pthread.h>
28 #include <stdio.h>
29 #include <assert.h>
30 
31 namespace Jack
32 {
38 {
39 
40  protected:
41 
42  pthread_mutex_t fMutex;
43  pthread_t fOwner;
44 
45  public:
46 
47  JackBasePosixMutex():fOwner(0)
48  {
49  int res = pthread_mutex_init(&fMutex, NULL);
50  ThrowIf(res != 0, JackException("JackBasePosixMutex: could not init the mutex"));
51  }
52 
53  virtual ~JackBasePosixMutex()
54  {
55  pthread_mutex_destroy(&fMutex);
56  }
57 
58  bool Lock()
59  {
60  pthread_t current_thread = pthread_self();
61 
62  if (!pthread_equal(current_thread, fOwner)) {
63  int res = pthread_mutex_lock(&fMutex);
64  if (res == 0) {
65  fOwner = current_thread;
66  return true;
67  } else {
68  jack_error("JackBasePosixMutex::Lock res = %d", res);
69  return false;
70  }
71  } else {
72  jack_error("JackBasePosixMutex::Lock mutex already locked by thread = %d", current_thread);
73  return false;
74  }
75  }
76 
77  bool Trylock()
78  {
79  pthread_t current_thread = pthread_self();
80 
81  if (!pthread_equal(current_thread, fOwner)) {
82  int res = pthread_mutex_trylock(&fMutex);
83  if (res == 0) {
84  fOwner = current_thread;
85  return true;
86  } else {
87  return false;
88  }
89  } else {
90  jack_error("JackBasePosixMutex::Trylock mutex already locked by thread = %d", current_thread);
91  return false;
92  }
93  }
94 
95  bool Unlock()
96  {
97  if (pthread_equal(pthread_self(), fOwner)) {
98  fOwner = 0;
99  int res = pthread_mutex_unlock(&fMutex);
100  if (res == 0) {
101  return true;
102  } else {
103  jack_error("JackBasePosixMutex::Unlock res = %d", res);
104  return false;
105  }
106  } else {
107  jack_error("JackBasePosixMutex::Unlock mutex not locked by thread = %d owner %d", pthread_self(), fOwner);
108  return false;
109  }
110  }
111 
112 };
113 
115 {
116  protected:
117 
118  pthread_mutex_t fMutex;
119 
120  public:
121 
123  {
124  // Use recursive mutex
125  pthread_mutexattr_t mutex_attr;
126  int res;
127  res = pthread_mutexattr_init(&mutex_attr);
128  ThrowIf(res != 0, JackException("JackBasePosixMutex: could not init the mutex attribute"));
129  res = pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE);
130  ThrowIf(res != 0, JackException("JackBasePosixMutex: could not settype the mutex"));
131  res = pthread_mutex_init(&fMutex, &mutex_attr);
132  ThrowIf(res != 0, JackException("JackBasePosixMutex: could not init the mutex"));
133  pthread_mutexattr_destroy(&mutex_attr);
134  }
135 
136  virtual ~JackPosixMutex()
137  {
138  pthread_mutex_destroy(&fMutex);
139  }
140 
141  bool Lock()
142  {
143  int res = pthread_mutex_lock(&fMutex);
144  if (res != 0) {
145  jack_log("JackPosixMutex::Lock res = %d", res);
146  }
147  return (res == 0);
148  }
149 
150  bool Trylock()
151  {
152  return (pthread_mutex_trylock(&fMutex) == 0);
153  }
154 
155  bool Unlock()
156  {
157  int res = pthread_mutex_unlock(&fMutex);
158  if (res != 0) {
159  jack_log("JackPosixMutex::Unlock res = %d", res);
160  }
161  return (res == 0);
162  }
163 
164 };
165 
166 
167 } // namespace
168 
169 #endif