Add zalsa configure flag, enabled by default if possible
[jack2.git] / windows / JackWinMutex.cpp
blob30f3fa0533485e268a5b9d6bb5307f2b6e0ef10a
1 /*
2 Copyright (C) 2004-2008 Grame
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.
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.
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.
20 #include "JackWinMutex.h"
21 #include "JackError.h"
23 namespace Jack
26 bool JackBaseWinMutex::Lock()
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;
37 } else {
38 jack_error("JackBaseWinMutex::Lock mutex already locked by thread = %d", GetCurrentThreadId());
39 return false;
43 bool JackBaseWinMutex::Trylock()
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;
54 } else {
55 jack_error("JackBaseWinMutex::Trylock mutex already locked by thread = %d", GetCurrentThreadId());
56 return false;
60 bool JackBaseWinMutex::Unlock()
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;
71 } else {
72 jack_error("JackBaseWinMutex::Unlock mutex not locked by thread = %d", GetCurrentThreadId());
73 return false;
77 bool JackWinMutex::Lock()
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;
87 bool JackWinMutex::Trylock()
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;
97 bool JackWinMutex::Unlock()
99 if (!ReleaseMutex(fMutex)) {
100 jack_log("JackWinProcessSync::Unlock ReleaseMutex err = %d", GetLastError());
101 return false;
102 } else {
103 return true;
107 bool JackWinCriticalSection::Lock()
109 EnterCriticalSection(&fSection);
110 return true;
113 bool JackWinCriticalSection::Trylock()
115 return (TryEnterCriticalSection(&fSection));
118 bool JackWinCriticalSection::Unlock()
120 LeaveCriticalSection(&fSection);
121 return true;
124 } // namespace