semaphore.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platformX86UNIX/platformX86UNIX.h"
  23. #include "platform/threads/semaphore.h"
  24. //MGT: converted SDL to OS semaphores to remove dependency on SDL in dedicated server
  25. #include <unistd.h>
  26. #include <sys/types.h>
  27. #include <errno.h>
  28. #include <semaphore.h>
  29. #include <time.h>
  30. struct PlatformSemaphore
  31. {
  32. sem_t semaphore;
  33. bool initialized;
  34. PlatformSemaphore(S32 initialCount)
  35. {
  36. initialized = true;
  37. if (sem_init(&semaphore, 0, initialCount) == -1) {
  38. initialized = false;
  39. AssertFatal(0, "PlatformSemaphore constructor - Failed to create Semaphore.");
  40. }
  41. }
  42. ~PlatformSemaphore()
  43. {
  44. sem_destroy(&semaphore);
  45. initialized = false;
  46. }
  47. };
  48. Semaphore::Semaphore(S32 initialCount)
  49. {
  50. mData = new PlatformSemaphore(initialCount);
  51. }
  52. Semaphore::~Semaphore()
  53. {
  54. AssertFatal(mData, "Semaphore destructor - Invalid semaphore.");
  55. delete mData;
  56. }
  57. bool Semaphore::acquire(bool block, S32 timeoutMS)
  58. {
  59. AssertFatal(mData && mData->initialized, "Semaphore::acquire - Invalid semaphore.");
  60. if (block)
  61. {
  62. //SDL was removed so I do not now if this still holds true or not with OS calls but my guess is they are used underneath SDL anyway
  63. // Semaphore acquiring is different from the MacOS/Win realization because SDL_SemWaitTimeout() with "infinite" timeout can be too heavy on some platforms.
  64. // (see "man SDL_SemWaitTimeout(3)" for more info)
  65. // "man" states to avoid the use of SDL_SemWaitTimeout at all, but at current stage this looks like a valid and working solution, so keeping it this way.
  66. // [bank / Feb-2010]
  67. if (timeoutMS == -1)
  68. {
  69. if (sem_wait(&mData->semaphore) < 0)
  70. AssertFatal(false, "Semaphore::acquire - Wait failed.");
  71. }
  72. else
  73. {
  74. //convert timeoutMS to timespec
  75. timespec ts;
  76. if (clock_gettime(CLOCK_REALTIME, &ts) == -1) {
  77. AssertFatal(false, "Semaphore::acquire - clock_realtime failed.");
  78. }
  79. ts.tv_sec += timeoutMS / 1000;
  80. ts.tv_nsec += (timeoutMS % 1000) * 1000;
  81. if (sem_timedwait(&mData->semaphore, &ts) < 0)
  82. AssertFatal(false, "Semaphore::acquire - Wait with timeout failed.");
  83. }
  84. return (true);
  85. }
  86. else
  87. {
  88. int res = sem_trywait(&mData->semaphore);
  89. return (res == 0);
  90. }
  91. }
  92. void Semaphore::release()
  93. {
  94. AssertFatal(mData, "Semaphore::releaseSemaphore - Invalid semaphore.");
  95. sem_post(&mData->semaphore);
  96. }
  97. //MGT: end