Use v8::internal threading support in samples/shell.cc.

We need this for isolates testing. To make it work I had to extend the
internal Thread constructor with an option to set the stack size (see
the comment in shell.cc).

BUG=1264

Review URL: http://codereview.chromium.org/6711068

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@7289 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
This commit is contained in:
vitalyr@chromium.org 2011-03-21 15:04:17 +00:00
parent 93d4776d46
commit 04537cb227
17 changed files with 186 additions and 122 deletions

View File

@ -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;

View File

@ -400,12 +400,18 @@ bool ThreadHandle::IsValid() const {
}
Thread::Thread() : ThreadHandle(ThreadHandle::INVALID) {
set_name("v8:<unknown>");
Thread::Thread(Isolate* isolate, const Options& options)
: ThreadHandle(ThreadHandle::INVALID),
isolate_(isolate),
stack_size_(options.stack_size) {
set_name(options.name);
}
Thread::Thread(const char* name) : ThreadHandle(ThreadHandle::INVALID) {
Thread::Thread(Isolate* isolate, const char* name)
: ThreadHandle(ThreadHandle::INVALID),
isolate_(isolate),
stack_size_(0) {
set_name(name);
}

View File

@ -425,15 +425,18 @@ bool ThreadHandle::IsValid() const {
}
Thread::Thread(Isolate* isolate)
Thread::Thread(Isolate* isolate, const Options& options)
: ThreadHandle(ThreadHandle::INVALID),
isolate_(isolate) {
set_name("v8:<unknown>");
isolate_(isolate),
stack_size_(options.stack_size) {
set_name(options.name);
}
Thread::Thread(Isolate* isolate, const char* name)
: ThreadHandle(ThreadHandle::INVALID),
isolate_(isolate) {
isolate_(isolate),
stack_size_(0) {
set_name(name);
}
@ -462,7 +465,14 @@ void Thread::set_name(const char* name) {
void Thread::Start() {
pthread_create(&thread_handle_data()->thread_, NULL, ThreadEntry, this);
pthread_attr_t* attr_ptr = NULL;
pthread_attr_t attr;
if (stack_size_ > 0) {
pthread_attr_init(&attr);
pthread_attr_setstacksize(&attr, static_cast<size_t>(stack_size_));
attr_ptr = &attr;
}
pthread_create(&thread_handle_data()->thread_, attr_ptr, ThreadEntry, this);
ASSERT(IsValid());
}

View File

@ -573,16 +573,18 @@ bool ThreadHandle::IsValid() const {
}
Thread::Thread(Isolate* isolate)
Thread::Thread(Isolate* isolate, const Options& options)
: ThreadHandle(ThreadHandle::INVALID),
isolate_(isolate) {
set_name("v8:<unknown>");
isolate_(isolate),
stack_size_(options.stack_size) {
set_name(options.name);
}
Thread::Thread(Isolate* isolate, const char* name)
: ThreadHandle(ThreadHandle::INVALID),
isolate_(isolate) {
isolate_(isolate),
stack_size_(0) {
set_name(name);
}
@ -614,7 +616,14 @@ void Thread::set_name(const char* name) {
void Thread::Start() {
pthread_create(&thread_handle_data()->thread_, NULL, ThreadEntry, this);
pthread_attr_t* attr_ptr = NULL;
pthread_attr_t attr;
if (stack_size_ > 0) {
pthread_attr_init(&attr);
pthread_attr_setstacksize(&attr, static_cast<size_t>(stack_size_));
attr_ptr = &attr;
}
pthread_create(&thread_handle_data()->thread_, attr_ptr, ThreadEntry, this);
ASSERT(IsValid());
}
@ -875,7 +884,9 @@ class SignalSender : public Thread {
};
explicit SignalSender(int interval)
: Thread(NULL), vm_tgid_(getpid()), interval_(interval) {}
: Thread(NULL, "SignalSender"),
vm_tgid_(getpid()),
interval_(interval) {}
static void AddActiveSampler(Sampler* sampler) {
ScopedLock lock(mutex_);

View File

@ -432,16 +432,18 @@ bool ThreadHandle::IsValid() const {
}
Thread::Thread(Isolate* isolate)
Thread::Thread(Isolate* isolate, const Options& options)
: ThreadHandle(ThreadHandle::INVALID),
isolate_(isolate) {
set_name("v8:<unknown>");
isolate_(isolate),
stack_size_(options.stack_size) {
set_name(options.name);
}
Thread::Thread(Isolate* isolate, const char* name)
: ThreadHandle(ThreadHandle::INVALID),
isolate_(isolate) {
isolate_(isolate),
stack_size_(0) {
set_name(name);
}
@ -450,7 +452,6 @@ Thread::~Thread() {
}
static void SetThreadName(const char* name) {
// pthread_setname_np is only available in 10.6 or later, so test
// for it at runtime.
@ -489,7 +490,15 @@ void Thread::set_name(const char* name) {
void Thread::Start() {
pthread_create(&thread_handle_data()->thread_, NULL, ThreadEntry, this);
pthread_attr_t* attr_ptr = NULL;
pthread_attr_t attr;
if (stack_size_ > 0) {
pthread_attr_init(&attr);
pthread_attr_setstacksize(&attr, static_cast<size_t>(stack_size_));
attr_ptr = &attr;
}
pthread_create(&thread_handle_data()->thread_, attr_ptr, ThreadEntry, this);
ASSERT(IsValid());
}
@ -626,7 +635,9 @@ class Sampler::PlatformData : public Malloced {
class SamplerThread : public Thread {
public:
explicit SamplerThread(int interval) : Thread(NULL), interval_(interval) {}
explicit SamplerThread(int interval)
: Thread(NULL, "SamplerThread"),
interval_(interval) {}
static void AddActiveSampler(Sampler* sampler) {
ScopedLock lock(mutex_);

View File

@ -340,17 +340,19 @@ bool ThreadHandle::IsValid() const {
}
Thread::Thread(Isolate* isolate)
Thread::Thread(Isolate* isolate, const Options& options)
: ThreadHandle(ThreadHandle::INVALID),
isolate_(isolate) {
set_name("v8:<unknown>");
isolate_(isolate),
stack_size_(options.stack_size) {
set_name(options.name);
UNIMPLEMENTED();
}
Thread::Thread(Isolate* isolate, const char* name)
: ThreadHandle(ThreadHandle::INVALID),
isolate_(isolate) {
isolate_(isolate),
stack_size_(0) {
set_name(name);
UNIMPLEMENTED();
}

View File

@ -400,16 +400,18 @@ bool ThreadHandle::IsValid() const {
}
Thread::Thread(Isolate* isolate)
Thread::Thread(Isolate* isolate, const Options& options)
: ThreadHandle(ThreadHandle::INVALID),
isolate_(isolate) {
set_name("v8:<unknown>");
isolate_(isolate),
stack_size_(options.stack_size) {
set_name(options.name);
}
Thread::Thread(Isolate* isolate, const char* name)
: ThreadHandle(ThreadHandle::INVALID),
isolate_(isolate) {
isolate_(isolate),
stack_size_(0) {
set_name(name);
}
@ -438,7 +440,14 @@ void Thread::set_name(const char* name) {
void Thread::Start() {
pthread_create(&thread_handle_data()->thread_, NULL, ThreadEntry, this);
pthread_attr_t* attr_ptr = NULL;
pthread_attr_t attr;
if (stack_size_ > 0) {
pthread_attr_init(&attr);
pthread_attr_setstacksize(&attr, static_cast<size_t>(stack_size_));
attr_ptr = &attr;
}
pthread_create(&thread_handle_data()->thread_, attr_ptr, ThreadEntry, this);
ASSERT(IsValid());
}

View File

@ -415,16 +415,18 @@ bool ThreadHandle::IsValid() const {
}
Thread::Thread(Isolate* isolate)
Thread::Thread(Isolate* isolate, const Options& options)
: ThreadHandle(ThreadHandle::INVALID),
isolate_(isolate) {
set_name("v8:<unknown>");
isolate_(isolate),
stack_size_(options.stack_size) {
set_name(options.name);
}
Thread::Thread(Isolate* isolate, const char* name)
: ThreadHandle(ThreadHandle::INVALID),
isolate_(isolate) {
isolate_(isolate),
stack_size_(0) {
set_name(name);
}
@ -453,6 +455,13 @@ void Thread::set_name(const char* name) {
void Thread::Start() {
pthread_attr_t* attr_ptr = NULL;
pthread_attr_t attr;
if (stack_size_ > 0) {
pthread_attr_init(&attr);
pthread_attr_setstacksize(&attr, static_cast<size_t>(stack_size_));
attr_ptr = &attr;
}
pthread_create(&thread_handle_data()->thread_, NULL, ThreadEntry, this);
ASSERT(IsValid());
}

View File

@ -1503,17 +1503,19 @@ class Thread::PlatformData : public Malloced {
// Initialize a Win32 thread object. The thread has an invalid thread
// handle until it is started.
Thread::Thread(Isolate* isolate)
Thread::Thread(Isolate* isolate, const Options& options)
: ThreadHandle(ThreadHandle::INVALID),
isolate_(isolate) {
isolate_(isolate),
stack_size_(options.stack_size) {
data_ = new PlatformData(kNoThread);
set_name("v8:<unknown>");
set_name(options.name);
}
Thread::Thread(Isolate* isolate, const char* name)
: ThreadHandle(ThreadHandle::INVALID),
isolate_(isolate) {
isolate_(isolate),
stack_size_(0) {
data_ = new PlatformData(kNoThread);
set_name(name);
}
@ -1538,7 +1540,7 @@ Thread::~Thread() {
void Thread::Start() {
data_->thread_ = reinterpret_cast<HANDLE>(
_beginthreadex(NULL,
0,
static_cast<unsigned>(stack_size_),
ThreadEntry,
this,
0,
@ -1884,7 +1886,9 @@ class Sampler::PlatformData : public Malloced {
class SamplerThread : public Thread {
public:
explicit SamplerThread(int interval) : Thread(NULL), interval_(interval) {}
explicit SamplerThread(int interval)
: Thread(NULL, "SamplerThread"),
interval_(interval) {}
static void AddActiveSampler(Sampler* sampler) {
ScopedLock lock(mutex_);

View File

@ -388,8 +388,15 @@ class Thread: public ThreadHandle {
LOCAL_STORAGE_KEY_MAX_VALUE = kMaxInt
};
struct Options {
Options() : name("v8:<unknown>"), stack_size(0) {}
const char* name;
int stack_size;
};
// Create new thread (with a value for storing in the TLS isolate field).
explicit Thread(Isolate* isolate);
Thread(Isolate* isolate, const Options& options);
Thread(Isolate* isolate, const char* name);
virtual ~Thread();
@ -436,6 +443,7 @@ class Thread: public ThreadHandle {
PlatformData* data_;
Isolate* isolate_;
char name_[kMaxThreadNameLength];
int stack_size_;
DISALLOW_COPY_AND_ASSIGN(Thread);
};

View File

@ -88,7 +88,7 @@ class ApiTestFuzzer: public v8::internal::Thread {
public:
void CallTest();
explicit ApiTestFuzzer(v8::internal::Isolate* isolate, int num)
: Thread(isolate),
: Thread(isolate, "ApiTestFuzzer"),
test_number_(num),
gate_(v8::internal::OS::CreateSemaphore(0)),
active_(true) {

View File

@ -9929,7 +9929,7 @@ class RegExpInterruptTest {
class GCThread : public i::Thread {
public:
explicit GCThread(i::Isolate* isolate, RegExpInterruptTest* test)
: Thread(isolate), test_(test) {}
: Thread(isolate, "GCThread"), test_(test) {}
virtual void Run() {
test_->CollectGarbage();
}
@ -10051,7 +10051,7 @@ class ApplyInterruptTest {
class GCThread : public i::Thread {
public:
explicit GCThread(i::Isolate* isolate, ApplyInterruptTest* test)
: Thread(isolate), test_(test) {}
: Thread(isolate, "GCThread"), test_(test) {}
virtual void Run() {
test_->CollectGarbage();
}
@ -10347,7 +10347,7 @@ class RegExpStringModificationTest {
public:
explicit MorphThread(i::Isolate* isolate,
RegExpStringModificationTest* test)
: Thread(isolate), test_(test) {}
: Thread(isolate, "MorphThread"), test_(test) {}
virtual void Run() {
test_->MorphString();
}
@ -13044,10 +13044,10 @@ static int CalcFibonacci(v8::Isolate* isolate, int limit) {
class IsolateThread : public v8::internal::Thread {
public:
explicit IsolateThread(v8::Isolate* isolate, int fib_limit)
: Thread(NULL),
isolate_(isolate),
fib_limit_(fib_limit),
result_(0) { }
: Thread(NULL, "IsolateThread"),
isolate_(isolate),
fib_limit_(fib_limit),
result_(0) { }
void Run() {
result_ = CalcFibonacci(isolate_, fib_limit_);
@ -13095,9 +13095,9 @@ class InitDefaultIsolateThread : public v8::internal::Thread {
enum TestCase { IgnoreOOM, SetResourceConstraints, SetFatalHandler };
explicit InitDefaultIsolateThread(TestCase testCase)
: Thread(NULL),
testCase_(testCase),
result_(false) { }
: Thread(NULL, "InitDefaultIsolateThread"),
testCase_(testCase),
result_(false) { }
void Run() {
switch (testCase_) {

View File

@ -89,7 +89,7 @@ class ProducerThread: public i::Thread {
int records_per_chunk,
Record value,
i::Semaphore* finished)
: Thread(isolate),
: Thread(isolate, "producer"),
scq_(scq),
records_per_chunk_(records_per_chunk),
value_(value),

View File

@ -4721,7 +4721,7 @@ Barriers message_queue_barriers;
class MessageQueueDebuggerThread : public v8::internal::Thread {
public:
explicit MessageQueueDebuggerThread(v8::internal::Isolate* isolate)
: Thread(isolate) { }
: Thread(isolate, "MessageQueueDebuggerThread") { }
void Run();
};
@ -4972,13 +4972,15 @@ Barriers threaded_debugging_barriers;
class V8Thread : public v8::internal::Thread {
public:
explicit V8Thread(v8::internal::Isolate* isolate) : Thread(isolate) { }
explicit V8Thread(v8::internal::Isolate* isolate)
: Thread(isolate, "V8Thread") { }
void Run();
};
class DebuggerThread : public v8::internal::Thread {
public:
explicit DebuggerThread(v8::internal::Isolate* isolate) : Thread(isolate) { }
explicit DebuggerThread(v8::internal::Isolate* isolate)
: Thread(isolate, "DebuggerThread") { }
void Run();
};
@ -5078,7 +5080,7 @@ TEST(ThreadedDebugging) {
class BreakpointsV8Thread : public v8::internal::Thread {
public:
explicit BreakpointsV8Thread(v8::internal::Isolate* isolate)
: Thread(isolate) { }
: Thread(isolate, "BreakpointsV8Thread") { }
void Run();
};
@ -5086,7 +5088,8 @@ class BreakpointsDebuggerThread : public v8::internal::Thread {
public:
explicit BreakpointsDebuggerThread(v8::internal::Isolate* isolate,
bool global_evaluate)
: Thread(isolate), global_evaluate_(global_evaluate) {}
: Thread(isolate, "BreakpointsDebuggerThread"),
global_evaluate_(global_evaluate) {}
void Run();
private:
@ -5647,14 +5650,14 @@ TEST(DebuggerClearMessageHandlerWhileActive) {
class HostDispatchV8Thread : public v8::internal::Thread {
public:
explicit HostDispatchV8Thread(v8::internal::Isolate* isolate)
: Thread(isolate) { }
: Thread(isolate, "HostDispatchV8Thread") { }
void Run();
};
class HostDispatchDebuggerThread : public v8::internal::Thread {
public:
explicit HostDispatchDebuggerThread(v8::internal::Isolate* isolate)
: Thread(isolate) { }
: Thread(isolate, "HostDispatchDebuggerThread") { }
void Run();
};
@ -5753,14 +5756,14 @@ TEST(DebuggerHostDispatch) {
class DebugMessageDispatchV8Thread : public v8::internal::Thread {
public:
explicit DebugMessageDispatchV8Thread(v8::internal::Isolate* isolate)
: Thread(isolate) { }
: Thread(isolate, "DebugMessageDispatchV8Thread") { }
void Run();
};
class DebugMessageDispatchDebuggerThread : public v8::internal::Thread {
public:
explicit DebugMessageDispatchDebuggerThread(v8::internal::Isolate* isolate)
: Thread(isolate) { }
: Thread(isolate, "DebugMessageDispatchDebuggerThread") { }
void Run();
};
@ -5863,7 +5866,10 @@ TEST(DebuggerAgent) {
class DebuggerAgentProtocolServerThread : public i::Thread {
public:
explicit DebuggerAgentProtocolServerThread(i::Isolate* isolate, int port)
: Thread(isolate), port_(port), server_(NULL), client_(NULL),
: Thread(isolate, "DebuggerAgentProtocolServerThread"),
port_(port),
server_(NULL),
client_(NULL),
listening_(OS::CreateSemaphore(0)) {
}
~DebuggerAgentProtocolServerThread() {

View File

@ -11,8 +11,12 @@ using namespace ::v8::internal;
class SocketListenerThread : public Thread {
public:
explicit SocketListenerThread(Isolate* isolate, int port, int data_size)
: Thread(isolate), port_(port), data_size_(data_size), server_(NULL),
client_(NULL), listening_(OS::CreateSemaphore(0)) {
: Thread(isolate, "SocketListenerThread"),
port_(port),
data_size_(data_size),
server_(NULL),
client_(NULL),
listening_(OS::CreateSemaphore(0)) {
data_ = new char[data_size_];
}
~SocketListenerThread() {

View File

@ -160,7 +160,8 @@ TEST(TerminateOnlyV8ThreadFromThreadItselfNoLoop) {
class TerminatorThread : public v8::internal::Thread {
public:
explicit TerminatorThread(i::Isolate* isolate) : Thread(isolate) { }
explicit TerminatorThread(i::Isolate* isolate)
: Thread(isolate, "TerminatorThread") { }
void Run() {
semaphore->Wait();
CHECK(!v8::V8::IsExecutionTerminating());
@ -195,7 +196,8 @@ TEST(TerminateOnlyV8ThreadFromOtherThread) {
class LoopingThread : public v8::internal::Thread {
public:
explicit LoopingThread(i::Isolate* isolate) : Thread(isolate) { }
explicit LoopingThread(i::Isolate* isolate)
: Thread(isolate, "LoopingThread") { }
void Run() {
v8::Locker locker;
v8::HandleScope scope;

View File

@ -64,7 +64,7 @@ static Turn turn = FILL_CACHE;
class ThreadA: public v8::internal::Thread {
public:
explicit ThreadA(i::Isolate* isolate) : Thread(isolate) { }
explicit ThreadA(i::Isolate* isolate) : Thread(isolate, "ThreadA") { }
void Run() {
v8::Locker locker;
v8::HandleScope scope;
@ -100,7 +100,7 @@ class ThreadA: public v8::internal::Thread {
class ThreadB: public v8::internal::Thread {
public:
explicit ThreadB(i::Isolate* isolate) : Thread(isolate) { }
explicit ThreadB(i::Isolate* isolate) : Thread(isolate, "ThreadB") { }
void Run() {
do {
{