semaphore.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #include <unistd.h>
  25. #include <sys/types.h>
  26. #include <errno.h>
  27. #include <semaphore.h>
  28. #include <time.h>
  29. struct PlatformSemaphore
  30. {
  31. sem_t semaphore;
  32. bool initialized;
  33. PlatformSemaphore(S32 initialCount)
  34. {
  35. initialized = true;
  36. if (sem_init(&semaphore, 0, initialCount) == -1) {
  37. initialized = false;
  38. AssertFatal(0, "PlatformSemaphore constructor - Failed to create Semaphore.");
  39. }
  40. }
  41. ~PlatformSemaphore()
  42. {
  43. sem_destroy(&semaphore);
  44. initialized = false;
  45. }
  46. };
  47. Semaphore::Semaphore(S32 initialCount)
  48. {
  49. mData = new PlatformSemaphore(initialCount);
  50. }
  51. Semaphore::~Semaphore()
  52. {
  53. AssertFatal(mData, "Semaphore destructor - Invalid semaphore.");
  54. delete mData;
  55. }
  56. bool Semaphore::acquire(bool block, S32 timeoutMS)
  57. {
  58. AssertFatal(mData && mData->initialized, "Semaphore::acquire - Invalid semaphore.");
  59. if (block)
  60. {
  61. //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
  62. // Semaphore acquiring is different from the MacOS/Win realization because SDL_SemWaitTimeout() with "infinite" timeout can be too heavy on some platforms.
  63. // (see "man SDL_SemWaitTimeout(3)" for more info)
  64. // "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.
  65. // [bank / Feb-2010]
  66. if (timeoutMS == -1)
  67. {
  68. if (sem_wait(&mData->semaphore) < 0)
  69. AssertFatal(false, "Semaphore::acquire - Wait failed.");
  70. }
  71. else
  72. {
  73. //convert timeoutMS to timespec
  74. timespec ts;
  75. if (clock_gettime(CLOCK_REALTIME, &ts) == -1) {
  76. AssertFatal(false, "Semaphore::acquire - clock_realtime failed.");
  77. }
  78. ts.tv_sec += timeoutMS / 1000;
  79. ts.tv_nsec += (timeoutMS % 1000) * 1000;
  80. if (sem_timedwait(&mData->semaphore, &ts) < 0)
  81. AssertFatal(false, "Semaphore::acquire - Wait with timeout failed.");
  82. }
  83. return (true);
  84. }
  85. else
  86. {
  87. int res = sem_trywait(&mData->semaphore);
  88. return (res == 0);
  89. }
  90. }
  91. void Semaphore::release()
  92. {
  93. AssertFatal(mData, "Semaphore::releaseSemaphore - Invalid semaphore.");
  94. sem_post(&mData->semaphore);
  95. }