mutexTest.cpp

Engine/source/platform/threads/test/mutexTest.cpp

More...

Detailed Description

 1
 2//-----------------------------------------------------------------------------
 3// Copyright (c) 2014 GarageGames, LLC
 4//
 5// Permission is hereby granted, free of charge, to any person obtaining a copy
 6// of this software and associated documentation files (the "Software"), to
 7// deal in the Software without restriction, including without limitation the
 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 9// sell copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in
13// all copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21// IN THE SOFTWARE.
22//-----------------------------------------------------------------------------
23
24#ifdef TORQUE_TESTS_ENABLED
25#include "testing/unitTesting.h"
26#include "platform/threads/mutex.h"
27#include "platform/threads/thread.h"
28
29TEST(Mutex, BasicSynchronization)
30{
31   // We test various scenarios wrt to locking and unlocking, in a single
32   // thread, just to make sure our basic primitives are working in the
33   // most basic case.
34   void *mutex1 = Mutex::createMutex();
35   EXPECT_TRUE(mutex1 != NULL)
36      << "First Mutex::createMutex call failed - that's pretty bad!";
37
38   // This mutex is intentionally unused.
39   void *mutex2 = Mutex::createMutex();
40   EXPECT_TRUE(mutex2 != NULL)
41      << "Second Mutex::createMutex call failed - that's pretty bad, too!";
42
43   EXPECT_TRUE(Mutex::lockMutex(mutex1, false))
44      << "Nonblocking call to brand new mutex failed - should not be.";
45   EXPECT_TRUE(Mutex::lockMutex(mutex1, true))
46      << "Failed relocking a mutex from the same thread - should be able to do this.";
47
48   // Try to acquire the mutex from another thread.
49   struct thread
50   {
51      static void body(void* mutex)
52      {
53         // We should not be able to lock the mutex from a separate thread, but
54         // we don't want to block either.
55         EXPECT_FALSE(Mutex::lockMutex(mutex, false));
56      }
57   };
58   Thread thread(&thread::body, mutex1);
59   thread.start();
60   thread.join();
61
62   // Unlock & kill mutex 1
63   Mutex::unlockMutex(mutex1);
64   Mutex::unlockMutex(mutex1);
65   Mutex::destroyMutex(mutex1);
66
67   // Kill mutex2, which was never touched.
68   Mutex::destroyMutex(mutex2);
69}
70
71#endif
72