mutexTest.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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/mutex.h"
  25. #include "platform/threads/thread.h"
  26. TEST(Mutex, BasicSynchronization)
  27. {
  28. // We test various scenarios wrt to locking and unlocking, in a single
  29. // thread, just to make sure our basic primitives are working in the
  30. // most basic case.
  31. void *mutex1 = Mutex::createMutex();
  32. EXPECT_TRUE(mutex1 != NULL)
  33. << "First Mutex::createMutex call failed - that's pretty bad!";
  34. // This mutex is intentionally unused.
  35. void *mutex2 = Mutex::createMutex();
  36. EXPECT_TRUE(mutex2 != NULL)
  37. << "Second Mutex::createMutex call failed - that's pretty bad, too!";
  38. EXPECT_TRUE(Mutex::lockMutex(mutex1, false))
  39. << "Nonblocking call to brand new mutex failed - should not be.";
  40. EXPECT_TRUE(Mutex::lockMutex(mutex1, true))
  41. << "Failed relocking a mutex from the same thread - should be able to do this.";
  42. // Try to acquire the mutex from another thread.
  43. struct thread
  44. {
  45. static void body(void* mutex)
  46. {
  47. // We should not be able to lock the mutex from a separate thread, but
  48. // we don't want to block either.
  49. EXPECT_FALSE(Mutex::lockMutex(mutex, false));
  50. }
  51. };
  52. Thread thread(&thread::body, mutex1);
  53. thread.start();
  54. thread.join();
  55. // Unlock & kill mutex 1
  56. Mutex::unlockMutex(mutex1);
  57. Mutex::unlockMutex(mutex1);
  58. Mutex::destroyMutex(mutex1);
  59. // Kill mutex2, which was never touched.
  60. Mutex::destroyMutex(mutex2);
  61. }
  62. #endif