mutex.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "mutex.h"
  19. #include "wwdebug.h"
  20. #include <windows.h>
  21. // ----------------------------------------------------------------------------
  22. MutexClass::MutexClass(const char* name) : handle(NULL), locked(false)
  23. {
  24. #ifdef _UNIX
  25. //assert(0);
  26. #else
  27. handle=CreateMutex(NULL,false,name);
  28. WWASSERT(handle);
  29. #endif
  30. }
  31. MutexClass::~MutexClass()
  32. {
  33. #ifdef _UNIX
  34. //assert(0);
  35. #else
  36. WWASSERT(!locked); // Can't delete locked mutex!
  37. CloseHandle(handle);
  38. #endif
  39. }
  40. bool MutexClass::Lock(int time)
  41. {
  42. #ifdef _UNIX
  43. //assert(0);
  44. return true;
  45. #else
  46. int res = WaitForSingleObject(handle,time==WAIT_INFINITE ? INFINITE : time);
  47. if (res!=WAIT_OBJECT_0) return false;
  48. locked++;
  49. return true;
  50. #endif
  51. }
  52. void MutexClass::Unlock()
  53. {
  54. #ifdef _UNIX
  55. //assert(0);
  56. #else
  57. WWASSERT(locked);
  58. locked--;
  59. int res=ReleaseMutex(handle);
  60. WWASSERT(res);
  61. #endif
  62. }
  63. // ----------------------------------------------------------------------------
  64. MutexClass::LockClass::LockClass(MutexClass& mutex_,int time) : mutex(mutex_)
  65. {
  66. failed=!mutex.Lock(time);
  67. }
  68. MutexClass::LockClass::~LockClass()
  69. {
  70. if (!failed) mutex.Unlock();
  71. }
  72. // ----------------------------------------------------------------------------
  73. CriticalSectionClass::CriticalSectionClass() : handle(NULL), locked(false)
  74. {
  75. #ifdef _UNIX
  76. //assert(0);
  77. #else
  78. handle=new char[sizeof(CRITICAL_SECTION)];
  79. InitializeCriticalSection((CRITICAL_SECTION*)handle);
  80. #endif
  81. }
  82. CriticalSectionClass::~CriticalSectionClass()
  83. {
  84. #ifdef _UNIX
  85. //assert(0);
  86. #else
  87. WWASSERT(!locked); // Can't delete locked mutex!
  88. DeleteCriticalSection((CRITICAL_SECTION*)handle);
  89. delete[] handle;
  90. #endif
  91. }
  92. void CriticalSectionClass::Lock()
  93. {
  94. #ifdef _UNIX
  95. //assert(0);
  96. #else
  97. EnterCriticalSection((CRITICAL_SECTION*)handle);
  98. locked++;
  99. #endif
  100. }
  101. void CriticalSectionClass::Unlock()
  102. {
  103. #ifdef _UNIX
  104. //assert(0);
  105. #else
  106. WWASSERT(locked);
  107. locked--;
  108. LeaveCriticalSection((CRITICAL_SECTION*)handle);
  109. #endif
  110. }
  111. // ----------------------------------------------------------------------------
  112. CriticalSectionClass::LockClass::LockClass(CriticalSectionClass& critical_section) : CriticalSection(critical_section)
  113. {
  114. CriticalSection.Lock();
  115. }
  116. CriticalSectionClass::LockClass::~LockClass()
  117. {
  118. CriticalSection.Unlock();
  119. }