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

Unified Diff: samples/shell.cc

Issue 6711068: Use v8::internal threading support in samples/shell.cc. (Closed)
Patch Set: Created 9 years, 9 months 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | src/platform.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: samples/shell.cc
diff --git a/samples/shell.cc b/samples/shell.cc
index d4aad9170cf67cfd6bd47ba94500308e61f2a6b3..3b2076accd58fadc6774aa6b4c838fc00b424f70 100644
--- a/samples/shell.cc
+++ b/samples/shell.cc
@@ -35,12 +35,7 @@
#include "../src/v8.h"
-// TODO(isolates):
-// o Either use V8 internal platform stuff for every platform or
-// re-implement it.
-// o Do not assume not WIN32 implies pthreads.
-#ifndef WIN32
-#include <pthread.h> // NOLINT
+#if !defined(_WIN32) && !defined(_WIN64)
#include <unistd.h> // NOLINT
#endif
@@ -67,10 +62,6 @@ v8::Handle<v8::String> ReadFile(const char* name);
void ReportException(v8::TryCatch* handler);
-#ifndef WIN32
-void* IsolateThreadEntry(void* arg);
-#endif
-
static bool last_run = true;
class SourceGroup {
@@ -78,14 +69,9 @@ class SourceGroup {
SourceGroup() : argv_(NULL),
begin_offset_(0),
end_offset_(0),
- next_semaphore_(NULL),
- done_semaphore_(NULL) {
-#ifndef WIN32
- next_semaphore_ = v8::internal::OS::CreateSemaphore(0);
- done_semaphore_ = v8::internal::OS::CreateSemaphore(0);
- thread_ = 0;
-#endif
- }
+ next_semaphore_(v8::internal::OS::CreateSemaphore(0)),
+ done_semaphore_(v8::internal::OS::CreateSemaphore(0)),
+ thread_(NULL) { }
void Begin(char** argv, int offset) {
argv_ = const_cast<const char**>(argv);
@@ -125,42 +111,49 @@ class SourceGroup {
}
}
-#ifdef WIN32
- void StartExecuteInThread() { ExecuteInThread(); }
- void WaitForThread() {}
-
-#else
void StartExecuteInThread() {
- if (thread_ == 0) {
- pthread_attr_t attr;
- // On some systems (OSX 10.6) the stack size default is 0.5Mb or less
- // which is not enough to parse the big literal expressions used in tests.
- // The stack size should be at least StackGuard::kLimitSize + some
- // OS-specific padding for thread startup code.
- size_t stacksize = 2 << 20; // 2 Mb seems to be enough
- pthread_attr_init(&attr);
- pthread_attr_setstacksize(&attr, stacksize);
- int error = pthread_create(&thread_, &attr, &IsolateThreadEntry, this);
- if (error != 0) {
- fprintf(stderr, "Error creating isolate thread.\n");
- ExitShell(1);
- }
+ if (thread_ == NULL) {
+ thread_ = new IsolateThread(this);
+ thread_->Start();
}
next_semaphore_->Signal();
}
void WaitForThread() {
- if (thread_ == 0) return;
+ if (thread_ == NULL) return;
if (last_run) {
- pthread_join(thread_, NULL);
- thread_ = 0;
+ thread_->Join();
+ thread_ = NULL;
} else {
done_semaphore_->Wait();
}
}
-#endif // WIN32
private:
+ static v8::internal::Thread::Options GetThreadOptions() {
+ v8::internal::Thread::Options options;
+ options.name = "IsolateThread";
+ // On some systems (OSX 10.6) the stack size default is 0.5Mb or less
+ // which is not enough to parse the big literal expressions used in tests.
+ // The stack size should be at least StackGuard::kLimitSize + some
+ // OS-specific padding for thread startup code.
+ options.stack_size = 2 << 20; // 2 Mb seems to be enough
+ return options;
+ }
+
+ class IsolateThread : public v8::internal::Thread {
+ public:
+ explicit IsolateThread(SourceGroup* group)
+ : group_(group), v8::internal::Thread(NULL, GetThreadOptions()) {}
+
+ virtual void Run() {
+ group_->ExecuteInThread();
+ }
+
+ private:
+ SourceGroup* group_;
+ };
+
void ExecuteInThread() {
v8::Isolate* isolate = v8::Isolate::New();
do {
@@ -185,20 +178,9 @@ class SourceGroup {
int end_offset_;
v8::internal::Semaphore* next_semaphore_;
v8::internal::Semaphore* done_semaphore_;
-#ifndef WIN32
- pthread_t thread_;
-#endif
-
- friend void* IsolateThreadEntry(void* arg);
+ v8::internal::Thread* thread_;
};
-#ifndef WIN32
-void* IsolateThreadEntry(void* arg) {
- reinterpret_cast<SourceGroup*>(arg)->ExecuteInThread();
- return NULL;
-}
-#endif
-
static SourceGroup* isolate_sources = NULL;
« no previous file with comments | « no previous file | src/platform.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698