mutex.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. ** Command & Conquer Generals(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. int res=ReleaseMutex(handle);
  59. res; // silence compiler warnings
  60. WWASSERT(res);
  61. locked--;
  62. #endif
  63. }
  64. // ----------------------------------------------------------------------------
  65. MutexClass::LockClass::LockClass(MutexClass& mutex_,int time) : mutex(mutex_)
  66. {
  67. failed=!mutex.Lock(time);
  68. }
  69. MutexClass::LockClass::~LockClass()
  70. {
  71. if (!failed) mutex.Unlock();
  72. }
  73. // ----------------------------------------------------------------------------
  74. CriticalSectionClass::CriticalSectionClass() : handle(NULL), locked(false)
  75. {
  76. #ifdef _UNIX
  77. //assert(0);
  78. #else
  79. handle=W3DNEWARRAY char[sizeof(CRITICAL_SECTION)];
  80. InitializeCriticalSection((CRITICAL_SECTION*)handle);
  81. #endif
  82. }
  83. CriticalSectionClass::~CriticalSectionClass()
  84. {
  85. #ifdef _UNIX
  86. //assert(0);
  87. #else
  88. WWASSERT(!locked); // Can't delete locked mutex!
  89. DeleteCriticalSection((CRITICAL_SECTION*)handle);
  90. delete[] handle;
  91. #endif
  92. }
  93. void CriticalSectionClass::Lock()
  94. {
  95. #ifdef _UNIX
  96. //assert(0);
  97. #else
  98. EnterCriticalSection((CRITICAL_SECTION*)handle);
  99. locked++;
  100. #endif
  101. }
  102. void CriticalSectionClass::Unlock()
  103. {
  104. #ifdef _UNIX
  105. //assert(0);
  106. #else
  107. WWASSERT(locked);
  108. LeaveCriticalSection((CRITICAL_SECTION*)handle);
  109. locked--;
  110. #endif
  111. }
  112. // ----------------------------------------------------------------------------
  113. CriticalSectionClass::LockClass::LockClass(CriticalSectionClass& critical_section) : CriticalSection(critical_section)
  114. {
  115. CriticalSection.Lock();
  116. }
  117. CriticalSectionClass::LockClass::~LockClass()
  118. {
  119. CriticalSection.Unlock();
  120. }