Tagging the 1.9.8 release of the 'jack2' project.
[jack2.git] / windows / JackWinMutex.h
blob47d2d86abfe039ca5b40dc1fe155c09b5408a91a
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.
21 #ifndef __JackWinMutex__
22 #define __JackWinMutex__
24 #include "JackError.h"
25 #include "JackException.h"
26 #include <windows.h>
28 namespace Jack
30 /*!
31 \brief Mutex abstraction.
33 class JackBaseWinMutex
36 protected:
38 HANDLE fMutex;
39 DWORD fOwner;
41 public:
43 JackBaseWinMutex():fOwner(0)
45 // In recursive mode by default
46 fMutex = (HANDLE)CreateMutex(0, FALSE, 0);
47 ThrowIf(fMutex == 0, JackException("JackWinMutex: could not init the mutex"));
50 virtual ~JackBaseWinMutex()
52 CloseHandle(fMutex);
55 bool Lock()
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;
66 } else {
67 jack_error("JackWinMutex::Lock mutex already locked by thread = %d", GetCurrentThreadId());
68 return false;
72 bool Trylock()
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;
83 } else {
84 jack_error("JackWinMutex::Trylock mutex already locked by thread = %d", GetCurrentThreadId());
85 return false;
89 bool Unlock()
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;
100 } else {
101 jack_error("JackWinMutex::Unlock mutex not locked by thread = %d", GetCurrentThreadId());
102 return false;
108 class JackWinMutex
111 protected:
113 HANDLE fMutex;
115 public:
117 JackWinMutex()
119 // In recursive mode by default
120 fMutex = (HANDLE)CreateMutex(0, FALSE, 0);
123 virtual ~JackWinMutex()
125 CloseHandle(fMutex);
128 bool Lock()
130 return (WAIT_OBJECT_0 == WaitForSingleObject(fMutex, INFINITE));
133 bool Trylock()
135 return (WAIT_OBJECT_0 == WaitForSingleObject(fMutex, 0));
138 bool Unlock()
140 return(ReleaseMutex(fMutex) != 0);
146 } // namespace
148 #endif