semaphore.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 "platform/platformAssert.h"
  23. #include "platform/threads/semaphore.h"
  24. #include <SDL.h>
  25. #include <SDL_thread.h>
  26. class PlatformSemaphore
  27. {
  28. public:
  29. SDL_sem *semaphore;
  30. PlatformSemaphore(S32 initialCount)
  31. {
  32. semaphore = SDL_CreateSemaphore(initialCount);
  33. AssertFatal(semaphore, "PlatformSemaphore constructor - Failed to create SDL Semaphore.");
  34. }
  35. ~PlatformSemaphore()
  36. {
  37. SDL_DestroySemaphore(semaphore);
  38. }
  39. };
  40. Semaphore::Semaphore(S32 initialCount)
  41. {
  42. mData = new PlatformSemaphore(initialCount);
  43. }
  44. Semaphore::~Semaphore()
  45. {
  46. AssertFatal(mData, "Semaphore destructor - Invalid semaphore.");
  47. delete mData;
  48. }
  49. bool Semaphore::acquire(bool block, S32 timeoutMS)
  50. {
  51. AssertFatal(mData && mData->semaphore, "Semaphore::acquire - Invalid semaphore.");
  52. if (block)
  53. {
  54. // Semaphore acquiring is different from the MacOS/Win realization because SDL_SemWaitTimeout() with "infinite" timeout can be too heavy on some platforms.
  55. // (see "man SDL_SemWaitTimeout(3)" for more info)
  56. // "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.
  57. // [bank / Feb-2010]
  58. if (timeoutMS == -1)
  59. {
  60. if (SDL_SemWait(mData->semaphore) < 0)
  61. AssertFatal(false, "Semaphore::acquie - Wait failed.");
  62. }
  63. else
  64. {
  65. if (SDL_SemWaitTimeout(mData->semaphore, timeoutMS) < 0)
  66. AssertFatal(false, "Semaphore::acquie - Wait with timeout failed.");
  67. }
  68. return (true);
  69. }
  70. else
  71. {
  72. int res = SDL_SemTryWait(mData->semaphore);
  73. return (res == 0);
  74. }
  75. }
  76. void Semaphore::release()
  77. {
  78. AssertFatal(mData, "Semaphore::releaseSemaphore - Invalid semaphore.");
  79. SDL_SemPost(mData->semaphore);
  80. }