00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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 }
00125
00126