2
0

semaphoreTest.cpp 3.1 KB

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