-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsync.cpp
38 lines (33 loc) · 873 Bytes
/
sync.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include "stdafx.h"
#include "sync.h"
#include <string>
void Sync::getObjectName(DWORD id, LPWSTR buffer)
{
_itow_s(id, buffer, 50, 16);
}
HANDLE Sync::_createUniqueMutex(SECURITY_ATTRIBUTES * mutexAttributes, LPWSTR name)
{
HANDLE mutexHandle = CreateMutexW(mutexAttributes, TRUE, name);
if (mutexHandle != NULL && GetLastError() == ERROR_ALREADY_EXISTS)
{
CloseHandle(mutexHandle);
mutexHandle = NULL;
}
return mutexHandle;
}
HANDLE Sync::_waitForMutex(SECURITY_ATTRIBUTES *mutexAttributes, LPWSTR name)
{
HANDLE mutexHandle = CreateMutexW(mutexAttributes, FALSE, name);
if (mutexHandle != NULL)
{
DWORD r = WaitForSingleObject(mutexHandle, INFINITE);
if (r == WAIT_OBJECT_0 || r == WAIT_ABANDONED)return mutexHandle;
CloseHandle(mutexHandle);
}
return NULL;
}
void Sync::_freeMutex(HANDLE mutex)
{
ReleaseMutex(mutex);
CloseHandle(mutex);
}