OLD | NEW |
---|---|
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 Loading... | |
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); | |
siva
2012/04/18 22:05:00
ASSERT(data->monitor != NULL);
turnidge
2012/04/19 19:46:55
Done.
| |
779 MonitorLocker ml(data->monitor); | |
780 data->done = true; | |
781 ml.Notify(); | |
782 } | |
783 | |
784 | |
771 DART_EXPORT Dart_Handle Dart_RunLoop() { | 785 DART_EXPORT Dart_Handle Dart_RunLoop() { |
772 Isolate* isolate = Isolate::Current(); | 786 Isolate* isolate = Isolate::Current(); |
787 | |
773 DARTSCOPE(isolate); | 788 DARTSCOPE(isolate); |
774 const Object& obj = Object::Handle(isolate, isolate->StandardRunLoop()); | 789 Monitor monitor; |
790 MonitorLocker ml(&monitor); | |
791 { | |
792 SwitchIsolateScope switch_scope(NULL); | |
793 | |
794 RunLoopData data; | |
795 data.monitor = &monitor; | |
796 data.done = false; | |
797 isolate->message_handler()->Run( | |
798 Dart::thread_pool(), | |
799 NULL, RunLoopDone, reinterpret_cast<uword>(&data)); | |
800 while (!data.done) { | |
801 ml.Wait(); | |
802 } | |
803 } | |
804 const Object& obj = Object::Handle(isolate->object_store()->sticky_error()); | |
805 isolate->object_store()->clear_sticky_error(); | |
775 if (obj.IsError()) { | 806 if (obj.IsError()) { |
776 return Api::NewHandle(isolate, obj.raw()); | 807 return Api::NewHandle(isolate, obj.raw()); |
777 } | 808 } |
778 ASSERT(obj.IsNull()); | 809 ASSERT(obj.IsNull()); |
779 return Api::Success(isolate); | 810 return Api::Success(isolate); |
780 } | 811 } |
781 | 812 |
782 | 813 |
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() { | 814 DART_EXPORT Dart_Handle Dart_HandleMessage() { |
797 Isolate* isolate = Isolate::Current(); | 815 Isolate* isolate = Isolate::Current(); |
798 // Process all OOB messages and at most one normal message. | 816 CHECK_ISOLATE(isolate); |
799 Message* message = NULL; | 817 if (!isolate->message_handler()->HandleNextMessage()) { |
800 Message::Priority priority = Message::kNormalPriority; | 818 // TODO(turnidge): Clear sticky error here? |
801 do { | 819 return Api::NewHandle(isolate, isolate->object_store()->sticky_error()); |
802 DARTSCOPE(isolate); | 820 } |
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); | 821 return Api::Success(isolate); |
836 } | 822 } |
837 | 823 |
838 | 824 |
839 DART_EXPORT bool Dart_HasLivePorts() { | 825 DART_EXPORT bool Dart_HasLivePorts() { |
840 Isolate* isolate = Isolate::Current(); | 826 Isolate* isolate = Isolate::Current(); |
841 ASSERT(isolate); | 827 ASSERT(isolate); |
842 return isolate->message_handler()->HasLivePorts(); | 828 return isolate->message_handler()->HasLivePorts(); |
843 } | 829 } |
844 | 830 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
898 if (handler == NULL) { | 884 if (handler == NULL) { |
899 OS::PrintErr("%s expects argument 'handler' to be non-null.", CURRENT_FUNC); | 885 OS::PrintErr("%s expects argument 'handler' to be non-null.", CURRENT_FUNC); |
900 return kIllegalPort; | 886 return kIllegalPort; |
901 } | 887 } |
902 // Start the native port without a current isolate. | 888 // Start the native port without a current isolate. |
903 IsolateSaver saver(Isolate::Current()); | 889 IsolateSaver saver(Isolate::Current()); |
904 Isolate::SetCurrent(NULL); | 890 Isolate::SetCurrent(NULL); |
905 | 891 |
906 NativeMessageHandler* nmh = new NativeMessageHandler(name, handler); | 892 NativeMessageHandler* nmh = new NativeMessageHandler(name, handler); |
907 Dart_Port port_id = PortMap::CreatePort(nmh); | 893 Dart_Port port_id = PortMap::CreatePort(nmh); |
908 nmh->StartWorker(); | 894 nmh->Run(Dart::thread_pool(), NULL, NULL, NULL); |
909 return port_id; | 895 return port_id; |
910 } | 896 } |
911 | 897 |
912 | 898 |
913 DART_EXPORT bool Dart_CloseNativePort(Dart_Port native_port_id) { | 899 DART_EXPORT bool Dart_CloseNativePort(Dart_Port native_port_id) { |
914 // Close the native port without a current isolate. | 900 // Close the native port without a current isolate. |
915 IsolateSaver saver(Isolate::Current()); | 901 IsolateSaver saver(Isolate::Current()); |
916 Isolate::SetCurrent(NULL); | 902 Isolate::SetCurrent(NULL); |
917 | 903 |
918 // TODO(turnidge): Check that the port is native before trying to close. | 904 // TODO(turnidge): Check that the port is native before trying to close. |
(...skipping 2480 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3399 *buffer = NULL; | 3385 *buffer = NULL; |
3400 } | 3386 } |
3401 delete debug_region; | 3387 delete debug_region; |
3402 } else { | 3388 } else { |
3403 *buffer = NULL; | 3389 *buffer = NULL; |
3404 *buffer_size = 0; | 3390 *buffer_size = 0; |
3405 } | 3391 } |
3406 } | 3392 } |
3407 | 3393 |
3408 } // namespace dart | 3394 } // namespace dart |
OLD | NEW |