2
0

mutex.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. #ifndef MUTEX_H
  19. #define MUTEX_H
  20. #if defined(_MSC_VER)
  21. #pragma once
  22. #endif
  23. #include "always.h"
  24. #include "thread.h"
  25. // Always use mutex or critical section when accessing the same data from multiple threads!
  26. // ----------------------------------------------------------------------------
  27. //
  28. // Mutex class is an expensive way of synchronization! Use critical sections
  29. // (below) for all synchronization. Use mutexes for inter-process locking.
  30. //
  31. // ----------------------------------------------------------------------------
  32. class MutexClass
  33. {
  34. void* handle;
  35. unsigned locked;
  36. // Lock and unlock are private so that you can't use them directly. Use LockClass as a sentry instead!
  37. // Lock returns true if lock was succesful, false otherwise
  38. bool Lock(int time);
  39. void Unlock();
  40. public:
  41. // Name can (and usually should) be NULL. Use name only if you wish to create a globally unique mutex
  42. MutexClass(const char* name = NULL);
  43. ~MutexClass();
  44. enum {
  45. WAIT_INFINITE=-1
  46. };
  47. class LockClass
  48. {
  49. MutexClass& mutex;
  50. bool failed;
  51. public:
  52. // In order to lock a mutex create a local instance of LockClass with mutex as a parameter.
  53. // Time is in milliseconds, INFINITE means infinite wait.
  54. LockClass(MutexClass& m, int time=MutexClass::WAIT_INFINITE);
  55. ~LockClass();
  56. // Returns true if the lock failed
  57. bool Failed() { return failed; }
  58. private:
  59. LockClass &operator=(const LockClass&) { return(*this); }
  60. };
  61. friend class LockClass;
  62. };
  63. // ----------------------------------------------------------------------------
  64. //
  65. // Critical sections are faster than mutex classes and they should be used
  66. // for all synchronization.
  67. //
  68. // ----------------------------------------------------------------------------
  69. class CriticalSectionClass
  70. {
  71. void* handle;
  72. unsigned locked;
  73. // Lock and unlock are private so that you can't use them directly. Use LockClass as a sentry instead!
  74. void Lock();
  75. void Unlock();
  76. public:
  77. // Name can (and usually should) be NULL. Use name only if you wish to create a globally unique mutex
  78. CriticalSectionClass();
  79. ~CriticalSectionClass();
  80. class LockClass
  81. {
  82. CriticalSectionClass& CriticalSection;
  83. public:
  84. // In order to lock a mutex create a local instance of LockClass with mutex as a parameter.
  85. LockClass(CriticalSectionClass& c);
  86. ~LockClass();
  87. private:
  88. LockClass &operator=(const LockClass&) { return(*this); }
  89. };
  90. friend class LockClass;
  91. };
  92. // ----------------------------------------------------------------------------
  93. //
  94. // Fast critical section is really fast version of CriticalSection. The downside
  95. // of it is that it can't be locked multiple times from the same thread.
  96. //
  97. // ----------------------------------------------------------------------------
  98. class FastCriticalSectionClass
  99. {
  100. volatile unsigned Flag;
  101. void Thread_Safe_Set_Flag()
  102. {
  103. volatile unsigned& nFlag=Flag;
  104. #define ts_lock _emit 0xF0
  105. assert(((unsigned)&nFlag % 4) == 0);
  106. __asm mov ebx, [nFlag]
  107. __asm ts_lock
  108. __asm bts dword ptr [ebx], 0
  109. __asm jc The_Bit_Was_Previously_Set_So_Try_Again
  110. return;
  111. The_Bit_Was_Previously_Set_So_Try_Again:
  112. ThreadClass::Switch_Thread();
  113. __asm mov ebx, [nFlag]
  114. __asm ts_lock
  115. __asm bts dword ptr [ebx], 0
  116. __asm jc The_Bit_Was_Previously_Set_So_Try_Again
  117. }
  118. WWINLINE void Thread_Safe_Clear_Flag()
  119. {
  120. Flag = 0;
  121. }
  122. public:
  123. // Name can (and usually should) be NULL. Use name only if you wish to create a globally unique mutex
  124. FastCriticalSectionClass() : Flag(0) {}
  125. class LockClass
  126. {
  127. FastCriticalSectionClass& CriticalSection;
  128. public:
  129. LockClass(FastCriticalSectionClass& critical_section) : CriticalSection(critical_section)
  130. {
  131. CriticalSection.Thread_Safe_Set_Flag();
  132. }
  133. ~LockClass()
  134. {
  135. CriticalSection.Thread_Safe_Clear_Flag();
  136. }
  137. private:
  138. LockClass &operator=(const LockClass&) { return(*this); }
  139. };
  140. friend class LockClass;
  141. };
  142. #endif