34 using namespace ::v8::internal;
37 TEST(WaitForAfterNofityOnSameThread) {
38 for (
int n = 0; n < 10; ++n) {
42 LockGuard<Mutex> lock_guard(&mutex);
45 CHECK_EQ(
false, cv.WaitFor(&mutex, TimeDelta::FromMicroseconds(n)));
48 CHECK_EQ(
false, cv.WaitFor(&mutex, TimeDelta::FromMicroseconds(n)));
53 class ThreadWithMutexAndConditionVariable
V8_FINAL :
public Thread {
56 : Thread(
"ThreadWithMutexAndConditionVariable"),
61 LockGuard<Mutex> lock_guard(&mutex_);
73 ConditionVariable
cv_;
78 TEST(MultipleThreadsWithSeparateConditionVariables) {
79 static const int kThreadCount = 128;
80 ThreadWithMutexAndConditionVariable threads[kThreadCount];
82 for (
int n = 0; n < kThreadCount; ++n) {
83 LockGuard<Mutex> lock_guard(&threads[n].mutex_);
84 CHECK(!threads[n].running_);
85 CHECK(!threads[n].finished_);
88 while (!threads[n].running_) {
89 threads[n].cv_.Wait(&threads[n].mutex_);
93 for (
int n = kThreadCount - 1; n >= 0; --n) {
94 LockGuard<Mutex> lock_guard(&threads[n].mutex_);
95 CHECK(threads[n].running_);
96 CHECK(!threads[n].finished_);
99 for (
int n = 0; n < kThreadCount; ++n) {
100 LockGuard<Mutex> lock_guard(&threads[n].mutex_);
101 CHECK(threads[n].running_);
102 CHECK(!threads[n].finished_);
104 threads[n].running_ =
false;
105 threads[n].cv_.NotifyOne();
108 for (
int n = kThreadCount - 1; n >= 0; --n) {
110 LockGuard<Mutex> lock_guard(&threads[n].mutex_);
111 while (!threads[n].finished_) {
112 threads[n].cv_.Wait(&threads[n].mutex_);
114 CHECK(!threads[n].running_);
115 CHECK(threads[n].finished_);
118 for (
int n = 0; n < kThreadCount; ++n) {
120 LockGuard<Mutex> lock_guard(&threads[n].mutex_);
121 CHECK(!threads[n].running_);
122 CHECK(threads[n].finished_);
127 class ThreadWithSharedMutexAndConditionVariable
V8_FINAL :
public Thread {
130 : Thread(
"ThreadWithSharedMutexAndConditionVariable"),
135 LockGuard<Mutex> lock_guard(mutex_);
152 TEST(MultipleThreadsWithSharedSeparateConditionVariables) {
153 static const int kThreadCount = 128;
154 ThreadWithSharedMutexAndConditionVariable threads[kThreadCount];
155 ConditionVariable cv;
158 for (
int n = 0; n < kThreadCount; ++n) {
159 threads[n].mutex_ = &mutex;
160 threads[n].cv_ = &cv;
165 LockGuard<Mutex> lock_guard(&mutex);
166 for (
int n = 0; n < kThreadCount; ++n) {
167 CHECK(!threads[n].running_);
168 CHECK(!threads[n].finished_);
175 LockGuard<Mutex> lock_guard(&mutex);
176 for (
int n = kThreadCount - 1; n >= 0; --n) {
177 while (!threads[n].running_) {
185 LockGuard<Mutex> lock_guard(&mutex);
186 for (
int n = 0; n < kThreadCount; ++n) {
187 CHECK(threads[n].running_);
188 CHECK(!threads[n].finished_);
194 LockGuard<Mutex> lock_guard(&mutex);
195 for (
int n = kThreadCount - 1; n >= 0; --n) {
196 CHECK(threads[n].running_);
197 CHECK(!threads[n].finished_);
199 threads[n].running_ =
false;
206 LockGuard<Mutex> lock_guard(&mutex);
207 for (
int n = 0; n < kThreadCount; ++n) {
208 while (!threads[n].finished_) {
216 LockGuard<Mutex> lock_guard(&mutex);
217 for (
int n = kThreadCount - 1; n >= 0; --n) {
218 CHECK(!threads[n].running_);
219 CHECK(threads[n].finished_);
224 for (
int n = 0; n < kThreadCount; ++n) {
230 class LoopIncrementThread
V8_FINAL :
public Thread {
236 ConditionVariable* cv,
238 : Thread(
"LoopIncrementThread"), rem_(rem), counter_(counter),
239 limit_(limit), thread_count_(thread_count), cv_(cv), mutex_(mutex) {
247 LockGuard<Mutex> lock_guard(mutex_);
248 int count = *counter_;
249 while (count % thread_count_ != rem_ && count < limit_) {
253 if (count >= limit_)
break;
255 if (last_count != -1) {
256 CHECK_EQ(last_count + (thread_count_ - 1), count);
269 const int thread_count_;
270 ConditionVariable* cv_;
276 static const int kMaxThreadCount = 16;
278 ConditionVariable cv;
279 for (
int thread_count = 1; thread_count < kMaxThreadCount; ++thread_count) {
280 int limit = thread_count * 100;
284 Thread** threads =
new Thread*[thread_count];
285 for (
int n = 0; n < thread_count; ++n) {
286 threads[n] =
new LoopIncrementThread(
287 n, &counter, limit, thread_count, &cv, &mutex);
291 for (
int n = thread_count - 1; n >= 0; --n) {
296 for (
int n = 0; n < thread_count; ++n) {
enable upcoming ES6 features enable harmony block scoping enable harmony enable harmony proxies enable harmony generators enable harmony numeric enable harmony string enable harmony math functions harmony_scoping harmony_symbols harmony_collections harmony_iteration harmony_strings harmony_scoping harmony_maths tracks arrays with only smi values Optimize object Array DOM strings and string pretenure call new trace pretenuring decisions of HAllocate instructions track fields with only smi values track fields with heap values track_fields track_fields Enables optimizations which favor memory size over execution speed use string slices optimization filter maximum number of GVN fix point iterations use function inlining use allocation folding eliminate write barriers targeting allocations in optimized code maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining crankshaft harvests type feedback from stub cache trace check elimination phase hydrogen tracing filter NULL
#define CHECK_EQ(expected, value)
TEST(WaitForAfterNofityOnSameThread)
ThreadWithSharedMutexAndConditionVariable()
ThreadWithMutexAndConditionVariable()
virtual ~ThreadWithMutexAndConditionVariable()
virtual ~ThreadWithSharedMutexAndConditionVariable()
LoopIncrementThread(int rem, int *counter, int limit, int thread_count, ConditionVariable *cv, Mutex *mutex)
virtual void Run() V8_OVERRIDE