OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 | 5 |
6 // Defined when linking against shared lib on Windows. | 6 // Defined when linking against shared lib on Windows. |
7 #if defined(USING_V8_SHARED) && !defined(V8_SHARED) | 7 #if defined(USING_V8_SHARED) && !defined(V8_SHARED) |
8 #define V8_SHARED | 8 #define V8_SHARED |
9 #endif | 9 #endif |
10 | 10 |
(...skipping 672 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
683 return; | 683 return; |
684 } | 684 } |
685 } | 685 } |
686 } | 686 } |
687 | 687 |
688 | 688 |
689 #ifndef V8_SHARED | 689 #ifndef V8_SHARED |
690 void Shell::WorkerNew(const v8::FunctionCallbackInfo<v8::Value>& args) { | 690 void Shell::WorkerNew(const v8::FunctionCallbackInfo<v8::Value>& args) { |
691 Isolate* isolate = args.GetIsolate(); | 691 Isolate* isolate = args.GetIsolate(); |
692 HandleScope handle_scope(isolate); | 692 HandleScope handle_scope(isolate); |
693 if (args.Length() < 1 || !args[0]->IsFunction()) { | 693 if (args.Length() < 1 || !args[0]->IsString()) { |
694 Throw(args.GetIsolate(), "1st argument must be function"); | 694 Throw(args.GetIsolate(), "1st argument must be string"); |
695 return; | 695 return; |
696 } | 696 } |
697 | 697 |
698 { | 698 { |
699 base::LockGuard<base::Mutex> lock_guard(&workers_mutex_); | 699 base::LockGuard<base::Mutex> lock_guard(&workers_mutex_); |
700 if (!allow_new_workers_) return; | 700 if (!allow_new_workers_) return; |
701 | 701 |
702 Worker* worker = new Worker; | 702 Worker* worker = new Worker; |
703 args.This()->SetInternalField(0, External::New(isolate, worker)); | 703 args.This()->SetInternalField(0, External::New(isolate, worker)); |
704 workers_.Add(worker); | 704 workers_.Add(worker); |
705 | 705 |
706 String::Utf8Value function_string(args[0]->ToString()); | 706 String::Utf8Value script(args[0]); |
707 if (!*function_string) { | 707 if (!*script) { |
708 Throw(args.GetIsolate(), "Function.prototype.toString failed"); | 708 Throw(args.GetIsolate(), "Can't get worker script"); |
709 return; | 709 return; |
710 } | 710 } |
711 worker->StartExecuteInThread(isolate, *function_string); | 711 worker->StartExecuteInThread(isolate, *script); |
712 } | 712 } |
713 } | 713 } |
714 | 714 |
715 | 715 |
716 void Shell::WorkerPostMessage(const v8::FunctionCallbackInfo<v8::Value>& args) { | 716 void Shell::WorkerPostMessage(const v8::FunctionCallbackInfo<v8::Value>& args) { |
717 Isolate* isolate = args.GetIsolate(); | 717 Isolate* isolate = args.GetIsolate(); |
718 HandleScope handle_scope(isolate); | 718 HandleScope handle_scope(isolate); |
719 Local<Context> context = isolate->GetCurrentContext(); | 719 Local<Context> context = isolate->GetCurrentContext(); |
720 | 720 |
721 if (args.Length() < 1) { | 721 if (args.Length() < 1) { |
(...skipping 912 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1634 : in_semaphore_(0), | 1634 : in_semaphore_(0), |
1635 out_semaphore_(0), | 1635 out_semaphore_(0), |
1636 thread_(NULL), | 1636 thread_(NULL), |
1637 script_(NULL), | 1637 script_(NULL), |
1638 state_(IDLE) {} | 1638 state_(IDLE) {} |
1639 | 1639 |
1640 | 1640 |
1641 Worker::~Worker() { Cleanup(); } | 1641 Worker::~Worker() { Cleanup(); } |
1642 | 1642 |
1643 | 1643 |
1644 void Worker::StartExecuteInThread(Isolate* isolate, | 1644 void Worker::StartExecuteInThread(Isolate* isolate, const char* script) { |
1645 const char* function_string) { | 1645 if (base::NoBarrier_CompareAndSwap(&state_, IDLE, RUNNING) == IDLE) { |
1646 DCHECK(base::NoBarrier_Load(&state_) == IDLE); | 1646 script_ = i::StrDup(script); |
1647 static const char format[] = "(%s).call(this);"; | 1647 thread_ = new WorkerThread(this); |
1648 size_t len = strlen(function_string) + sizeof(format); | 1648 thread_->Start(); |
1649 | 1649 } else { |
1650 script_ = new char[len + 1]; | 1650 // Somehow the Worker was started twice. |
1651 i::Vector<char> vec(script_, static_cast<int>(len + 1)); | 1651 UNREACHABLE(); |
1652 i::SNPrintF(vec, format, function_string); | 1652 } |
1653 | |
1654 base::NoBarrier_Store(&state_, RUNNING); | |
1655 thread_ = new WorkerThread(this); | |
1656 thread_->Start(); | |
1657 } | 1653 } |
1658 | 1654 |
1659 | 1655 |
1660 void Worker::PostMessage(SerializationData* data) { | 1656 void Worker::PostMessage(SerializationData* data) { |
1661 in_queue_.Enqueue(data); | 1657 in_queue_.Enqueue(data); |
1662 in_semaphore_.Signal(); | 1658 in_semaphore_.Signal(); |
1663 } | 1659 } |
1664 | 1660 |
1665 | 1661 |
1666 SerializationData* Worker::GetMessage() { | 1662 SerializationData* Worker::GetMessage() { |
(...skipping 767 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2434 } | 2430 } |
2435 | 2431 |
2436 } // namespace v8 | 2432 } // namespace v8 |
2437 | 2433 |
2438 | 2434 |
2439 #ifndef GOOGLE3 | 2435 #ifndef GOOGLE3 |
2440 int main(int argc, char* argv[]) { | 2436 int main(int argc, char* argv[]) { |
2441 return v8::Shell::Main(argc, argv); | 2437 return v8::Shell::Main(argc, argv); |
2442 } | 2438 } |
2443 #endif | 2439 #endif |
OLD | NEW |