| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium 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 part of core; | 5 part of internal; |
| 6 | |
| 7 class _MojoHandleWatcherNatives { | |
| 8 static int sendControlData(int controlHandle, int mojoHandle, SendPort port, | |
| 9 int data) native "MojoHandleWatcher_SendControlData"; | |
| 10 static List recvControlData( | |
| 11 int controlHandle) native "MojoHandleWatcher_RecvControlData"; | |
| 12 static int setControlHandle( | |
| 13 int controlHandle) native "MojoHandleWatcher_SetControlHandle"; | |
| 14 static int getControlHandle() native "MojoHandleWatcher_GetControlHandle"; | |
| 15 } | |
| 16 | 6 |
| 17 // The MojoHandleWatcher sends a stream of events to application isolates that | 7 // The MojoHandleWatcher sends a stream of events to application isolates that |
| 18 // register Mojo handles with it. Application isolates make the following calls: | 8 // register Mojo handles with it. Application isolates make the following calls: |
| 19 // | 9 // |
| 20 // add(handle, port, signals) - Instructs the MojoHandleWatcher isolate to add | 10 // add(handle, port, signals) - Instructs the MojoHandleWatcher isolate to add |
| 21 // 'handle' to the set of handles it watches, and to notify the calling | 11 // 'handle' to the set of handles it watches, and to notify the calling |
| 22 // isolate only for the events specified by 'signals' using the send port | 12 // isolate only for the events specified by 'signals' using the send port |
| 23 // 'port' | 13 // 'port' |
| 24 // | 14 // |
| 25 // remove(handle) - Instructs the MojoHandleWatcher isolate to remove 'handle' | 15 // remove(handle) - Instructs the MojoHandleWatcher isolate to remove 'handle' |
| 26 // from the set of handles it watches. This allows the application isolate | 16 // from the set of handles it watches. This allows the application isolate |
| 27 // to, e.g., pause the stream of events. | 17 // to, e.g., pause the stream of events. |
| 28 // | 18 // |
| 29 // close(handle) - Notifies the HandleWatcherIsolate that a handle it is | 19 // close(handle) - Notifies the HandleWatcherIsolate that a handle it is |
| 30 // watching should be removed from its set and closed. | 20 // watching should be removed from its set and closed. |
| 31 class MojoHandleWatcher { | 21 class MojoHandleWatcher { |
| 32 // Control commands. | 22 // Control commands. |
| 33 static const int ADD = 0; | 23 static const int ADD = 0; |
| 34 static const int REMOVE = 1; | 24 static const int REMOVE = 1; |
| 35 static const int CLOSE = 2; | 25 static const int CLOSE = 2; |
| 36 static const int TIMER = 3; | 26 static const int TIMER = 3; |
| 37 static const int SHUTDOWN = 4; | 27 static const int SHUTDOWN = 4; |
| 38 | 28 |
| 29 static const int kMojoHandleInvalid = 0; |
| 30 static const int kDeadlineIndefinite = -1; |
| 31 |
| 32 static const int kMojoResultOk = 0; |
| 33 static const int kMojoResultDeadlineExceeded = -4; |
| 34 static const int kMojoResultFailedPrecondition = -9; |
| 35 |
| 36 static const int kMojoSignalsReadable = (1 << 0); |
| 37 static const int kMojoSignalsWritable = (1 << 1); |
| 38 static const int kMojoSignalsPeerClosed = (1 << 2); |
| 39 static const int kMojoSignalsAll = |
| 40 kMojoSignalsReadable | kMojoSignalsWritable | kMojoSignalsPeerClosed; |
| 41 |
| 39 static int _encodeCommand(int cmd, [int signals = 0]) => | 42 static int _encodeCommand(int cmd, [int signals = 0]) => |
| 40 (cmd << 3) | (signals & MojoHandleSignals.kAll); | 43 (cmd << 3) | (signals & kMojoSignalsAll); |
| 41 static int _decodeCommand(int cmd) { | 44 static int _decodeCommand(int cmd) { |
| 42 assert(MojoHandleSignals.kAll < 1 << 3); | 45 assert(kMojoSignalsAll < 1 << 3); |
| 43 return cmd >> 3; | 46 return cmd >> 3; |
| 44 } | 47 } |
| 45 static MojoHandleSignals _decodeSignals(int cmd) { | 48 static int _decodeSignals(int cmd) { |
| 46 return new MojoHandleSignals(cmd & MojoHandleSignals.kAll); | 49 return cmd & kMojoSignalsAll; |
| 47 } | 50 } |
| 48 | 51 |
| 49 // The Mojo handle over which control messages are sent. | 52 // The Mojo handle over which control messages are sent. |
| 50 int _controlHandle; | 53 int _controlHandle; |
| 51 | 54 |
| 52 // Whether the handle watcher should shut down. | 55 // Whether the handle watcher should shut down. |
| 53 bool _shutdown; | 56 bool _shutdown; |
| 54 | 57 |
| 55 // The list of handles being watched. | 58 // The list of handles being watched. |
| 56 List<int> _handles; | 59 List<int> _handles; |
| 57 int _handleCount; | 60 int _handleCount; |
| 58 | 61 |
| 59 // A port for each handle on which to send events back to the isolate that | 62 // A port for each handle on which to send events back to the isolate that |
| 60 // owns the handle. | 63 // owns the handle. |
| 61 List<SendPort> _ports; | 64 List<SendPort> _ports; |
| 62 | 65 |
| 63 // The signals that we care about for each handle. | 66 // The signals that we care about for each handle. |
| 64 List<int> _signals; | 67 List<int> _signals; |
| 65 | 68 |
| 66 // A mapping from Mojo handles to their indices in _handles. | 69 // A mapping from Mojo handles to their indices in _handles. |
| 67 Map<int, int> _handleIndices; | 70 Map<int, int> _handleIndices; |
| 68 | 71 |
| 69 // Since we are not storing wrapped handles, a dummy handle for when we need | |
| 70 // a MojoHandle. | |
| 71 MojoHandle _tempHandle; | |
| 72 | |
| 73 // Priority queue of timers registered with the watcher. | 72 // Priority queue of timers registered with the watcher. |
| 74 TimerQueue _timerQueue; | 73 TimerQueue _timerQueue; |
| 75 | 74 |
| 76 MojoHandleWatcher(this._controlHandle) | 75 MojoHandleWatcher(this._controlHandle) |
| 77 : _shutdown = false, | 76 : _shutdown = false, |
| 78 _handles = new List<int>(), | 77 _handles = new List<int>(), |
| 79 _ports = new List<SendPort>(), | 78 _ports = new List<SendPort>(), |
| 80 _signals = new List<int>(), | 79 _signals = new List<int>(), |
| 81 _handleIndices = new Map<int, int>(), | 80 _handleIndices = new Map<int, int>(), |
| 82 _handleCount = 1, | 81 _handleCount = 1, |
| 83 _tempHandle = new MojoHandle(MojoHandle.INVALID), | |
| 84 _timerQueue = new TimerQueue() { | 82 _timerQueue = new TimerQueue() { |
| 85 // Setup control handle. | 83 // Setup control handle. |
| 86 _handles.add(_controlHandle); | 84 _handles.add(_controlHandle); |
| 87 _ports.add(null); // There is no port for the control handle. | 85 _ports.add(null); // There is no port for the control handle. |
| 88 _signals.add(MojoHandleSignals.kReadable); | 86 _signals.add(kMojoSignalsReadable); |
| 89 _handleIndices[_controlHandle] = 0; | 87 _handleIndices[_controlHandle] = 0; |
| 90 } | 88 } |
| 91 | 89 |
| 92 static void _handleWatcherIsolate(int consumerHandle) { | 90 static void _handleWatcherIsolate(int consumerHandle) { |
| 93 MojoHandleWatcher watcher = new MojoHandleWatcher(consumerHandle); | 91 MojoHandleWatcher watcher = new MojoHandleWatcher(consumerHandle); |
| 94 while (!watcher._shutdown) { | 92 while (!watcher._shutdown) { |
| 95 int deadline = watcher._processTimerDeadlines(); | 93 int deadline = watcher._processTimerDeadlines(); |
| 96 MojoWaitManyResult mwmr = | 94 // mwmr[0]: result, mwmr[1]: index, mwmr[2]: list of signal states. |
| 97 MojoHandle.waitMany(watcher._handles, watcher._signals, deadline); | 95 List mwmr = MojoHandleNatives.waitMany( |
| 98 if (mwmr.result.isOk && mwmr.index == 0) { | 96 watcher._handles, watcher._signals, deadline); |
| 97 if ((mwmr[0] == kMojoResultOk) && (mwmr[1] == 0)) { |
| 99 watcher._handleControlMessage(); | 98 watcher._handleControlMessage(); |
| 100 } else if (mwmr.result.isOk && (mwmr.index > 0)) { | 99 } else if ((mwmr[0] == kMojoResultOk) && (mwmr[1] > 0)) { |
| 101 int handle = watcher._handles[mwmr.index]; | 100 int handle = watcher._handles[mwmr[1]]; |
| 102 | 101 |
| 103 // Route event. | 102 // Route event. |
| 104 watcher._routeEvent( | 103 watcher._routeEvent(mwmr[2][mwmr[1]][0], mwmr[1]); |
| 105 mwmr.states[mwmr.index].satisfied_signals, mwmr.index); | |
| 106 // Remove the handle from the list. | 104 // Remove the handle from the list. |
| 107 watcher._removeHandle(handle); | 105 watcher._removeHandle(handle); |
| 108 } else if (!mwmr.result.isDeadlineExceeded) { | 106 } else if (mwmr[0] != kMojoResultDeadlineExceeded) { |
| 109 // Some handle was closed, but not by us. | 107 // Some handle was closed, but not by us. |
| 110 // Find it and close it on our side. | 108 // Find it and close it on our side. |
| 111 watcher._pruneClosedHandles(mwmr.states); | 109 watcher._pruneClosedHandles(mwmr[2]); |
| 112 } | 110 } |
| 113 } | 111 } |
| 114 } | 112 } |
| 115 | 113 |
| 116 void _routeEvent(int satisfiedSignals, int idx) { | 114 void _routeEvent(int satisfiedSignals, int idx) { |
| 117 _ports[idx].send([_signals[idx], satisfiedSignals & _signals[idx]]); | 115 _ports[idx].send([_signals[idx], satisfiedSignals & _signals[idx]]); |
| 118 } | 116 } |
| 119 | 117 |
| 120 void _handleControlMessage() { | 118 void _handleControlMessage() { |
| 121 List result = _MojoHandleWatcherNatives.recvControlData(_controlHandle); | 119 List result = MojoHandleWatcherNatives.recvControlData(_controlHandle); |
| 122 // result[0] = mojo handle if any, or a timer deadline in milliseconds. | 120 // result[0] = mojo handle if any, or a timer deadline in milliseconds. |
| 123 // result[1] = SendPort if any. | 121 // result[1] = SendPort if any. |
| 124 // result[2] = command << 2 | WRITABLE | READABLE | 122 // result[2] = command << 2 | WRITABLE | READABLE |
| 125 | 123 |
| 126 var signals = _decodeSignals(result[2]); | 124 var signals = _decodeSignals(result[2]); |
| 127 int command = _decodeCommand(result[2]); | 125 int command = _decodeCommand(result[2]); |
| 128 switch (command) { | 126 switch (command) { |
| 129 case ADD: | 127 case ADD: |
| 130 _addHandle(result[0], result[1], signals); | 128 _addHandle(result[0], result[1], signals); |
| 131 break; | 129 break; |
| 132 case REMOVE: | 130 case REMOVE: |
| 133 _removeHandle(result[0]); | 131 _removeHandle(result[0]); |
| 134 break; | 132 break; |
| 135 case CLOSE: | 133 case CLOSE: |
| 136 _close(result[0], result[1]); | 134 _close(result[0], result[1]); |
| 137 break; | 135 break; |
| 138 case TIMER: | 136 case TIMER: |
| 139 _timer(result[1], result[0]); | 137 _timer(result[1], result[0]); |
| 140 break; | 138 break; |
| 141 case SHUTDOWN: | 139 case SHUTDOWN: |
| 142 _shutdownHandleWatcher(result[1]); | 140 _shutdownHandleWatcher(result[1]); |
| 143 break; | 141 break; |
| 144 default: | 142 default: |
| 145 throw "Invalid Command: $command"; | 143 throw "Invalid Command: $command"; |
| 146 break; | 144 break; |
| 147 } | 145 } |
| 148 } | 146 } |
| 149 | 147 |
| 150 void _addHandle(int mojoHandle, SendPort port, MojoHandleSignals signals) { | 148 void _addHandle(int mojoHandle, SendPort port, int signals) { |
| 151 int idx = _handleIndices[mojoHandle]; | 149 int idx = _handleIndices[mojoHandle]; |
| 152 if (idx == null) { | 150 if (idx == null) { |
| 153 _handles.add(mojoHandle); | 151 _handles.add(mojoHandle); |
| 154 _ports.add(port); | 152 _ports.add(port); |
| 155 _signals.add(signals.value); | 153 _signals.add(signals); |
| 156 _handleIndices[mojoHandle] = _handleCount; | 154 _handleIndices[mojoHandle] = _handleCount; |
| 157 _handleCount++; | 155 _handleCount++; |
| 158 } else { | 156 } else { |
| 159 assert(_ports[idx] == port); | 157 assert(_ports[idx] == port); |
| 160 assert(_handles[idx] == mojoHandle); | 158 assert(_handles[idx] == mojoHandle); |
| 161 _signals[idx] |= signals.value; | 159 _signals[idx] |= signals; |
| 162 } | 160 } |
| 163 } | 161 } |
| 164 | 162 |
| 165 void _removeHandle(int mojoHandle) { | 163 void _removeHandle(int mojoHandle) { |
| 166 int idx = _handleIndices[mojoHandle]; | 164 int idx = _handleIndices[mojoHandle]; |
| 167 if (idx == null) { | 165 if (idx == null) { |
| 168 throw "Remove on a non-existent handle: idx = $idx."; | 166 throw "Remove on a non-existent handle: idx = $idx."; |
| 169 } | 167 } |
| 170 if (idx == 0) { | 168 if (idx == 0) { |
| 171 throw "The control handle (idx = 0) cannot be removed."; | 169 throw "The control handle (idx = 0) cannot be removed."; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 193 } | 191 } |
| 194 | 192 |
| 195 void _close(int mojoHandle, SendPort port, {bool pruning: false}) { | 193 void _close(int mojoHandle, SendPort port, {bool pruning: false}) { |
| 196 assert(!pruning || (port == null)); | 194 assert(!pruning || (port == null)); |
| 197 int idx = _handleIndices[mojoHandle]; | 195 int idx = _handleIndices[mojoHandle]; |
| 198 if (idx == null) { | 196 if (idx == null) { |
| 199 // An app isolate may request that the handle watcher close a handle that | 197 // An app isolate may request that the handle watcher close a handle that |
| 200 // has already been pruned. This happens when the app isolate has not yet | 198 // has already been pruned. This happens when the app isolate has not yet |
| 201 // received the PEER_CLOSED event. The app isolate will not close the | 199 // received the PEER_CLOSED event. The app isolate will not close the |
| 202 // handle, so we must do so here. | 200 // handle, so we must do so here. |
| 203 _tempHandle._set(mojoHandle); | 201 MojoHandleNatives.close(mojoHandle); |
| 204 _tempHandle.close(); | |
| 205 if (port != null) port.send(null); // Notify that close is done. | 202 if (port != null) port.send(null); // Notify that close is done. |
| 206 return; | 203 return; |
| 207 } | 204 } |
| 208 if (idx == 0) { | 205 if (idx == 0) { |
| 209 throw "The control handle (idx = 0) cannot be closed."; | 206 throw "The control handle (idx = 0) cannot be closed."; |
| 210 } | 207 } |
| 211 _tempHandle._set(_handles[idx]); | 208 MojoHandleNatives.close(_handles[idx]); |
| 212 _tempHandle.close(); | |
| 213 if (port != null) port.send(null); // Notify that close is done. | 209 if (port != null) port.send(null); // Notify that close is done. |
| 214 if (pruning) { | 210 if (pruning) { |
| 215 // If this handle is being pruned, notify the application isolate | 211 // If this handle is being pruned, notify the application isolate |
| 216 // by sending MojoHandleSignals.PEER_CLOSED. | 212 // by sending MojoHandleSignals.PEER_CLOSED. |
| 217 _ports[idx].send([_signals[idx], MojoHandleSignals.kPeerClosed]); | 213 _ports[idx].send([_signals[idx], kMojoSignalsPeerClosed]); |
| 218 } | 214 } |
| 219 _removeHandle(mojoHandle); | 215 _removeHandle(mojoHandle); |
| 220 } | 216 } |
| 221 | 217 |
| 222 // Returns the next timer deadline in units of microseconds from 'now'. | 218 // Returns the next timer deadline in units of microseconds from 'now'. |
| 223 int _processTimerDeadlines() { | 219 int _processTimerDeadlines() { |
| 224 int now = (new DateTime.now()).millisecondsSinceEpoch; | 220 int now = (new DateTime.now()).millisecondsSinceEpoch; |
| 225 while (_timerQueue.hasTimer && (now >= _timerQueue.currentTimeout)) { | 221 while (_timerQueue.hasTimer && (now >= _timerQueue.currentTimeout)) { |
| 226 _timerQueue.currentPort.send(null); | 222 _timerQueue.currentPort.send(null); |
| 227 _timerQueue.removeCurrent(); | 223 _timerQueue.removeCurrent(); |
| 228 now = (new DateTime.now()).millisecondsSinceEpoch; | 224 now = (new DateTime.now()).millisecondsSinceEpoch; |
| 229 } | 225 } |
| 230 return _timerQueue.hasTimer | 226 return _timerQueue.hasTimer |
| 231 ? (_timerQueue.currentTimeout - now) * 1000 | 227 ? (_timerQueue.currentTimeout - now) * 1000 |
| 232 : MojoHandle.DEADLINE_INDEFINITE; | 228 : kDeadlineIndefinite; |
| 233 } | 229 } |
| 234 | 230 |
| 235 void _timer(SendPort port, int deadline) { | 231 void _timer(SendPort port, int deadline) { |
| 236 _timerQueue.updateTimer(port, deadline); | 232 _timerQueue.updateTimer(port, deadline); |
| 237 } | 233 } |
| 238 | 234 |
| 239 void _pruneClosedHandles(List<MojoHandleSignalsState> states) { | 235 void _pruneClosedHandles(List<List<int>> states) { |
| 240 List<int> closed = new List(); | 236 List<int> closed = new List(); |
| 241 for (var i = 0; i < _handles.length; i++) { | 237 for (var i = 0; i < _handles.length; i++) { |
| 242 if (states != null) { | 238 if (states != null) { |
| 243 var signals = new MojoHandleSignals(states[i].satisfied_signals); | 239 int signals = states[i][0]; |
| 244 if (signals.isPeerClosed) { | 240 if ((signals & kMojoSignalsPeerClosed) != 0) { |
| 245 closed.add(_handles[i]); | 241 closed.add(_handles[i]); |
| 246 } | 242 } |
| 247 } else { | 243 } else { |
| 248 _tempHandle._set(_handles[i]); | 244 List mwr = MojoHandleNatives.wait(_handles[i], kMojoSignalsAll, 0); |
| 249 MojoWaitResult mwr = _tempHandle.wait(MojoHandleSignals.kAll, 0); | 245 if ((mwr[0] != kMojoResultOk) && |
| 250 if ((!mwr.result.isOk) && (!mwr.result.isDeadlineExceeded)) { | 246 (mwr[0] != kMojoResultDeadlineExceeded)) { |
| 251 closed.add(_handles[i]); | 247 closed.add(_handles[i]); |
| 252 } | 248 } |
| 253 _tempHandle._set(MojoHandle.INVALID); | |
| 254 } | 249 } |
| 255 } | 250 } |
| 256 for (var h in closed) { | 251 for (var h in closed) { |
| 257 _close(h, null, pruning: true); | 252 _close(h, null, pruning: true); |
| 258 } | 253 } |
| 259 // '_close' updated the '_handles' array, so at this point the '_handles' | 254 // '_close' updated the '_handles' array, so at this point the '_handles' |
| 260 // array and the caller's 'states' array are mismatched. | 255 // array and the caller's 'states' array are mismatched. |
| 261 } | 256 } |
| 262 | 257 |
| 263 void _shutdownHandleWatcher(SendPort shutdownSendPort) { | 258 void _shutdownHandleWatcher(SendPort shutdownSendPort) { |
| 264 _shutdown = true; | 259 _shutdown = true; |
| 265 _tempHandle._set(_controlHandle); | 260 MojoHandleNatives.close(_controlHandle); |
| 266 _tempHandle.close(); | |
| 267 shutdownSendPort.send(null); | 261 shutdownSendPort.send(null); |
| 268 } | 262 } |
| 269 | 263 |
| 270 static MojoResult _sendControlData( | 264 static int _sendControlData(int rawHandle, SendPort port, int data) { |
| 271 MojoHandle mojoHandle, SendPort port, int data) { | 265 int controlHandle = MojoHandleWatcherNatives.getControlHandle(); |
| 272 int controlHandle = _MojoHandleWatcherNatives.getControlHandle(); | 266 if (controlHandle == kMojoHandleInvalid) { |
| 273 if (controlHandle == MojoHandle.INVALID) { | 267 return kMojoResultFailedPrecondition; |
| 274 return MojoResult.FAILED_PRECONDITION; | |
| 275 } | 268 } |
| 276 | 269 |
| 277 int rawHandle = MojoHandle.INVALID; | 270 var result = MojoHandleWatcherNatives.sendControlData( |
| 278 if (mojoHandle != null) { | |
| 279 rawHandle = mojoHandle.h; | |
| 280 } | |
| 281 var result = _MojoHandleWatcherNatives.sendControlData( | |
| 282 controlHandle, rawHandle, port, data); | 271 controlHandle, rawHandle, port, data); |
| 283 return new MojoResult(result); | 272 return result; |
| 284 } | 273 } |
| 285 | 274 |
| 286 // Starts up the MojoHandleWatcher isolate. Should be called only once | 275 // Starts up the MojoHandleWatcher isolate. Should be called only once |
| 287 // per VM process. | 276 // per VM process. |
| 288 static Future<Isolate> _start() { | 277 static Future<Isolate> _start() { |
| 289 // Make a control message pipe, | 278 // Make a control message pipe, |
| 290 MojoMessagePipe pipe = new MojoMessagePipe(); | 279 List pipeEndpoints = MojoMessagePipeNatives.MojoCreateMessagePipe(0); |
| 291 int consumerHandle = pipe.endpoints[0].handle.h; | 280 assert(pipeEndpoints != null); |
| 292 int producerHandle = pipe.endpoints[1].handle.h; | 281 assert((pipeEndpoints is List) && (pipeEndpoints.length == 3)); |
| 282 assert(pipeEndpoints[0] == kMojoResultOk); |
| 283 |
| 284 int consumerHandle = pipeEndpoints[1]; |
| 285 int producerHandle = pipeEndpoints[2]; |
| 293 | 286 |
| 294 // Call setControlHandle with the other end. | 287 // Call setControlHandle with the other end. |
| 295 assert(producerHandle != MojoHandle.INVALID); | 288 assert(producerHandle != kMojoHandleInvalid); |
| 296 _MojoHandleWatcherNatives.setControlHandle(producerHandle); | 289 MojoHandleWatcherNatives.setControlHandle(producerHandle); |
| 297 | 290 |
| 298 // Spawn the handle watcher isolate with the MojoHandleWatcher, | 291 // Spawn the handle watcher isolate with the MojoHandleWatcher, |
| 299 return Isolate.spawn(_handleWatcherIsolate, consumerHandle); | 292 return Isolate.spawn(_handleWatcherIsolate, consumerHandle); |
| 300 } | 293 } |
| 301 | 294 |
| 302 // Causes the MojoHandleWatcher isolate to exit. Should be called only | 295 // Causes the MojoHandleWatcher isolate to exit. Should be called only |
| 303 // once per VM process. | 296 // once per VM process. |
| 304 static void _stop() { | 297 static void _stop() { |
| 305 // Create a port for notification that the handle watcher has shutdown. | 298 // Create a port for notification that the handle watcher has shutdown. |
| 306 var shutdownReceivePort = new ReceivePort(); | 299 var shutdownReceivePort = new ReceivePort(); |
| 307 var shutdownSendPort = shutdownReceivePort.sendPort; | 300 var shutdownSendPort = shutdownReceivePort.sendPort; |
| 308 | 301 |
| 309 // Send the shutdown command. | 302 // Send the shutdown command. |
| 310 _sendControlData(null, shutdownSendPort, _encodeCommand(SHUTDOWN)); | 303 _sendControlData( |
| 304 kMojoHandleInvalid, shutdownSendPort, _encodeCommand(SHUTDOWN)); |
| 311 | 305 |
| 312 // Close the control handle. | 306 // Close the control handle. |
| 313 int controlHandle = _MojoHandleWatcherNatives.getControlHandle(); | 307 int controlHandle = MojoHandleWatcherNatives.getControlHandle(); |
| 314 var handle = new MojoHandle(controlHandle); | 308 MojoHandleNatives.close(controlHandle); |
| 315 handle.close(); | |
| 316 | 309 |
| 317 // Invalidate the control handle. | 310 // Invalidate the control handle. |
| 318 _MojoHandleWatcherNatives.setControlHandle(MojoHandle.INVALID); | 311 MojoHandleWatcherNatives.setControlHandle(kMojoHandleInvalid); |
| 319 | 312 |
| 320 // Wait for the handle watcher isolate to exit. | 313 // Wait for the handle watcher isolate to exit. |
| 321 shutdownReceivePort.first.then((_) { | 314 shutdownReceivePort.first.then((_) { |
| 322 shutdownReceivePort.close(); | 315 shutdownReceivePort.close(); |
| 323 }); | 316 }); |
| 324 } | 317 } |
| 325 | 318 |
| 326 // If wait is true, returns a future that resolves only after the handle | 319 // If wait is true, returns a future that resolves only after the handle |
| 327 // has actually been closed by the handle watcher. Otherwise, returns a | 320 // has actually been closed by the handle watcher. Otherwise, returns a |
| 328 // future that resolves immediately. | 321 // future that resolves immediately. |
| 329 static Future<MojoResult> close(MojoHandle mojoHandle, {bool wait: false}) { | 322 static Future<int> close(int mojoHandle, {bool wait: false}) { |
| 330 assert(MojoHandle._removeUnclosedHandle(mojoHandle)); | 323 //assert(MojoHandle._removeUnclosedHandle(mojoHandle)); |
| 331 if (!wait) { | 324 if (!wait) { |
| 332 return new Future.value( | 325 return new Future.value( |
| 333 _sendControlData(mojoHandle, null, _encodeCommand(CLOSE))); | 326 _sendControlData(mojoHandle, null, _encodeCommand(CLOSE))); |
| 334 } | 327 } |
| 335 MojoResult result; | 328 int result; |
| 336 var completer = new Completer(); | 329 var completer = new Completer(); |
| 337 var rawPort = new RawReceivePort((_) { | 330 var rawPort = new RawReceivePort((_) { |
| 338 completer.complete(result); | 331 completer.complete(result); |
| 339 }); | 332 }); |
| 340 result = | 333 result = |
| 341 _sendControlData(mojoHandle, rawPort.sendPort, _encodeCommand(CLOSE)); | 334 _sendControlData(mojoHandle, rawPort.sendPort, _encodeCommand(CLOSE)); |
| 342 return completer.future.then((r) { | 335 return completer.future.then((r) { |
| 343 rawPort.close(); | 336 rawPort.close(); |
| 344 return r; | 337 return r; |
| 345 }); | 338 }); |
| 346 } | 339 } |
| 347 | 340 |
| 348 static MojoResult add(MojoHandle mojoHandle, SendPort port, int signals) { | 341 static int add(int mojoHandle, SendPort port, int signals) { |
| 349 return _sendControlData(mojoHandle, port, _encodeCommand(ADD, signals)); | 342 return _sendControlData(mojoHandle, port, _encodeCommand(ADD, signals)); |
| 350 } | 343 } |
| 351 | 344 |
| 352 static MojoResult remove(MojoHandle mojoHandle) { | 345 static int remove(int mojoHandle) { |
| 353 return _sendControlData(mojoHandle, null, _encodeCommand(REMOVE)); | 346 return _sendControlData(mojoHandle, null, _encodeCommand(REMOVE)); |
| 354 } | 347 } |
| 355 | 348 |
| 356 static MojoResult timer(Object ignored, SendPort port, int deadline) { | 349 static int timer(Object ignored, SendPort port, int deadline) { |
| 357 // The deadline will be unwrapped before sending to the handle watcher. | 350 // The deadline will be unwrapped before sending to the handle watcher. |
| 358 return _sendControlData( | 351 return _sendControlData(deadline, port, _encodeCommand(TIMER)); |
| 359 new MojoHandle._internal(deadline), port, _encodeCommand(TIMER)); | |
| 360 } | 352 } |
| 361 } | 353 } |
| OLD | NEW |