Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(220)

Side by Side Diff: test/cctest/test-libplatform.h

Issue 104583003: [platform] Implement a worker pool (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: updates Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2013 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 #ifndef TEST_LIBPLATFORM_H_
29 #define TEST_LIBPLATFORM_H_
30
31 #include "v8.h"
32
33 #include "cctest.h"
34
35 using namespace v8::internal;
36
37 class TaskCounter {
38 public:
39 TaskCounter() : counter_(0) {}
40 ~TaskCounter() { CHECK_EQ(0, counter_); }
41
42 int GetCount() const {
43 LockGuard<Mutex> guard(&lock_);
44 return counter_;
45 }
46
47 void Inc() {
48 LockGuard<Mutex> guard(&lock_);
49 ++counter_;
50 }
51
52 void Dec() {
53 LockGuard<Mutex> guard(&lock_);
54 --counter_;
55 }
56
57 private:
58 mutable Mutex lock_;
59 int counter_;
60
61 DISALLOW_COPY_AND_ASSIGN(TaskCounter);
62 };
63
Hannes Payer (out of office) 2013/12/11 12:07:27 Can we have a newline here?
64 class TestTask : public v8::Task {
65 public:
66 TestTask(TaskCounter* task_counter, bool expected_to_run)
67 : task_counter_(task_counter),
68 expected_to_run_(expected_to_run),
69 executed_(false) {
70 task_counter_->Inc();
71 }
Hannes Payer (out of office) 2013/12/11 12:07:27 Can we have a newline here?
72 explicit TestTask(TaskCounter* task_counter)
73 : task_counter_(task_counter), expected_to_run_(false), executed_(false) {
74 task_counter_->Inc();
75 }
Hannes Payer (out of office) 2013/12/11 12:07:27 Can we have a newline here?
76 virtual ~TestTask() {
77 CHECK_EQ(expected_to_run_, executed_);
78 task_counter_->Dec();
79 }
80
81 // v8::Task implementation.
82 virtual void Run() V8_OVERRIDE { executed_ = true; }
83
84 private:
85 TaskCounter* task_counter_;
86 bool expected_to_run_;
87 bool executed_;
88
89 DISALLOW_COPY_AND_ASSIGN(TestTask);
90 };
91
92
93 class TestWorkerThread : public Thread {
94 public:
95 explicit TestWorkerThread(v8::Task* task)
96 : Thread("libplatform TestWorkerThread"), semaphore_(0), task_(task) {}
97 virtual ~TestWorkerThread() {}
98
99 void Signal() { semaphore_.Signal(); }
100
101 // Thread implementation.
102 virtual void Run() V8_OVERRIDE {
103 semaphore_.Wait();
104 if (task_) {
105 task_->Run();
106 delete task_;
107 }
108 }
109
110 private:
111 Semaphore semaphore_;
112 v8::Task* task_;
113
114 DISALLOW_COPY_AND_ASSIGN(TestWorkerThread);
115 };
116
117 #endif // TEST_LIBPLATFORM_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698