Home | History | Annotate | Download | only in cctest
      1 // Copyright 2006-2008 the V8 project authors. All rights reserved.
      2 // Redistribution and use in source and binary forms, with or without
      3 // modification, are permitted provided that the following conditions are
      4 // met:
      5 //
      6 //     * Redistributions of source code must retain the above copyright
      7 //       notice, this list of conditions and the following disclaimer.
      8 //     * Redistributions in binary form must reproduce the above
      9 //       copyright notice, this list of conditions and the following
     10 //       disclaimer in the documentation and/or other materials provided
     11 //       with the distribution.
     12 //     * Neither the name of Google Inc. nor the names of its
     13 //       contributors may be used to endorse or promote products derived
     14 //       from this software without specific prior written permission.
     15 //
     16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27 //
     28 // Tests of the TokenLock class from lock.h
     29 
     30 #include <pthread.h>
     31 #include <stdlib.h>
     32 #include <unistd.h>  // for usleep()
     33 
     34 #include "v8.h"
     35 
     36 #include "platform.h"
     37 #include "cctest.h"
     38 
     39 using namespace ::v8::internal;
     40 
     41 
     42 static void yield() {
     43   usleep(1);
     44 }
     45 
     46 static const int kLockCounterLimit = 50;
     47 static int busy_lock_counter = 0;
     48 
     49 
     50 static void LoopIncrement(Mutex* mutex, int rem) {
     51   while (true) {
     52     int count = 0;
     53     int last_count = -1;
     54     do {
     55       CHECK_EQ(0, mutex->Lock());
     56       count = busy_lock_counter;
     57       CHECK_EQ(0, mutex->Unlock());
     58       yield();
     59     } while (count % 2 == rem && count < kLockCounterLimit);
     60     if (count >= kLockCounterLimit) break;
     61     CHECK_EQ(0, mutex->Lock());
     62     CHECK_EQ(count, busy_lock_counter);
     63     CHECK(last_count == -1 || count == last_count + 1);
     64     busy_lock_counter++;
     65     last_count = count;
     66     CHECK_EQ(0, mutex->Unlock());
     67     yield();
     68   }
     69 }
     70 
     71 
     72 static void* RunTestBusyLock(void* arg) {
     73   LoopIncrement(static_cast<Mutex*>(arg), 0);
     74   return 0;
     75 }
     76 
     77 
     78 // Runs two threads that repeatedly acquire the lock and conditionally
     79 // increment a variable.
     80 TEST(BusyLock) {
     81   pthread_t other;
     82   Mutex* mutex = OS::CreateMutex();
     83   int thread_created = pthread_create(&other,
     84                                       NULL,
     85                                       &RunTestBusyLock,
     86                                       mutex);
     87   CHECK_EQ(0, thread_created);
     88   LoopIncrement(mutex, 1);
     89   pthread_join(other, NULL);
     90   delete mutex;
     91 }
     92 
     93 
     94 TEST(VirtualMemory) {
     95   OS::SetUp();
     96   VirtualMemory* vm = new VirtualMemory(1 * MB);
     97   CHECK(vm->IsReserved());
     98   void* block_addr = vm->address();
     99   size_t block_size = 4 * KB;
    100   CHECK(vm->Commit(block_addr, block_size, false));
    101   // Check whether we can write to memory.
    102   int* addr = static_cast<int*>(block_addr);
    103   addr[KB-1] = 2;
    104   CHECK(vm->Uncommit(block_addr, block_size));
    105   delete vm;
    106 }
    107 
    108 
    109 TEST(GetCurrentProcessId) {
    110   OS::SetUp();
    111   CHECK_EQ(static_cast<int>(getpid()), OS::GetCurrentProcessId());
    112 }
    113