resourcelock.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #ifdef SYSTEM_WIN32
  2. #define WIN32_LEAN_AND_MEAN
  3. #include <windows.h>
  4. #endif
  5. #include "cmdlib.h"
  6. #include "messages.h"
  7. #include "log.h"
  8. #include "blockmem.h"
  9. #include "resourcelock.h"
  10. #ifdef SYSTEM_WIN32
  11. typedef struct
  12. {
  13. HANDLE Mutex;
  14. }
  15. ResourceLock_t;
  16. void* CreateResourceLock(int LockNumber)
  17. {
  18. char lockname[_MAX_PATH];
  19. char mapname[_MAX_PATH];
  20. ResourceLock_t* lock = (ResourceLock_t*)Alloc(sizeof(ResourceLock_t));
  21. ExtractFile(g_Mapname, mapname);
  22. safe_snprintf(lockname, _MAX_PATH, "%d%s", LockNumber, mapname);
  23. lock->Mutex = CreateMutex(NULL, FALSE, lockname);
  24. if (lock->Mutex == NULL)
  25. {
  26. LPVOID lpMsgBuf;
  27. Log("lock->Mutex is NULL! [%s]", lockname);
  28. FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
  29. FORMAT_MESSAGE_FROM_SYSTEM |
  30. FORMAT_MESSAGE_IGNORE_INSERTS,
  31. NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) & lpMsgBuf, 0, NULL);
  32. Error((LPCTSTR)lpMsgBuf);
  33. }
  34. WaitForSingleObject(lock->Mutex, INFINITE);
  35. return lock;
  36. }
  37. void ReleaseResourceLock(void** lock)
  38. {
  39. ResourceLock_t* lockTmp = (ResourceLock_t*)*lock;
  40. if (!ReleaseMutex(lockTmp->Mutex))
  41. {
  42. Error("Failed to release mutex");
  43. }
  44. Free(lockTmp);
  45. *lock = NULL;
  46. }
  47. #endif