The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.

Please select the desired protocol below to get the URL.

This URL has Read-Only access.

Statistics
| Branch: | Revision:

main_repo / deps / v8 / test / cctest / test-threads.cc @ f230a1cf

History | View | Annotate | Download (5.91 KB)

1
// Copyright 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
#include "v8.h"
29

    
30
#include "platform.h"
31
#include "isolate.h"
32

    
33
#include "cctest.h"
34

    
35

    
36
TEST(Preemption) {
37
  v8::Isolate* isolate = CcTest::isolate();
38
  v8::Locker locker(isolate);
39
  v8::V8::Initialize();
40
  v8::HandleScope scope(isolate);
41
  v8::Handle<v8::Context> context = v8::Context::New(isolate);
42
  v8::Context::Scope context_scope(context);
43

    
44
  v8::Locker::StartPreemption(isolate, 100);
45

    
46
  v8::Handle<v8::Script> script = v8::Script::Compile(
47
      v8::String::New("var count = 0; var obj = new Object(); count++;\n"));
48

    
49
  script->Run();
50

    
51
  v8::Locker::StopPreemption(isolate);
52
  v8::internal::OS::Sleep(500);  // Make sure the timer fires.
53

    
54
  script->Run();
55
}
56

    
57

    
58
enum Turn {
59
  FILL_CACHE,
60
  CLEAN_CACHE,
61
  SECOND_TIME_FILL_CACHE,
62
  DONE
63
};
64

    
65
static Turn turn = FILL_CACHE;
66

    
67

    
68
class ThreadA : public v8::internal::Thread {
69
 public:
70
  ThreadA() : Thread("ThreadA") { }
71
  void Run() {
72
    v8::Isolate* isolate = CcTest::isolate();
73
    v8::Locker locker(isolate);
74
    v8::Isolate::Scope isolate_scope(isolate);
75
    v8::HandleScope scope(isolate);
76
    v8::Handle<v8::Context> context = v8::Context::New(isolate);
77
    v8::Context::Scope context_scope(context);
78

    
79
    CHECK_EQ(FILL_CACHE, turn);
80

    
81
    // Fill String.search cache.
82
    v8::Handle<v8::Script> script = v8::Script::Compile(
83
        v8::String::New(
84
          "for (var i = 0; i < 3; i++) {"
85
          "  var result = \"a\".search(\"a\");"
86
          "  if (result != 0) throw \"result: \" + result + \" @\" + i;"
87
          "};"
88
          "true"));
89
    CHECK(script->Run()->IsTrue());
90

    
91
    turn = CLEAN_CACHE;
92
    do {
93
      {
94
        v8::Unlocker unlocker(CcTest::isolate());
95
        Thread::YieldCPU();
96
      }
97
    } while (turn != SECOND_TIME_FILL_CACHE);
98

    
99
    // Rerun the script.
100
    CHECK(script->Run()->IsTrue());
101

    
102
    turn = DONE;
103
  }
104
};
105

    
106

    
107
class ThreadB : public v8::internal::Thread {
108
 public:
109
  ThreadB() : Thread("ThreadB") { }
110
  void Run() {
111
    do {
112
      {
113
        v8::Isolate* isolate = CcTest::isolate();
114
        v8::Locker locker(isolate);
115
        v8::Isolate::Scope isolate_scope(isolate);
116
        if (turn == CLEAN_CACHE) {
117
          v8::HandleScope scope(isolate);
118
          v8::Handle<v8::Context> context = v8::Context::New(isolate);
119
          v8::Context::Scope context_scope(context);
120

    
121
          // Clear the caches by forcing major GC.
122
          CcTest::heap()->CollectAllGarbage(v8::internal::Heap::kNoGCFlags);
123
          turn = SECOND_TIME_FILL_CACHE;
124
          break;
125
        }
126
      }
127

    
128
      Thread::YieldCPU();
129
    } while (true);
130
  }
131
};
132

    
133

    
134
TEST(JSFunctionResultCachesInTwoThreads) {
135
  ThreadA threadA;
136
  ThreadB threadB;
137

    
138
  threadA.Start();
139
  threadB.Start();
140

    
141
  threadA.Join();
142
  threadB.Join();
143

    
144
  CHECK_EQ(DONE, turn);
145
}
146

    
147
class ThreadIdValidationThread : public v8::internal::Thread {
148
 public:
149
  ThreadIdValidationThread(i::Thread* thread_to_start,
150
                           i::List<i::ThreadId>* refs,
151
                           unsigned int thread_no,
152
                           i::Semaphore* semaphore)
153
    : Thread("ThreadRefValidationThread"),
154
      refs_(refs), thread_no_(thread_no), thread_to_start_(thread_to_start),
155
      semaphore_(semaphore) {
156
  }
157

    
158
  void Run() {
159
    i::ThreadId thread_id = i::ThreadId::Current();
160
    for (int i = 0; i < thread_no_; i++) {
161
      CHECK(!(*refs_)[i].Equals(thread_id));
162
    }
163
    CHECK(thread_id.IsValid());
164
    (*refs_)[thread_no_] = thread_id;
165
    if (thread_to_start_ != NULL) {
166
      thread_to_start_->Start();
167
    }
168
    semaphore_->Signal();
169
  }
170

    
171
 private:
172
  i::List<i::ThreadId>* refs_;
173
  int thread_no_;
174
  i::Thread* thread_to_start_;
175
  i::Semaphore* semaphore_;
176
};
177

    
178

    
179
TEST(ThreadIdValidation) {
180
  const int kNThreads = 100;
181
  i::List<ThreadIdValidationThread*> threads(kNThreads);
182
  i::List<i::ThreadId> refs(kNThreads);
183
  i::Semaphore* semaphore = new i::Semaphore(0);
184
  ThreadIdValidationThread* prev = NULL;
185
  for (int i = kNThreads - 1; i >= 0; i--) {
186
    ThreadIdValidationThread* newThread =
187
        new ThreadIdValidationThread(prev, &refs, i, semaphore);
188
    threads.Add(newThread);
189
    prev = newThread;
190
    refs.Add(i::ThreadId::Invalid());
191
  }
192
  prev->Start();
193
  for (int i = 0; i < kNThreads; i++) {
194
    semaphore->Wait();
195
  }
196
  for (int i = 0; i < kNThreads; i++) {
197
    delete threads[i];
198
  }
199
}
200

    
201

    
202
class ThreadC : public v8::internal::Thread {
203
 public:
204
  ThreadC() : Thread("ThreadC") { }
205
  void Run() {
206
    Join();
207
  }
208
};
209

    
210

    
211
TEST(ThreadJoinSelf) {
212
  ThreadC thread;
213
  thread.Start();
214
  thread.Join();
215
}