Torque3D Documentation / _generateds / semaphoreTest.cpp

semaphoreTest.cpp

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