1// Copyright 2006-2008 the V8 project authors. All rights reserved.
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
13using namespace ::v8::internal;
14
15
16// Simple test of locking logic
17TEST(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
25TEST(MultiLock) {
26  Mutex* mutex = OS::CreateMutex();
27  CHECK_EQ(0, mutex->Lock());
28  CHECK_EQ(0, mutex->Unlock());
29  delete mutex;
30}
31
32
33TEST(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}
41
42
43TEST(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);
63  delete sem;
64}
65