Index: runtime/vm/custom_isolate_test.cc |
=================================================================== |
--- runtime/vm/custom_isolate_test.cc (revision 3210) |
+++ runtime/vm/custom_isolate_test.cc (working copy) |
@@ -85,12 +85,9 @@ |
virtual ~Event() {} |
virtual void Process() = 0; |
- virtual bool IsShutdownEvent(Dart_Isolate isolate) { |
+ virtual bool IsMessageEvent(Dart_Isolate isolate) { |
Anton Muhin
2012/01/12 12:58:16
maybe now move this logic into StartEvent and add
turnidge
2012/01/12 19:01:55
I have added an isolate_ field to the base Event c
Anton Muhin
2012/01/13 14:26:53
Yes, thank you.
On 2012/01/12 19:01:55, turnidge
|
return false; |
} |
- virtual bool IsMessageEvent(Dart_Isolate isolate, Dart_Port port) { |
- return false; |
- } |
private: |
friend class EventQueue; |
@@ -98,6 +95,66 @@ |
}; |
+// A simple event queue for our test. |
+class EventQueue { |
+ public: |
+ EventQueue() { |
+ head_ = NULL; |
+ } |
+ |
+ void Add(Event* event) { |
+ if (head_ == NULL) { |
+ head_ = event; |
+ tail_ = event; |
+ } else { |
+ tail_->next_ = event; |
+ tail_ = event; |
+ } |
+ } |
+ |
+ Event* Get() { |
+ if (head_ == NULL) { |
+ return NULL; |
+ } |
+ Event* tmp = head_; |
+ head_ = head_->next_; |
+ if (head_ == NULL) { |
+ tail_ = NULL; |
Anton Muhin
2012/01/12 12:58:16
technically, you don't need this assignment, corre
turnidge
2012/01/12 19:01:55
Added a comment.
|
+ } |
+ |
+ return tmp; |
+ } |
+ |
+ void Remove(Dart_Isolate isolate) { |
Anton Muhin
2012/01/12 12:58:16
nit: longer name like RemoveEventsForIsolate may m
turnidge
2012/01/12 19:01:55
Done.
|
+ Event* cur = head_; |
+ Event* prev = NULL; |
+ while (cur != NULL) { |
+ Event* next = cur->next_; |
+ if (cur->IsMessageEvent(isolate)) { |
+ // Remove matching event. |
+ if (prev != NULL) { |
+ prev->next_ = next; |
+ } else { |
+ head_ = next; |
+ } |
+ delete cur; |
+ } else { |
+ // Advance. |
+ prev = cur; |
+ } |
+ cur = next; |
+ } |
+ tail_ = prev; |
+ } |
+ |
+ private: |
+ Event* head_; |
+ Event* tail_; |
+}; |
+Event* current_event; |
Anton Muhin
2012/01/12 12:58:16
it looks like you don't need current_event any mor
turnidge
2012/01/12 19:01:55
Thanks, removed.
|
+EventQueue* event_queue; |
+ |
+ |
// Start an isolate. |
class StartEvent : public Event { |
public: |
@@ -150,163 +207,51 @@ |
} |
-// Shutdown an isolate. |
-class ShutdownEvent : public Event { |
- public: |
- explicit ShutdownEvent(Dart_Isolate isolate) : isolate_(isolate) {} |
- |
- virtual bool IsShutdownEvent(Dart_Isolate isolate) { |
- return isolate == isolate_; |
- } |
- |
- virtual void Process(); |
- private: |
- Dart_Isolate isolate_; |
-}; |
- |
- |
-void ShutdownEvent::Process() { |
- OS::Print("<< ShutdownEvent with isolate(%p)--\n", isolate_); |
- Dart_EnterIsolate(isolate_); |
- Dart_ShutdownIsolate(); |
-} |
- |
- |
-// Deliver a message to an isolate. |
+// Notify an isolate of a pending message. |
class MessageEvent : public Event { |
public: |
- MessageEvent(Dart_Isolate isolate, Dart_Port dest, Dart_Port reply, |
- Dart_Message msg) |
- : isolate_(isolate), dest_(dest), reply_(reply), msg_(msg) {} |
+ explicit MessageEvent(Dart_Isolate isolate) : isolate_(isolate) {} |
~MessageEvent() { |
- free(msg_); |
- msg_ = NULL; |
} |
- virtual bool IsMessageEvent(Dart_Isolate isolate, Dart_Port port) { |
- return isolate == isolate_ && (port == kCloseAllPorts || port == dest_); |
+ virtual bool IsMessageEvent(Dart_Isolate isolate) { |
+ return isolate == isolate_; |
} |
virtual void Process(); |
private: |
Dart_Isolate isolate_; |
- Dart_Port dest_; |
- Dart_Port reply_; |
- Dart_Message msg_; |
}; |
void MessageEvent::Process() { |
- OS::Print("$$ MessageEvent with dest port %lld--\n", dest_); |
+ OS::Print("$$ MessageEvent with isolate(%p)\n", isolate_); |
Dart_EnterIsolate(isolate_); |
Dart_EnterScope(); |
- Dart_Handle result = Dart_HandleMessage(dest_, reply_, msg_); |
+ Dart_Handle result = Dart_HandleMessage(); |
EXPECT_VALID(result); |
- Dart_ExitScope(); |
- Dart_ExitIsolate(); |
+ if (!Dart_HasLivePorts()) { |
+ OS::Print("<< Shutting down isolate(%p)\n", isolate_); |
+ event_queue->Remove(isolate_); |
+ Dart_ShutdownIsolate(); |
+ } else { |
+ Dart_ExitScope(); |
+ Dart_ExitIsolate(); |
+ } |
+ ASSERT(Dart_CurrentIsolate() == NULL); |
} |
-// A simple event queue for our test. |
-class EventQueue { |
- public: |
- EventQueue() { |
- head_ = NULL; |
- } |
- |
- void Add(Event* event) { |
- if (head_ == NULL) { |
- head_ = event; |
- tail_ = event; |
- } else { |
- tail_->next_ = event; |
- tail_ = event; |
- } |
- } |
- |
- Event* Get() { |
- if (head_ == NULL) { |
- return NULL; |
- } |
- Event* tmp = head_; |
- head_ = head_->next_; |
- if (head_ == NULL) { |
- tail_ = NULL; |
- } |
- |
- return tmp; |
- } |
- |
- void ClosePort(Dart_Isolate isolate, Dart_Port port) { |
- Event* cur = head_; |
- Event* prev = NULL; |
- while (cur != NULL) { |
- Event* next = cur->next_; |
- if (cur->IsMessageEvent(isolate, port)) { |
- // Remove matching event. |
- if (prev != NULL) { |
- prev->next_ = next; |
- } else { |
- head_ = next; |
- } |
- delete cur; |
- } else { |
- // Advance. |
- prev = cur; |
- } |
- cur = next; |
- } |
- tail_ = prev; |
- } |
- |
- private: |
- Event* head_; |
- Event* tail_; |
-}; |
-EventQueue* event_queue; |
-Event* current_event; |
- |
-static bool PostMessage(Dart_Isolate dest_isolate, |
- Dart_Port dest_port, |
- Dart_Port reply_port, |
- Dart_Message message) { |
- OS::Print("-- Posting message dest(%d) reply(%d) --\n", |
- dest_port, reply_port); |
+static void NotifyMessage(Dart_Isolate dest_isolate) { |
+ OS::Print("-- Notify isolate(%p) of pending message --\n", dest_isolate); |
OS::Print("-- Adding MessageEvent to queue --\n"); |
- event_queue->Add( |
- new MessageEvent(dest_isolate, dest_port, reply_port, message)); |
- return true; |
+ event_queue->Add(new MessageEvent(dest_isolate)); |
} |
-static void ClosePort(Dart_Isolate isolate, |
- Dart_Port port) { |
- OS::Print("-- Closing port (%lld) for isolate(%p) --\n", |
- port, isolate); |
- |
- // Remove any pending events for the isolate/port. |
- event_queue->ClosePort(isolate, port); |
- |
- Dart_Isolate current = Dart_CurrentIsolate(); |
- if (current) { |
- Dart_ExitIsolate(); |
- } |
- Dart_EnterIsolate(isolate); |
- if (!Dart_HasLivePorts() && |
- (current_event == NULL || !current_event->IsShutdownEvent(isolate))) { |
- OS::Print("-- Adding ShutdownEvent to queue --\n"); |
- event_queue->Add(new ShutdownEvent(isolate)); |
- } |
- Dart_ExitIsolate(); |
- if (current) { |
- Dart_EnterIsolate(current); |
- } |
-} |
- |
- |
static Dart_NativeFunction NativeLookup(Dart_Handle name, int argc) { |
const char* name_str = NULL; |
EXPECT(Dart_IsString(name)); |
@@ -358,7 +303,7 @@ |
// Create a new Dart_Isolate. |
Dart_Isolate new_isolate = TestCase::CreateTestIsolate(); |
EXPECT(new_isolate != NULL); |
- Dart_SetMessageCallbacks(&PostMessage, &ClosePort); |
+ Dart_SetMessageNotifyCallback(&NotifyMessage); |
Dart_Port new_port = Dart_GetMainPortId(); |
OS::Print("-- Adding StartEvent to queue --\n"); |
@@ -384,7 +329,7 @@ |
Dart_Isolate dart_isolate = TestCase::CreateTestIsolate(); |
EXPECT(dart_isolate != NULL); |
- Dart_SetMessageCallbacks(&PostMessage, &ClosePort); |
+ Dart_SetMessageNotifyCallback(&NotifyMessage); |
Dart_EnterScope(); |
Dart_Handle result; |