• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2006-2008 the V8 project authors. All rights reserved.
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 
yield()17 static void yield() {
18   UNIMPLEMENTED();
19 }
20 
21 static const int kLockCounterLimit = 50;
22 static int busy_lock_counter = 0;
23 
24 
LoopIncrement(Mutex * mutex,int rem)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 
RunTestBusyLock(void * arg)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.
TEST(BusyLock)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 
TEST(VirtualMemory)69 TEST(VirtualMemory) {
70   VirtualMemory* vm = new VirtualMemory(1 * MB);
71   CHECK(vm->IsReserved());
72   void* block_addr = vm->address();
73   size_t block_size = 4 * KB;
74   CHECK(vm->Commit(block_addr, block_size, false));
75   // Check whether we can write to memory.
76   int* addr = static_cast<int*>(block_addr);
77   addr[KB-1] = 2;
78   CHECK(vm->Uncommit(block_addr, block_size));
79   delete vm;
80 }
81