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