#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
void ReportException(v8::TryCatch* handler);
-#ifndef WIN32
-void* IsolateThreadEntry(void* arg);
-#endif
-
static bool last_run = true;
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);
}
}
-#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 {
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;
}
-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);
}
}
-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);
}
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());
}
}
-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);
}
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());
}
};
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_);
}
-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);
}
}
-
static void SetThreadName(const char* name) {
// pthread_setname_np is only available in 10.6 or later, so test
// for it at runtime.
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());
}
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_);
}
-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();
}
}
-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);
}
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());
}
}
-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);
}
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());
}
// 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);
}
void Thread::Start() {
data_->thread_ = reinterpret_cast<HANDLE>(
_beginthreadex(NULL,
- 0,
+ static_cast<unsigned>(stack_size_),
ThreadEntry,
this,
0,
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_);
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();
PlatformData* data_;
Isolate* isolate_;
char name_[kMaxThreadNameLength];
+ int stack_size_;
DISALLOW_COPY_AND_ASSIGN(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) {
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();
}
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();
}
public:
explicit MorphThread(i::Isolate* isolate,
RegExpStringModificationTest* test)
- : Thread(isolate), test_(test) {}
+ : Thread(isolate, "MorphThread"), test_(test) {}
virtual void Run() {
test_->MorphString();
}
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_);
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_) {
int records_per_chunk,
Record value,
i::Semaphore* finished)
- : Thread(isolate),
+ : Thread(isolate, "producer"),
scq_(scq),
records_per_chunk_(records_per_chunk),
value_(value),
class MessageQueueDebuggerThread : public v8::internal::Thread {
public:
explicit MessageQueueDebuggerThread(v8::internal::Isolate* isolate)
- : Thread(isolate) { }
+ : Thread(isolate, "MessageQueueDebuggerThread") { }
void Run();
};
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();
};
class BreakpointsV8Thread : public v8::internal::Thread {
public:
explicit BreakpointsV8Thread(v8::internal::Isolate* isolate)
- : Thread(isolate) { }
+ : Thread(isolate, "BreakpointsV8Thread") { }
void Run();
};
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:
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();
};
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();
};
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() {
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() {
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());
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;
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;
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 {
{