OLD | NEW |
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 | 45 |
46 static const int kLockCounterLimit = 50; | 46 static const int kLockCounterLimit = 50; |
47 static int busy_lock_counter = 0; | 47 static int busy_lock_counter = 0; |
48 | 48 |
49 | 49 |
50 static void LoopIncrement(Mutex* mutex, int rem) { | 50 static void LoopIncrement(Mutex* mutex, int rem) { |
51 while (true) { | 51 while (true) { |
52 int count = 0; | 52 int count = 0; |
53 int last_count = -1; | 53 int last_count = -1; |
54 do { | 54 do { |
55 CHECK_EQ(0, mutex->Lock()); | 55 mutex->Lock(); |
56 count = busy_lock_counter; | 56 count = busy_lock_counter; |
57 CHECK_EQ(0, mutex->Unlock()); | 57 mutex->Unlock(); |
58 yield(); | 58 yield(); |
59 } while (count % 2 == rem && count < kLockCounterLimit); | 59 } while (count % 2 == rem && count < kLockCounterLimit); |
60 if (count >= kLockCounterLimit) break; | 60 if (count >= kLockCounterLimit) break; |
61 CHECK_EQ(0, mutex->Lock()); | 61 mutex->Lock(); |
62 CHECK_EQ(count, busy_lock_counter); | 62 CHECK_EQ(count, busy_lock_counter); |
63 CHECK(last_count == -1 || count == last_count + 1); | 63 CHECK(last_count == -1 || count == last_count + 1); |
64 busy_lock_counter++; | 64 busy_lock_counter++; |
65 last_count = count; | 65 last_count = count; |
66 CHECK_EQ(0, mutex->Unlock()); | 66 mutex->Unlock(); |
67 yield(); | 67 yield(); |
68 } | 68 } |
69 } | 69 } |
70 | 70 |
71 | 71 |
72 static void* RunTestBusyLock(void* arg) { | 72 static void* RunTestBusyLock(void* arg) { |
73 LoopIncrement(static_cast<Mutex*>(arg), 0); | 73 LoopIncrement(static_cast<Mutex*>(arg), 0); |
74 return 0; | 74 return 0; |
75 } | 75 } |
76 | 76 |
77 | 77 |
78 // Runs two threads that repeatedly acquire the lock and conditionally | 78 // Runs two threads that repeatedly acquire the lock and conditionally |
79 // increment a variable. | 79 // increment a variable. |
80 TEST(BusyLock) { | 80 TEST(BusyLock) { |
81 pthread_t other; | 81 pthread_t other; |
82 Mutex* mutex = OS::CreateMutex(); | 82 Mutex mutex; |
83 int thread_created = pthread_create(&other, | 83 int thread_created = pthread_create(&other, |
84 NULL, | 84 NULL, |
85 &RunTestBusyLock, | 85 &RunTestBusyLock, |
86 mutex); | 86 &mutex); |
87 CHECK_EQ(0, thread_created); | 87 CHECK_EQ(0, thread_created); |
88 LoopIncrement(mutex, 1); | 88 LoopIncrement(&mutex, 1); |
89 pthread_join(other, NULL); | 89 pthread_join(other, NULL); |
90 delete mutex; | |
91 } | 90 } |
92 | 91 |
93 | 92 |
94 TEST(VirtualMemory) { | 93 TEST(VirtualMemory) { |
95 OS::SetUp(); | 94 OS::SetUp(); |
96 VirtualMemory* vm = new VirtualMemory(1 * MB); | 95 VirtualMemory* vm = new VirtualMemory(1 * MB); |
97 CHECK(vm->IsReserved()); | 96 CHECK(vm->IsReserved()); |
98 void* block_addr = vm->address(); | 97 void* block_addr = vm->address(); |
99 size_t block_size = 4 * KB; | 98 size_t block_size = 4 * KB; |
100 CHECK(vm->Commit(block_addr, block_size, false)); | 99 CHECK(vm->Commit(block_addr, block_size, false)); |
101 // Check whether we can write to memory. | 100 // Check whether we can write to memory. |
102 int* addr = static_cast<int*>(block_addr); | 101 int* addr = static_cast<int*>(block_addr); |
103 addr[KB-1] = 2; | 102 addr[KB-1] = 2; |
104 CHECK(vm->Uncommit(block_addr, block_size)); | 103 CHECK(vm->Uncommit(block_addr, block_size)); |
105 delete vm; | 104 delete vm; |
106 } | 105 } |
107 | 106 |
108 | 107 |
109 TEST(GetCurrentProcessId) { | 108 TEST(GetCurrentProcessId) { |
110 OS::SetUp(); | 109 OS::SetUp(); |
111 CHECK_EQ(static_cast<int>(getpid()), OS::GetCurrentProcessId()); | 110 CHECK_EQ(static_cast<int>(getpid()), OS::GetCurrentProcessId()); |
112 } | 111 } |
OLD | NEW |