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"
31 \brief Mutex abstraction.
33 class JackBaseWinMutex
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()
57 if (fOwner
!= GetCurrentThreadId()) {
58 DWORD res
= WaitForSingleObject(fMutex
, INFINITE
);
59 if (res
== WAIT_OBJECT_0
) {
60 fOwner
= GetCurrentThreadId();
63 jack_log("JackWinMutex::Lock res = %d", res
);
67 jack_error("JackWinMutex::Lock mutex already locked by thread = %d", GetCurrentThreadId());
74 if (fOwner
!= GetCurrentThreadId()) {
75 DWORD res
= WaitForSingleObject(fMutex
, 0);
76 if (res
== WAIT_OBJECT_0
) {
77 fOwner
= GetCurrentThreadId();
80 jack_log("JackWinMutex::Trylock res = %d", res
);
84 jack_error("JackWinMutex::Trylock mutex already locked by thread = %d", GetCurrentThreadId());
91 if (fOwner
== GetCurrentThreadId()) {
93 int res
= ReleaseMutex(fMutex
);
97 jack_log("JackWinMutex::Unlock res = %d", res
);
101 jack_error("JackWinMutex::Unlock mutex not locked by thread = %d", GetCurrentThreadId());
119 // In recursive mode by default
120 fMutex
= (HANDLE
)CreateMutex(0, FALSE
, 0);
123 virtual ~JackWinMutex()
130 return (WAIT_OBJECT_0
== WaitForSingleObject(fMutex
, INFINITE
));
135 return (WAIT_OBJECT_0
== WaitForSingleObject(fMutex
, 0));
140 return(ReleaseMutex(fMutex
) != 0);