Jack2  1.9.10
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 "JackCompilerDeps.h"
00025 #include "JackException.h"
00026 #include <windows.h>
00027 #include <stdio.h>
00028 
00029 namespace Jack
00030 {
00031 
00035 class SERVER_EXPORT JackBaseWinMutex
00036 {
00037 
00038     protected:
00039 
00040         HANDLE fMutex;
00041         DWORD fOwner;
00042 
00043     public:
00044 
00045         JackBaseWinMutex():fOwner(0)
00046         {
00047             // In recursive mode by default
00048             fMutex = CreateMutex(NULL, FALSE, NULL);
00049             ThrowIf((fMutex == 0), JackException("JackBaseWinMutex: could not init the mutex"));
00050         }
00051 
00052         virtual ~JackBaseWinMutex()
00053         {
00054             CloseHandle(fMutex);
00055         }
00056 
00057         bool Lock();
00058         bool Trylock();
00059         bool Unlock();
00060 
00061 };
00062 
00063 class SERVER_EXPORT JackWinMutex
00064 {
00065 
00066     protected:
00067 
00068         HANDLE fMutex;
00069 
00070     public:
00071 
00072         JackWinMutex(const char* name = NULL)
00073         {
00074             // In recursive mode by default
00075             if (name) {
00076                 char buffer[MAX_PATH];
00077                 snprintf(buffer, sizeof(buffer), "%s_%s", "JackWinMutex", name);
00078                 fMutex = CreateMutex(NULL, FALSE, buffer);
00079             } else {
00080                 fMutex = CreateMutex(NULL, FALSE, NULL);
00081             }
00082 
00083             ThrowIf((fMutex == 0), JackException("JackWinMutex: could not init the mutex"));
00084         }
00085 
00086         virtual ~JackWinMutex()
00087         {
00088             CloseHandle(fMutex);
00089         }
00090 
00091         bool Lock();
00092         bool Trylock();
00093         bool Unlock();
00094 
00095 };
00096 
00097 class SERVER_EXPORT JackWinCriticalSection
00098 {
00099 
00100     protected:
00101 
00102         CRITICAL_SECTION fSection;
00103 
00104     public:
00105 
00106         JackWinCriticalSection(const char* name = NULL)
00107         {
00108             InitializeCriticalSection(&fSection);
00109         }
00110 
00111         virtual ~JackWinCriticalSection()
00112         {
00113             DeleteCriticalSection(&fSection);
00114         }
00115 
00116         bool Lock();
00117         bool Trylock();
00118         bool Unlock();
00119 
00120 };
00121 
00122 
00123 } // namespace
00124 
00125 #endif
00126