Jack2 1.9.10

JackWinMutex.cpp

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 #include "JackWinMutex.h"
00021 #include "JackError.h"
00022 
00023 namespace Jack
00024 {
00025 
00026     bool JackBaseWinMutex::Lock()
00027     {
00028         if (fOwner != GetCurrentThreadId()) {
00029             DWORD res = WaitForSingleObject(fMutex, INFINITE);
00030             if (res == WAIT_OBJECT_0) {
00031                 fOwner = GetCurrentThreadId();
00032                 return true;
00033             } else {
00034                 jack_log("JackBaseWinMutex::Lock res = %d", res);
00035                 return false;
00036             }
00037         } else {
00038             jack_error("JackBaseWinMutex::Lock mutex already locked by thread = %d", GetCurrentThreadId());
00039             return false;
00040         }
00041     }
00042 
00043     bool JackBaseWinMutex::Trylock()
00044     {
00045         if (fOwner != GetCurrentThreadId()) {
00046             DWORD res = WaitForSingleObject(fMutex, 0);
00047             if (res == WAIT_OBJECT_0) {
00048                 fOwner = GetCurrentThreadId();
00049                 return true;
00050             } else {
00051                 jack_log("JackBaseWinMutex::Trylock res = %d", res);
00052                 return false;
00053             }
00054         } else {
00055             jack_error("JackBaseWinMutex::Trylock mutex already locked by thread = %d", GetCurrentThreadId());
00056             return false;
00057         }
00058     }
00059 
00060     bool JackBaseWinMutex::Unlock()
00061     {
00062         if (fOwner == GetCurrentThreadId()) {
00063             fOwner = 0;
00064             int res = ReleaseMutex(fMutex);
00065             if (res != 0) {
00066                 return true;
00067             } else {
00068                 jack_log("JackBaseWinMutex::Unlock res = %d", res);
00069                 return false;
00070             }
00071         } else {
00072             jack_error("JackBaseWinMutex::Unlock mutex not locked by thread = %d", GetCurrentThreadId());
00073             return false;
00074         }
00075     }
00076 
00077     bool JackWinMutex::Lock()
00078     {
00079         if (WAIT_OBJECT_0 == WaitForSingleObject(fMutex, INFINITE)) {
00080             return true;
00081         } else  {
00082             jack_log("JackWinProcessSync::Lock WaitForSingleObject err = %d", GetLastError());
00083             return false;
00084         }
00085     }
00086 
00087     bool JackWinMutex::Trylock()
00088     {
00089         if (WAIT_OBJECT_0 == WaitForSingleObject(fMutex, 0)) {
00090             return true;
00091         } else  {
00092             jack_log("JackWinProcessSync::Trylock WaitForSingleObject err = %d", GetLastError());
00093             return false;
00094         }
00095     }
00096 
00097     bool JackWinMutex::Unlock()
00098     {
00099         if (!ReleaseMutex(fMutex)) {
00100             jack_log("JackWinProcessSync::Unlock ReleaseMutex err = %d", GetLastError());
00101             return false;
00102         } else  {
00103             return true;
00104         }
00105     }
00106 
00107     bool JackWinCriticalSection::Lock()
00108     {
00109         EnterCriticalSection(&fSection);
00110         return true;
00111     }
00112 
00113     bool JackWinCriticalSection::Trylock()
00114     {
00115         return (TryEnterCriticalSection(&fSection));
00116     }
00117 
00118     bool JackWinCriticalSection::Unlock()
00119     {
00120         LeaveCriticalSection(&fSection);
00121         return true;
00122     }
00123 
00124 } // namespace
00125 
00126