| 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 LockGuard<Mutex> lock_guard(mutex); |
| 56 count = busy_lock_counter; | 56 count = busy_lock_counter; |
| 57 CHECK_EQ(0, mutex->Unlock()); | |
| 58 yield(); | 57 yield(); |
| 59 } while (count % 2 == rem && count < kLockCounterLimit); | 58 } while (count % 2 == rem && count < kLockCounterLimit); |
| 60 if (count >= kLockCounterLimit) break; | 59 if (count >= kLockCounterLimit) break; |
| 61 CHECK_EQ(0, mutex->Lock()); | 60 LockGuard<Mutex> lock_guard(mutex); |
| 62 CHECK_EQ(count, busy_lock_counter); | 61 CHECK_EQ(count, busy_lock_counter); |
| 63 CHECK(last_count == -1 || count == last_count + 1); | 62 CHECK(last_count == -1 || count == last_count + 1); |
| 64 busy_lock_counter++; | 63 busy_lock_counter++; |
| 65 last_count = count; | 64 last_count = count; |
| 66 CHECK_EQ(0, mutex->Unlock()); | |
| 67 yield(); | 65 yield(); |
| 68 } | 66 } |
| 69 } | 67 } |
| 70 | 68 |
| 71 | 69 |
| 72 static void* RunTestBusyLock(void* arg) { | 70 static void* RunTestBusyLock(void* arg) { |
| 73 LoopIncrement(static_cast<Mutex*>(arg), 0); | 71 LoopIncrement(static_cast<Mutex*>(arg), 0); |
| 74 return 0; | 72 return 0; |
| 75 } | 73 } |
| 76 | 74 |
| 77 | 75 |
| 78 // Runs two threads that repeatedly acquire the lock and conditionally | 76 // Runs two threads that repeatedly acquire the lock and conditionally |
| 79 // increment a variable. | 77 // increment a variable. |
| 80 TEST(BusyLock) { | 78 TEST(BusyLock) { |
| 81 pthread_t other; | 79 pthread_t other; |
| 82 Mutex* mutex = OS::CreateMutex(); | 80 Mutex mutex; |
| 83 int thread_created = pthread_create(&other, | 81 int thread_created = pthread_create(&other, |
| 84 NULL, | 82 NULL, |
| 85 &RunTestBusyLock, | 83 &RunTestBusyLock, |
| 86 mutex); | 84 &mutex); |
| 87 CHECK_EQ(0, thread_created); | 85 CHECK_EQ(0, thread_created); |
| 88 LoopIncrement(mutex, 1); | 86 LoopIncrement(&mutex, 1); |
| 89 pthread_join(other, NULL); | 87 pthread_join(other, NULL); |
| 90 delete mutex; | |
| 91 } | 88 } |
| 92 | 89 |
| 93 | 90 |
| 94 TEST(VirtualMemory) { | 91 TEST(VirtualMemory) { |
| 95 OS::SetUp(); | 92 OS::SetUp(); |
| 96 VirtualMemory* vm = new VirtualMemory(1 * MB); | 93 VirtualMemory* vm = new VirtualMemory(1 * MB); |
| 97 CHECK(vm->IsReserved()); | 94 CHECK(vm->IsReserved()); |
| 98 void* block_addr = vm->address(); | 95 void* block_addr = vm->address(); |
| 99 size_t block_size = 4 * KB; | 96 size_t block_size = 4 * KB; |
| 100 CHECK(vm->Commit(block_addr, block_size, false)); | 97 CHECK(vm->Commit(block_addr, block_size, false)); |
| 101 // Check whether we can write to memory. | 98 // Check whether we can write to memory. |
| 102 int* addr = static_cast<int*>(block_addr); | 99 int* addr = static_cast<int*>(block_addr); |
| 103 addr[KB-1] = 2; | 100 addr[KB-1] = 2; |
| 104 CHECK(vm->Uncommit(block_addr, block_size)); | 101 CHECK(vm->Uncommit(block_addr, block_size)); |
| 105 delete vm; | 102 delete vm; |
| 106 } | 103 } |
| 107 | 104 |
| 108 | 105 |
| 109 TEST(GetCurrentProcessId) { | 106 TEST(GetCurrentProcessId) { |
| 110 OS::SetUp(); | 107 OS::SetUp(); |
| 111 CHECK_EQ(static_cast<int>(getpid()), OS::GetCurrentProcessId()); | 108 CHECK_EQ(static_cast<int>(getpid()), OS::GetCurrentProcessId()); |
| 112 } | 109 } |
| OLD | NEW |