Merge V8 at r7668: Initial merge by Git.
Change-Id: I1703c8b4f5c63052451a22cf3fb878abc9a0ec75
diff --git a/test/cctest/test-threads.cc b/test/cctest/test-threads.cc
index 37f0205..c6d5cb0 100644
--- a/test/cctest/test-threads.cc
+++ b/test/cctest/test-threads.cc
@@ -28,6 +28,7 @@
#include "v8.h"
#include "platform.h"
+#include "isolate.h"
#include "cctest.h"
@@ -136,3 +137,55 @@
CHECK_EQ(DONE, turn);
}
+
+class ThreadIdValidationThread : public v8::internal::Thread {
+ public:
+ ThreadIdValidationThread(i::Thread* thread_to_start,
+ i::List<i::ThreadId>* refs,
+ unsigned int thread_no,
+ i::Semaphore* semaphore)
+ : Thread(NULL, "ThreadRefValidationThread"),
+ refs_(refs), thread_no_(thread_no), thread_to_start_(thread_to_start),
+ semaphore_(semaphore) {
+ }
+
+ void Run() {
+ i::ThreadId thread_id = i::ThreadId::Current();
+ for (int i = 0; i < thread_no_; i++) {
+ CHECK(!(*refs_)[i].Equals(thread_id));
+ }
+ CHECK(thread_id.IsValid());
+ (*refs_)[thread_no_] = thread_id;
+ if (thread_to_start_ != NULL) {
+ thread_to_start_->Start();
+ }
+ semaphore_->Signal();
+ }
+ private:
+ i::List<i::ThreadId>* refs_;
+ int thread_no_;
+ i::Thread* thread_to_start_;
+ i::Semaphore* semaphore_;
+};
+
+TEST(ThreadIdValidation) {
+ const int kNThreads = 100;
+ i::List<ThreadIdValidationThread*> threads(kNThreads);
+ i::List<i::ThreadId> refs(kNThreads);
+ i::Semaphore* semaphore = i::OS::CreateSemaphore(0);
+ ThreadIdValidationThread* prev = NULL;
+ for (int i = kNThreads - 1; i >= 0; i--) {
+ ThreadIdValidationThread* newThread =
+ new ThreadIdValidationThread(prev, &refs, i, semaphore);
+ threads.Add(newThread);
+ prev = newThread;
+ refs.Add(i::ThreadId::Invalid());
+ }
+ prev->Start();
+ for (int i = 0; i < kNThreads; i++) {
+ semaphore->Wait();
+ }
+ for (int i = 0; i < kNThreads; i++) {
+ delete threads[i];
+ }
+}