Jack2  1.9.8
JackWinMutex.h
1 /*
2  Copyright (C) 2004-2008 Grame
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU Lesser General Public License as published by
6  the Free Software Foundation; either version 2.1 of the License, or
7  (at your option) any later version.
8 
9  This program 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
12  GNU Lesser General Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 
18  */
19 
20 
21 #ifndef __JackWinMutex__
22 #define __JackWinMutex__
23 
24 #include "JackError.h"
25 #include "JackException.h"
26 #include <windows.h>
27 
28 namespace Jack
29 {
34 {
35 
36  protected:
37 
38  HANDLE fMutex;
39  DWORD fOwner;
40 
41  public:
42 
43  JackBaseWinMutex():fOwner(0)
44  {
45  // In recursive mode by default
46  fMutex = (HANDLE)CreateMutex(0, FALSE, 0);
47  ThrowIf(fMutex == 0, JackException("JackWinMutex: could not init the mutex"));
48  }
49 
50  virtual ~JackBaseWinMutex()
51  {
52  CloseHandle(fMutex);
53  }
54 
55  bool Lock()
56  {
57  if (fOwner != GetCurrentThreadId()) {
58  DWORD res = WaitForSingleObject(fMutex, INFINITE);
59  if (res == WAIT_OBJECT_0) {
60  fOwner = GetCurrentThreadId();
61  return true;
62  } else {
63  jack_log("JackWinMutex::Lock res = %d", res);
64  return false;
65  }
66  } else {
67  jack_error("JackWinMutex::Lock mutex already locked by thread = %d", GetCurrentThreadId());
68  return false;
69  }
70  }
71 
72  bool Trylock()
73  {
74  if (fOwner != GetCurrentThreadId()) {
75  DWORD res = WaitForSingleObject(fMutex, 0);
76  if (res == WAIT_OBJECT_0) {
77  fOwner = GetCurrentThreadId();
78  return true;
79  } else {
80  jack_log("JackWinMutex::Trylock res = %d", res);
81  return false;
82  }
83  } else {
84  jack_error("JackWinMutex::Trylock mutex already locked by thread = %d", GetCurrentThreadId());
85  return false;
86  }
87  }
88 
89  bool Unlock()
90  {
91  if (fOwner == GetCurrentThreadId()) {
92  fOwner = 0;
93  int res = ReleaseMutex(fMutex);
94  if (res != 0) {
95  return true;
96  } else {
97  jack_log("JackWinMutex::Unlock res = %d", res);
98  return false;
99  }
100  } else {
101  jack_error("JackWinMutex::Unlock mutex not locked by thread = %d", GetCurrentThreadId());
102  return false;
103  }
104  }
105 
106 };
107 
109 {
110 
111  protected:
112 
113  HANDLE fMutex;
114 
115  public:
116 
117  JackWinMutex()
118  {
119  // In recursive mode by default
120  fMutex = (HANDLE)CreateMutex(0, FALSE, 0);
121  }
122 
123  virtual ~JackWinMutex()
124  {
125  CloseHandle(fMutex);
126  }
127 
128  bool Lock()
129  {
130  return (WAIT_OBJECT_0 == WaitForSingleObject(fMutex, INFINITE));
131  }
132 
133  bool Trylock()
134  {
135  return (WAIT_OBJECT_0 == WaitForSingleObject(fMutex, 0));
136  }
137 
138  bool Unlock()
139  {
140  return(ReleaseMutex(fMutex) != 0);
141  }
142 
143 };
144 
145 
146 } // namespace
147 
148 #endif