Skip to content

Commit cae03aa

Browse files
committed
Wait for eventhandler to shut down before exiting
[email protected] Review URL: https://codereview.chromium.org//875403006 git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@43504 260f80e4-7a28-3924-810f-c04153c831b5
1 parent 1c81585 commit cae03aa

6 files changed

+48
-15
lines changed

runtime/bin/eventhandler.cc

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
#include "bin/dartutils.h"
66
#include "bin/eventhandler.h"
7+
#include "bin/lockers.h"
78
#include "bin/socket.h"
9+
#include "bin/thread.h"
810

911
#include "include/dart_api.h"
1012

@@ -57,19 +59,40 @@ void TimeoutQueue::UpdateTimeout(Dart_Port port, int64_t timeout) {
5759

5860

5961
static EventHandler* event_handler = NULL;
62+
static Monitor *shutdown_monitor = NULL;
6063

6164

6265
void EventHandler::Start() {
6366
ASSERT(event_handler == NULL);
67+
shutdown_monitor = new Monitor();
6468
event_handler = new EventHandler();
6569
event_handler->delegate_.Start(event_handler);
6670
}
6771

6872

73+
void EventHandler::NotifyShutdownDone() {
74+
MonitorLocker ml(shutdown_monitor);
75+
ml.Notify();
76+
}
77+
78+
6979
void EventHandler::Stop() {
7080
if (event_handler == NULL) return;
71-
event_handler->delegate_.Shutdown();
81+
82+
// Wait until it has stopped.
83+
{
84+
MonitorLocker ml(shutdown_monitor);
85+
86+
// Signal to event handler that we want it to stop.
87+
event_handler->delegate_.Shutdown();
88+
ml.Wait(Monitor::kNoTimeout);
89+
}
90+
91+
// Cleanup
92+
delete event_handler;
7293
event_handler = NULL;
94+
delete shutdown_monitor;
95+
shutdown_monitor = NULL;
7396
}
7497

7598

runtime/bin/eventhandler.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ class EventHandler {
119119
delegate_.SendData(id, dart_port, data);
120120
}
121121

122+
/**
123+
* Signal to main thread that event handler is done.
124+
*/
125+
void NotifyShutdownDone();
126+
122127
/**
123128
* Start the event-handler.
124129
*/

runtime/bin/eventhandler_android.cc

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -299,25 +299,27 @@ void EventHandlerImplementation::Poll(uword args) {
299299
ThreadSignalBlocker signal_blocker(SIGPROF);
300300
static const intptr_t kMaxEvents = 16;
301301
struct epoll_event events[kMaxEvents];
302-
EventHandlerImplementation* handler =
303-
reinterpret_cast<EventHandlerImplementation*>(args);
304-
ASSERT(handler != NULL);
305-
while (!handler->shutdown_) {
306-
int64_t millis = handler->GetTimeout();
302+
EventHandler* handler = reinterpret_cast<EventHandler*>(args);
303+
EventHandlerImplementation* handler_impl = &handler->delegate_;
304+
ASSERT(handler_impl != NULL);
305+
306+
while (!handler_impl->shutdown_) {
307+
int64_t millis = handler_impl->GetTimeout();
307308
ASSERT(millis == kInfinityTimeout || millis >= 0);
308309
if (millis > kMaxInt32) millis = kMaxInt32;
309310
intptr_t result = TEMP_FAILURE_RETRY_NO_SIGNAL_BLOCKER(
310-
epoll_wait(handler->epoll_fd_, events, kMaxEvents, millis));
311+
epoll_wait(handler_impl->epoll_fd_, events, kMaxEvents, millis));
311312
ASSERT(EAGAIN == EWOULDBLOCK);
312313
if (result == -1) {
313314
if (errno != EWOULDBLOCK) {
314315
perror("Poll failed");
315316
}
316317
} else {
317-
handler->HandleTimeout();
318-
handler->HandleEvents(events, result);
318+
handler_impl->HandleTimeout();
319+
handler_impl->HandleEvents(events, result);
319320
}
320321
}
322+
handler->NotifyShutdownDone();
321323
}
322324

323325

runtime/bin/eventhandler_linux.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ void EventHandlerImplementation::Poll(uword args) {
315315
EventHandler* handler = reinterpret_cast<EventHandler*>(args);
316316
EventHandlerImplementation* handler_impl = &handler->delegate_;
317317
ASSERT(handler_impl != NULL);
318+
318319
while (!handler_impl->shutdown_) {
319320
intptr_t result = TEMP_FAILURE_RETRY_NO_SIGNAL_BLOCKER(
320321
epoll_wait(handler_impl->epoll_fd_, events, kMaxEvents, -1));
@@ -327,13 +328,13 @@ void EventHandlerImplementation::Poll(uword args) {
327328
handler_impl->HandleEvents(events, result);
328329
}
329330
}
330-
delete handler;
331+
handler->NotifyShutdownDone();
331332
}
332333

333334

334335
void EventHandlerImplementation::Start(EventHandler* handler) {
335336
int result = Thread::Start(&EventHandlerImplementation::Poll,
336-
reinterpret_cast<uword>(handler));
337+
reinterpret_cast<uword>(handler));
337338
if (result != 0) {
338339
FATAL1("Failed to start event handler thread %d", result);
339340
}

runtime/bin/eventhandler_macos.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ void EventHandlerImplementation::EventHandlerEntry(uword args) {
358358
EventHandler* handler = reinterpret_cast<EventHandler*>(args);
359359
EventHandlerImplementation* handler_impl = &handler->delegate_;
360360
ASSERT(handler_impl != NULL);
361+
361362
while (!handler_impl->shutdown_) {
362363
int64_t millis = handler_impl->GetTimeout();
363364
ASSERT(millis == kInfinityTimeout || millis >= 0);
@@ -387,14 +388,14 @@ void EventHandlerImplementation::EventHandlerEntry(uword args) {
387388
handler_impl->HandleEvents(events, result);
388389
}
389390
}
390-
delete handler;
391+
handler->NotifyShutdownDone();
391392
}
392393

393394

394395
void EventHandlerImplementation::Start(EventHandler* handler) {
395396
int result =
396397
Thread::Start(&EventHandlerImplementation::EventHandlerEntry,
397-
reinterpret_cast<uword>(handler));
398+
reinterpret_cast<uword>(handler));
398399
if (result != 0) {
399400
FATAL1("Failed to start event handler thread %d", result);
400401
}

runtime/bin/eventhandler_win.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,6 +1295,7 @@ void EventHandlerImplementation::EventHandlerEntry(uword args) {
12951295
EventHandler* handler = reinterpret_cast<EventHandler*>(args);
12961296
EventHandlerImplementation* handler_impl = &handler->delegate_;
12971297
ASSERT(handler_impl != NULL);
1298+
12981299
while (!handler_impl->shutdown_) {
12991300
DWORD bytes;
13001301
ULONG_PTR key;
@@ -1344,13 +1345,13 @@ void EventHandlerImplementation::EventHandlerEntry(uword args) {
13441345
handler_impl->HandleIOCompletion(bytes, key, overlapped);
13451346
}
13461347
}
1347-
delete handler;
1348+
handler->NotifyShutdownDone();
13481349
}
13491350

13501351

13511352
void EventHandlerImplementation::Start(EventHandler* handler) {
13521353
int result = Thread::Start(EventHandlerEntry,
1353-
reinterpret_cast<uword>(handler));
1354+
reinterpret_cast<uword>(handler));
13541355
if (result != 0) {
13551356
FATAL1("Failed to start event handler thread %d", result);
13561357
}

0 commit comments

Comments
 (0)