Jack2  1.9.9
JackWinMutex.cpp
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 #include "JackWinMutex.h"
21 #include "JackError.h"
22 
23 namespace Jack
24 {
25 
26  bool JackBaseWinMutex::Lock()
27  {
28  if (fOwner != GetCurrentThreadId()) {
29  DWORD res = WaitForSingleObject(fMutex, INFINITE);
30  if (res == WAIT_OBJECT_0) {
31  fOwner = GetCurrentThreadId();
32  return true;
33  } else {
34  jack_log("JackBaseWinMutex::Lock res = %d", res);
35  return false;
36  }
37  } else {
38  jack_error("JackBaseWinMutex::Lock mutex already locked by thread = %d", GetCurrentThreadId());
39  return false;
40  }
41  }
42 
43  bool JackBaseWinMutex::Trylock()
44  {
45  if (fOwner != GetCurrentThreadId()) {
46  DWORD res = WaitForSingleObject(fMutex, 0);
47  if (res == WAIT_OBJECT_0) {
48  fOwner = GetCurrentThreadId();
49  return true;
50  } else {
51  jack_log("JackBaseWinMutex::Trylock res = %d", res);
52  return false;
53  }
54  } else {
55  jack_error("JackBaseWinMutex::Trylock mutex already locked by thread = %d", GetCurrentThreadId());
56  return false;
57  }
58  }
59 
60  bool JackBaseWinMutex::Unlock()
61  {
62  if (fOwner == GetCurrentThreadId()) {
63  fOwner = 0;
64  int res = ReleaseMutex(fMutex);
65  if (res != 0) {
66  return true;
67  } else {
68  jack_log("JackBaseWinMutex::Unlock res = %d", res);
69  return false;
70  }
71  } else {
72  jack_error("JackBaseWinMutex::Unlock mutex not locked by thread = %d", GetCurrentThreadId());
73  return false;
74  }
75  }
76 
77  bool JackWinMutex::Lock()
78  {
79  if (WAIT_OBJECT_0 == WaitForSingleObject(fMutex, INFINITE)) {
80  return true;
81  } else {
82  jack_log("JackWinProcessSync::Lock WaitForSingleObject err = %d", GetLastError());
83  return false;
84  }
85  }
86 
87  bool JackWinMutex::Trylock()
88  {
89  if (WAIT_OBJECT_0 == WaitForSingleObject(fMutex, 0)) {
90  return true;
91  } else {
92  jack_log("JackWinProcessSync::Trylock WaitForSingleObject err = %d", GetLastError());
93  return false;
94  }
95  }
96 
97  bool JackWinMutex::Unlock()
98  {
99  if (!ReleaseMutex(fMutex)) {
100  jack_log("JackWinProcessSync::Unlock ReleaseMutex err = %d", GetLastError());
101  return false;
102  } else {
103  return true;
104  }
105  }
106 
107  bool JackWinCriticalSection::Lock()
108  {
109  EnterCriticalSection(&fSection);
110  return true;
111  }
112 
113  bool JackWinCriticalSection::Trylock()
114  {
115  return (TryEnterCriticalSection(&fSection));
116  }
117 
118  bool JackWinCriticalSection::Unlock()
119  {
120  LeaveCriticalSection(&fSection);
121  return true;
122  }
123 
124 } // namespace
125 
126 
SERVER_EXPORT void jack_error(const char *fmt,...)
Definition: JackError.cpp:91
SERVER_EXPORT void jack_log(const char *fmt,...)
Definition: JackError.cpp:107