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

Side by Side Diff: runtime/vm/dart_api_impl.cc

Issue 9924015: Use the ThreadPool for all isolates and native ports. Previously, (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 8 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/vm/dart.cc ('k') | runtime/vm/isolate.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "include/dart_api.h" 5 #include "include/dart_api.h"
6 6
7 #include "vm/bigint_operations.h" 7 #include "vm/bigint_operations.h"
8 #include "vm/class_finalizer.h" 8 #include "vm/class_finalizer.h"
9 #include "vm/compiler.h" 9 #include "vm/compiler.h"
10 #include "vm/dart.h" 10 #include "vm/dart.h"
(...skipping 750 matching lines...) Expand 10 before | Expand all | Expand 10 after
761 761
762 762
763 DART_EXPORT void Dart_SetMessageNotifyCallback( 763 DART_EXPORT void Dart_SetMessageNotifyCallback(
764 Dart_MessageNotifyCallback message_notify_callback) { 764 Dart_MessageNotifyCallback message_notify_callback) {
765 Isolate* isolate = Isolate::Current(); 765 Isolate* isolate = Isolate::Current();
766 CHECK_ISOLATE(isolate); 766 CHECK_ISOLATE(isolate);
767 isolate->set_message_notify_callback(message_notify_callback); 767 isolate->set_message_notify_callback(message_notify_callback);
768 } 768 }
769 769
770 770
771 struct RunLoopData {
772 Monitor* monitor;
773 bool done;
774 };
775
776
777 static void RunLoopDone(uword param) {
778 RunLoopData* data = reinterpret_cast<RunLoopData*>(param);
779 ASSERT(data->monitor != NULL);
780 MonitorLocker ml(data->monitor);
781 data->done = true;
782 ml.Notify();
783 }
784
785
771 DART_EXPORT Dart_Handle Dart_RunLoop() { 786 DART_EXPORT Dart_Handle Dart_RunLoop() {
772 Isolate* isolate = Isolate::Current(); 787 Isolate* isolate = Isolate::Current();
788
773 DARTSCOPE(isolate); 789 DARTSCOPE(isolate);
774 const Object& obj = Object::Handle(isolate, isolate->StandardRunLoop()); 790 Monitor monitor;
791 MonitorLocker ml(&monitor);
792 {
793 SwitchIsolateScope switch_scope(NULL);
794
795 RunLoopData data;
796 data.monitor = &monitor;
797 data.done = false;
798 isolate->message_handler()->Run(
799 Dart::thread_pool(),
800 NULL, RunLoopDone, reinterpret_cast<uword>(&data));
801 while (!data.done) {
802 ml.Wait();
803 }
804 }
805 const Object& obj = Object::Handle(isolate->object_store()->sticky_error());
806 isolate->object_store()->clear_sticky_error();
775 if (obj.IsError()) { 807 if (obj.IsError()) {
776 return Api::NewHandle(isolate, obj.raw()); 808 return Api::NewHandle(isolate, obj.raw());
777 } 809 }
778 ASSERT(obj.IsNull()); 810 ASSERT(obj.IsNull());
779 return Api::Success(isolate); 811 return Api::Success(isolate);
780 } 812 }
781 813
782 814
783 static RawInstance* DeserializeMessage(Isolate* isolate, void* data) {
784 // Create a snapshot object using the buffer.
785 const Snapshot* snapshot = Snapshot::SetupFromBuffer(data);
786 ASSERT(snapshot->IsMessageSnapshot());
787
788 // Read object back from the snapshot.
789 SnapshotReader reader(snapshot, isolate);
790 Instance& instance = Instance::Handle(isolate);
791 instance ^= reader.ReadObject();
792 return instance.raw();
793 }
794
795
796 DART_EXPORT Dart_Handle Dart_HandleMessage() { 815 DART_EXPORT Dart_Handle Dart_HandleMessage() {
797 Isolate* isolate = Isolate::Current(); 816 Isolate* isolate = Isolate::Current();
798 // Process all OOB messages and at most one normal message. 817 CHECK_ISOLATE(isolate);
799 Message* message = NULL; 818 if (!isolate->message_handler()->HandleNextMessage()) {
800 Message::Priority priority = Message::kNormalPriority; 819 // TODO(turnidge): Clear sticky error here?
801 do { 820 return Api::NewHandle(isolate, isolate->object_store()->sticky_error());
802 DARTSCOPE(isolate); 821 }
803 // TODO(turnidge): This code is duplicated elsewhere. Consolidate.
804 message = isolate->message_handler()->queue()->DequeueNoWait();
805 if (message == NULL) {
806 break;
807 }
808 const Instance& msg =
809 Instance::Handle(isolate, DeserializeMessage(isolate, message->data()));
810 priority = message->priority();
811 if (priority == Message::kOOBPriority) {
812 // For now the only OOB messages are Mirrors messages.
813 const Object& result = Object::Handle(
814 isolate,
815 DartLibraryCalls::HandleMirrorsMessage(
816 message->dest_port(), message->reply_port(), msg));
817 delete message;
818 if (result.IsError()) {
819 // TODO(turnidge): Propagating the error is probably wrong here.
820 return Api::NewHandle(isolate, result.raw());
821 }
822 ASSERT(result.IsNull());
823 } else {
824 const Object& result = Object::Handle(
825 isolate,
826 DartLibraryCalls::HandleMessage(
827 message->dest_port(), message->reply_port(), msg));
828 delete message;
829 if (result.IsError()) {
830 return Api::NewHandle(isolate, result.raw());
831 }
832 ASSERT(result.IsNull());
833 }
834 } while (priority >= Message::kOOBPriority);
835 return Api::Success(isolate); 822 return Api::Success(isolate);
836 } 823 }
837 824
838 825
839 DART_EXPORT bool Dart_HasLivePorts() { 826 DART_EXPORT bool Dart_HasLivePorts() {
840 Isolate* isolate = Isolate::Current(); 827 Isolate* isolate = Isolate::Current();
841 ASSERT(isolate); 828 ASSERT(isolate);
842 return isolate->message_handler()->HasLivePorts(); 829 return isolate->message_handler()->HasLivePorts();
843 } 830 }
844 831
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
889 } 876 }
890 877
891 878
892 DART_EXPORT Dart_Port Dart_NewNativePort(const char* name, 879 DART_EXPORT Dart_Port Dart_NewNativePort(const char* name,
893 Dart_NativeMessageHandler handler, 880 Dart_NativeMessageHandler handler,
894 bool handle_concurrently) { 881 bool handle_concurrently) {
895 if (name == NULL) { 882 if (name == NULL) {
896 name = "<UnnamedNativePort>"; 883 name = "<UnnamedNativePort>";
897 } 884 }
898 if (handler == NULL) { 885 if (handler == NULL) {
899 OS::PrintErr("%s expects argument 'handler' to be non-null.", CURRENT_FUNC); 886 OS::PrintErr("%s expects argument 'handler' to be non-null.\n",
887 CURRENT_FUNC);
900 return kIllegalPort; 888 return kIllegalPort;
901 } 889 }
902 // Start the native port without a current isolate. 890 // Start the native port without a current isolate.
903 IsolateSaver saver(Isolate::Current()); 891 IsolateSaver saver(Isolate::Current());
904 Isolate::SetCurrent(NULL); 892 Isolate::SetCurrent(NULL);
905 893
906 NativeMessageHandler* nmh = new NativeMessageHandler(name, handler); 894 NativeMessageHandler* nmh = new NativeMessageHandler(name, handler);
907 Dart_Port port_id = PortMap::CreatePort(nmh); 895 Dart_Port port_id = PortMap::CreatePort(nmh);
908 nmh->StartWorker(); 896 nmh->Run(Dart::thread_pool(), NULL, NULL, NULL);
909 return port_id; 897 return port_id;
910 } 898 }
911 899
912 900
913 DART_EXPORT bool Dart_CloseNativePort(Dart_Port native_port_id) { 901 DART_EXPORT bool Dart_CloseNativePort(Dart_Port native_port_id) {
914 // Close the native port without a current isolate. 902 // Close the native port without a current isolate.
915 IsolateSaver saver(Isolate::Current()); 903 IsolateSaver saver(Isolate::Current());
916 Isolate::SetCurrent(NULL); 904 Isolate::SetCurrent(NULL);
917 905
918 // TODO(turnidge): Check that the port is native before trying to close. 906 // TODO(turnidge): Check that the port is native before trying to close.
(...skipping 2523 matching lines...) Expand 10 before | Expand all | Expand 10 after
3442 *buffer = NULL; 3430 *buffer = NULL;
3443 } 3431 }
3444 delete debug_region; 3432 delete debug_region;
3445 } else { 3433 } else {
3446 *buffer = NULL; 3434 *buffer = NULL;
3447 *buffer_size = 0; 3435 *buffer_size = 0;
3448 } 3436 }
3449 } 3437 }
3450 3438
3451 } // namespace dart 3439 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/dart.cc ('k') | runtime/vm/isolate.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698