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 <pthread.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <unistd.h> // for usleep() |
| 8 | |
| 9 | #include "v8.h" |
| 10 | |
| 11 | #include "platform.h" |
| 12 | #include "cctest.h" |
| 13 | |
| 14 | using namespace ::v8::internal; |
| 15 | |
| 16 | |
| 17 | static void yield() { |
| 18 | usleep(1); |
| 19 | } |
| 20 | |
| 21 | static const int kLockCounterLimit = 50; |
| 22 | static int busy_lock_counter = 0; |
| 23 | |
| 24 | |
| 25 | static void LoopIncrement(Mutex* mutex, int rem) { |
| 26 | while (true) { |
| 27 | int count = 0; |
| 28 | int last_count = -1; |
| 29 | do { |
| 30 | CHECK_EQ(0, mutex->Lock()); |
| 31 | count = busy_lock_counter; |
| 32 | CHECK_EQ(0, mutex->Unlock()); |
| 33 | yield(); |
| 34 | } while (count % 2 == rem && count < kLockCounterLimit); |
| 35 | if (count >= kLockCounterLimit) break; |
| 36 | CHECK_EQ(0, mutex->Lock()); |
| 37 | CHECK_EQ(count, busy_lock_counter); |
| 38 | CHECK(last_count == -1 || count == last_count + 1); |
| 39 | busy_lock_counter++; |
| 40 | last_count = count; |
| 41 | CHECK_EQ(0, mutex->Unlock()); |
| 42 | yield(); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | |
| 47 | static void* RunTestBusyLock(void* arg) { |
| 48 | LoopIncrement(static_cast<Mutex*>(arg), 0); |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | |
| 53 | // Runs two threads that repeatedly acquire the lock and conditionally |
| 54 | // increment a variable. |
| 55 | TEST(BusyLock) { |
| 56 | pthread_t other; |
| 57 | Mutex* mutex = OS::CreateMutex(); |
| 58 | int thread_created = pthread_create(&other, |
| 59 | NULL, |
| 60 | &RunTestBusyLock, |
| 61 | mutex); |
| 62 | CHECK_EQ(0, thread_created); |
| 63 | LoopIncrement(mutex, 1); |
| 64 | pthread_join(other, NULL); |
| 65 | delete mutex; |
| 66 | } |
| 67 | |
| 68 | |
| 69 | TEST(VirtualMemory) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 70 | OS::Setup(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 71 | VirtualMemory* vm = new VirtualMemory(1 * MB); |
| 72 | CHECK(vm->IsReserved()); |
| 73 | void* block_addr = vm->address(); |
| 74 | size_t block_size = 4 * KB; |
| 75 | CHECK(vm->Commit(block_addr, block_size, false)); |
| 76 | // Check whether we can write to memory. |
| 77 | int* addr = static_cast<int*>(block_addr); |
| 78 | addr[KB-1] = 2; |
| 79 | CHECK(vm->Uncommit(block_addr, block_size)); |
| 80 | delete vm; |
| 81 | } |