Jack2  1.9.8
JackWinMutex.h
00001 /*
00002  Copyright (C) 2004-2008 Grame
00003 
00004  This program is free software; you can redistribute it and/or modify
00005  it under the terms of the GNU Lesser General Public License as published by
00006  the Free Software Foundation; either version 2.1 of the License, or
00007  (at your option) any later version.
00008 
00009  This program is distributed in the hope that it will be useful,
00010  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  GNU Lesser General Public License for more details.
00013 
00014  You should have received a copy of the GNU Lesser General Public License
00015  along with this program; if not, write to the Free Software
00016  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017 
00018  */
00019 
00020 
00021 #ifndef __JackWinMutex__
00022 #define __JackWinMutex__
00023 
00024 #include "JackError.h"
00025 #include "JackException.h"
00026 #include <windows.h>
00027 
00028 namespace Jack
00029 {
00033 class JackBaseWinMutex
00034 {
00035 
00036     protected:
00037 
00038         HANDLE fMutex;
00039         DWORD fOwner;
00040 
00041     public:
00042 
00043         JackBaseWinMutex():fOwner(0)
00044         {
00045             // In recursive mode by default
00046             fMutex = (HANDLE)CreateMutex(0, FALSE, 0);
00047             ThrowIf(fMutex == 0, JackException("JackWinMutex: could not init the mutex"));
00048         }
00049 
00050         virtual ~JackBaseWinMutex()
00051         {
00052             CloseHandle(fMutex);
00053         }
00054 
00055         bool Lock()
00056         {
00057             if (fOwner != GetCurrentThreadId()) {
00058                 DWORD res = WaitForSingleObject(fMutex, INFINITE);
00059                 if (res == WAIT_OBJECT_0) {
00060                     fOwner = GetCurrentThreadId();
00061                     return true;
00062                 } else {
00063                     jack_log("JackWinMutex::Lock res = %d", res);
00064                     return false;
00065                 }
00066             } else {
00067                 jack_error("JackWinMutex::Lock mutex already locked by thread = %d", GetCurrentThreadId());
00068                 return false;
00069             }
00070         }
00071 
00072         bool Trylock()
00073         {
00074             if (fOwner != GetCurrentThreadId()) {
00075                 DWORD res = WaitForSingleObject(fMutex, 0);
00076                 if (res == WAIT_OBJECT_0) {
00077                     fOwner = GetCurrentThreadId();
00078                     return true;
00079                 } else {
00080                     jack_log("JackWinMutex::Trylock res = %d", res);
00081                     return false;
00082                 }
00083             } else {
00084                 jack_error("JackWinMutex::Trylock mutex already locked by thread = %d", GetCurrentThreadId());
00085                 return false;
00086             }
00087         }
00088 
00089         bool Unlock()
00090         {
00091             if (fOwner == GetCurrentThreadId()) {
00092                 fOwner = 0;
00093                 int res = ReleaseMutex(fMutex);
00094                 if (res != 0) {
00095                     return true;
00096                 } else {
00097                     jack_log("JackWinMutex::Unlock res = %d", res);
00098                     return false;
00099                 }
00100             } else {
00101                 jack_error("JackWinMutex::Unlock mutex not locked by thread = %d", GetCurrentThreadId());
00102                 return false;
00103             }
00104         }
00105 
00106 };
00107 
00108 class JackWinMutex
00109 {
00110 
00111     protected:
00112 
00113         HANDLE fMutex;
00114 
00115     public:
00116 
00117         JackWinMutex()
00118         {
00119             // In recursive mode by default
00120             fMutex = (HANDLE)CreateMutex(0, FALSE, 0);
00121         }
00122 
00123         virtual ~JackWinMutex()
00124         {
00125             CloseHandle(fMutex);
00126         }
00127 
00128         bool Lock()
00129         {
00130             return (WAIT_OBJECT_0 == WaitForSingleObject(fMutex, INFINITE));
00131         }
00132 
00133         bool Trylock()
00134         {
00135             return (WAIT_OBJECT_0 == WaitForSingleObject(fMutex, 0));
00136         }
00137 
00138         bool Unlock()
00139         {
00140             return(ReleaseMutex(fMutex) != 0);
00141         }
00142 
00143 };
00144 
00145 
00146 } // namespace
00147 
00148 #endif