sem4.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. /******************************************************************************\
  19. sem4.cpp Neal Kettler
  20. Simple Posix semaphore class
  21. This is useful because the constructor will automatically call sem_init
  22. and you don't have to worry about it. It also allows for other semaphore
  23. libraries if you don't have posix.
  24. \******************************************************************************/
  25. #include "sem4.h"
  26. #ifdef _REENTRANT
  27. Sem4::Sem4()
  28. {
  29. #ifndef _WINDOWS
  30. sem_init(&sem,1,1);
  31. #else
  32. sem = CreateSemaphore(NULL, 1, 1, NULL);
  33. #endif
  34. }
  35. Sem4::Sem4(uint32 value)
  36. {
  37. #ifndef _WINDOWS
  38. sem_init(&sem,1,value);
  39. #else
  40. sem = CreateSemaphore(NULL, value, value, NULL);
  41. #endif
  42. }
  43. Sem4::~Sem4()
  44. {
  45. #ifndef _WINDOWS
  46. sem_destroy(&sem);
  47. #else
  48. if (sem) CloseHandle(sem);
  49. #endif
  50. }
  51. sint32 Sem4::Wait(void) const
  52. {
  53. #ifndef _WINDOWS
  54. return(sem_wait((sem_t *)&sem));
  55. #else
  56. if (!sem)
  57. return -1; // no semaphore!
  58. DWORD dwWaitResult = WaitForSingleObject(sem, INFINITE);
  59. switch (dwWaitResult) {
  60. case WAIT_OBJECT_0: // The semaphore object was signaled.
  61. return 0;
  62. break;
  63. case WAIT_TIMEOUT: // Should not happen ;)
  64. return -1;
  65. break;
  66. }
  67. return -1;
  68. #endif
  69. }
  70. sint32 Sem4::Post(void) const
  71. {
  72. #ifndef _WINDOWS
  73. return(sem_post((sem_t *)&sem));
  74. #else
  75. if (!sem)
  76. return -1;
  77. if (!ReleaseSemaphore(sem, 1 ,NULL))
  78. return -1;
  79. return 0;
  80. #endif
  81. }
  82. sint32 Sem4::TryWait(void) const
  83. {
  84. #ifndef _WINDOWS
  85. return(sem_trywait((sem_t *)&sem));
  86. #else
  87. if (!sem)
  88. return -1;
  89. DWORD dwWaitResult = WaitForSingleObject(sem, 0L);
  90. switch (dwWaitResult) {
  91. case WAIT_OBJECT_0: // The semaphore object was signaled.
  92. return 0;
  93. break;
  94. case WAIT_TIMEOUT:
  95. return -1;
  96. break;
  97. }
  98. return -1;
  99. #endif
  100. }
  101. sint32 Sem4::GetValue(int *sval) const
  102. {
  103. #ifndef _WINDOWS
  104. return(sem_getvalue((sem_t *)&sem,sval));
  105. #else
  106. if (!sem)
  107. return -1;
  108. long prev;
  109. if (!ReleaseSemaphore(sem, 0, &prev))
  110. return -1;
  111. if (sval)
  112. *sval = prev;
  113. return 0;
  114. #endif
  115. }
  116. sint32 Sem4::Destroy(void)
  117. {
  118. #ifndef _WINDOWS
  119. return(sem_destroy(&sem));
  120. #else
  121. return CloseHandle(sem);
  122. #endif
  123. }
  124. #else
  125. /****************************************************************************
  126. non threaded versions that do nothing
  127. *****************************************************************************/
  128. Sem4::Sem4()
  129. {
  130. }
  131. Sem4::Sem4(uint32)
  132. {
  133. }
  134. Sem4::~Sem4()
  135. {
  136. }
  137. sint32 Sem4::Wait(void) const
  138. {
  139. return(0);
  140. }
  141. sint32 Sem4::Post(void) const
  142. {
  143. return(0);
  144. }
  145. sint32 Sem4::TryWait(void) const
  146. {
  147. return(0);
  148. }
  149. sint32 Sem4::GetValue(int *) const
  150. {
  151. return(0);
  152. }
  153. sint32 Sem4::Destroy(void)
  154. {
  155. return(0);
  156. }
  157. #endif