11 #include <drift/dslcore.h>
15 #include <drift/Mutex.h>
16 #include <drift/Threading.h>
19 DSL_Mutex_Win32Mutex::DSL_Mutex_Win32Mutex(
int timeout,
const char * name) {
22 lock_timeout = timeout;
23 hMutex = CreateMutexA(NULL, FALSE, name);
26 DSL_Mutex_Win32Mutex::~DSL_Mutex_Win32Mutex() {
30 bool DSL_Mutex_Win32Mutex::Lock() {
31 if (lock_timeout >= 0) {
32 return Lock(lock_timeout);
35 DWORD ret = WaitForSingleObject(hMutex, INFINITE);
36 if (ret != WAIT_OBJECT_0) {
37 printf(
"Error while locking mutex! (%p, %p, %d, %d)\n",
this, hMutex, ret, GetLastError());
40 LockingThreadID = GetCurrentThreadId();
42 if (refcnt <= 0) { refcnt = 1; }
46 bool DSL_Mutex_Win32Mutex::Lock(
int timeout) {
47 DWORD ret = WaitForSingleObject(hMutex, timeout);
48 if (ret == WAIT_TIMEOUT) {
50 printf(
"Timeout while locking mutex! (%p, %p, %d)\n",
this, hMutex, GetLastError());
54 if (ret != WAIT_OBJECT_0) {
55 printf(
"Error while locking mutex! (%p, %p, %d, %d)\n",
this, hMutex, ret, GetLastError());
59 LockingThreadID = GetCurrentThreadId();
61 if (refcnt <= 0) { refcnt = 1; }
65 void DSL_Mutex_Win32Mutex::Release() {
70 if (ReleaseMutex(hMutex) == 0) {
71 printf(
"Error while unlocking mutex! (%p, %p, %d)\n",
this, hMutex, GetLastError());
76 THREADIDTYPE DSL_Mutex_Win32Mutex::LockingThread() {
77 return LockingThreadID;
80 bool DSL_Mutex_Win32Mutex::IsLockMine() {
81 if (refcnt > 0 && GetCurrentThreadId() == LockingThreadID) {
return true; }
85 bool DSL_Mutex_Win32Mutex::IsLocked() {
86 if (refcnt > 0) {
return true; }