semaphoreTest.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2014 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. #ifdef TORQUE_TESTS_ENABLED
  23. #include "testing/unitTesting.h"
  24. #include "platform/threads/semaphore.h"
  25. #include "platform/threads/thread.h"
  26. TEST(Semaphore, BasicSynchronization)
  27. {
  28. Semaphore *sem1 = new Semaphore(1);
  29. Semaphore *sem2 = new Semaphore(1);
  30. // Test that we can do non-blocking acquires that succeed.
  31. EXPECT_TRUE(sem1->acquire(false))
  32. << "Should succeed at acquiring a new semaphore with count 1.";
  33. EXPECT_TRUE(sem2->acquire(false))
  34. << "This one should succeed too, see previous test.";
  35. // Test that we can do non-blocking acquires that fail.
  36. EXPECT_FALSE(sem1->acquire(false))
  37. << "Should failed, as we've already got the sem.";
  38. sem1->release();
  39. EXPECT_FALSE(sem2->acquire(false))
  40. << "Should also fail.";
  41. sem2->release();
  42. // Test that we can do blocking acquires that succeed.
  43. EXPECT_TRUE(sem1->acquire(true))
  44. << "Should succeed as we just released.";
  45. EXPECT_TRUE(sem2->acquire(true))
  46. << "Should succeed as we just released.";
  47. // Clean up.
  48. delete sem1;
  49. delete sem2;
  50. }
  51. TEST(Semaphore, MultiThreadSynchronization)
  52. {
  53. Semaphore semaphore(1);
  54. struct thread
  55. {
  56. // Try to acquire the semaphore from another thread.
  57. static void body1(void* sem)
  58. {
  59. Semaphore *semaphore = reinterpret_cast<Semaphore*>(sem);
  60. EXPECT_TRUE(semaphore->acquire(true));
  61. // Note that this semaphore is never released. Bad programmer!
  62. }
  63. // One more acquisition should fail!
  64. static void body2(void* sem)
  65. {
  66. Semaphore *semaphore = reinterpret_cast<Semaphore*>(sem);
  67. EXPECT_FALSE(semaphore->acquire(false));
  68. }
  69. };
  70. Thread thread1(&thread::body1, &semaphore);
  71. EXPECT_TRUE(semaphore.acquire(true));
  72. thread1.start();
  73. semaphore.release();
  74. thread1.join();
  75. Thread thread2(&thread::body2, &semaphore);
  76. thread2.start();
  77. thread2.join();
  78. }
  79. #endif