ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2 | // |
| 3 | // Tests of the TokenLock class from lock.h |
| 4 | |
| 5 | #include <stdlib.h> |
| 6 | |
| 7 | #include "v8.h" |
| 8 | |
| 9 | #include "platform.h" |
| 10 | #include "cctest.h" |
| 11 | |
| 12 | |
| 13 | using namespace ::v8::internal; |
| 14 | |
| 15 | |
| 16 | // Simple test of locking logic |
| 17 | TEST(Simple) { |
| 18 | Mutex* mutex = OS::CreateMutex(); |
| 19 | CHECK_EQ(0, mutex->Lock()); // acquire the lock with the right token |
| 20 | CHECK_EQ(0, mutex->Unlock()); // can unlock with the right token |
| 21 | delete mutex; |
| 22 | } |
| 23 | |
| 24 | |
| 25 | TEST(MultiLock) { |
| 26 | Mutex* mutex = OS::CreateMutex(); |
| 27 | CHECK_EQ(0, mutex->Lock()); |
| 28 | CHECK_EQ(0, mutex->Unlock()); |
| 29 | delete mutex; |
| 30 | } |
| 31 | |
| 32 | |
| 33 | TEST(ShallowLock) { |
| 34 | Mutex* mutex = OS::CreateMutex(); |
| 35 | CHECK_EQ(0, mutex->Lock()); |
| 36 | CHECK_EQ(0, mutex->Unlock()); |
| 37 | CHECK_EQ(0, mutex->Lock()); |
| 38 | CHECK_EQ(0, mutex->Unlock()); |
| 39 | delete mutex; |
| 40 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 41 | |
| 42 | |
| 43 | TEST(SemaphoreTimeout) { |
| 44 | bool ok; |
| 45 | Semaphore* sem = OS::CreateSemaphore(0); |
| 46 | |
| 47 | // Semaphore not signalled - timeout. |
| 48 | ok = sem->Wait(0); |
| 49 | CHECK(!ok); |
| 50 | ok = sem->Wait(100); |
| 51 | CHECK(!ok); |
| 52 | ok = sem->Wait(1000); |
| 53 | CHECK(!ok); |
| 54 | |
| 55 | // Semaphore signalled - no timeout. |
| 56 | sem->Signal(); |
| 57 | ok = sem->Wait(0); |
| 58 | sem->Signal(); |
| 59 | ok = sem->Wait(100); |
| 60 | sem->Signal(); |
| 61 | ok = sem->Wait(1000); |
| 62 | CHECK(ok); |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 63 | delete sem; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 64 | } |