| 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 core; |
| 6 | 6 |
| 7 class _MojoHandleNatives { | |
| 8 static int register(MojoEventStream eventStream) native "MojoHandle_Register"; | |
| 9 static int close(int handle) native "MojoHandle_Close"; | |
| 10 static List wait( | |
| 11 int handle, int signals, int deadline) native "MojoHandle_Wait"; | |
| 12 static List waitMany(List<int> handles, List<int> signals, | |
| 13 int deadline) native "MojoHandle_WaitMany"; | |
| 14 } | |
| 15 | |
| 16 class _HandleCreationRecord { | 7 class _HandleCreationRecord { |
| 17 final MojoHandle handle; | 8 final MojoHandle handle; |
| 18 final StackTrace stack; | 9 final StackTrace stack; |
| 19 _HandleCreationRecord(this.handle, this.stack); | 10 _HandleCreationRecord(this.handle, this.stack); |
| 20 } | 11 } |
| 21 | 12 |
| 22 class MojoHandle { | 13 class MojoHandle { |
| 23 static const int INVALID = 0; | 14 static const int INVALID = 0; |
| 24 static const int DEADLINE_INDEFINITE = -1; | 15 static const int DEADLINE_INDEFINITE = -1; |
| 25 | 16 |
| 26 int _h; | 17 int _h; |
| 27 int get h => _h; | 18 int get h => _h; |
| 28 | 19 |
| 29 MojoHandle(this._h) { | 20 MojoHandle(this._h) { |
| 30 assert(_addUnclosedHandle(this)); | 21 assert(_addUnclosedHandle(this)); |
| 31 } | 22 } |
| 32 | 23 |
| 33 MojoHandle._internal(this._h); | 24 MojoHandle._internal(this._h); |
| 34 | 25 |
| 35 MojoHandle.invalid() : this._internal(INVALID); | 26 MojoHandle.invalid() : this._internal(INVALID); |
| 36 | 27 |
| 37 MojoResult close() { | 28 MojoResult close() { |
| 38 assert(_removeUnclosedHandle(this)); | 29 assert(_removeUnclosedHandle(this)); |
| 39 int result = _MojoHandleNatives.close(_h); | 30 int result = MojoHandleNatives.close(_h); |
| 40 _h = INVALID; | 31 _h = INVALID; |
| 41 return new MojoResult(result); | 32 return new MojoResult(result); |
| 42 } | 33 } |
| 43 | 34 |
| 44 MojoHandle pass() { | 35 MojoHandle pass() { |
| 45 assert(_removeUnclosedHandle(this)); | 36 assert(_removeUnclosedHandle(this)); |
| 46 return this; | 37 return this; |
| 47 } | 38 } |
| 48 | 39 |
| 49 MojoWaitResult wait(int signals, int deadline) { | 40 MojoWaitResult wait(int signals, int deadline) { |
| 50 List result = _MojoHandleNatives.wait(h, signals, deadline); | 41 List result = MojoHandleNatives.wait(h, signals, deadline); |
| 51 return new MojoWaitResult(new MojoResult(result[0]), result[1]); | 42 var state = result[1] != null |
| 43 ? new MojoHandleSignalsState(result[1][0], result[1][1]) |
| 44 : null; |
| 45 return new MojoWaitResult(new MojoResult(result[0]), state); |
| 52 } | 46 } |
| 53 | 47 |
| 54 bool _ready(MojoHandleSignals signal) { | 48 bool _ready(MojoHandleSignals signal) { |
| 55 MojoWaitResult mwr = wait(signal.value, 0); | 49 MojoWaitResult mwr = wait(signal.value, 0); |
| 56 switch (mwr.result) { | 50 switch (mwr.result) { |
| 57 case MojoResult.OK: | 51 case MojoResult.OK: |
| 58 return true; | 52 return true; |
| 59 case MojoResult.DEADLINE_EXCEEDED: | 53 case MojoResult.DEADLINE_EXCEEDED: |
| 60 case MojoResult.CANCELLED: | 54 case MojoResult.CANCELLED: |
| 61 case MojoResult.INVALID_ARGUMENT: | 55 case MojoResult.INVALID_ARGUMENT: |
| (...skipping 20 matching lines...) Expand all Loading... |
| 82 var mwr = wait(MojoHandleSignals.kAll, 0); | 76 var mwr = wait(MojoHandleSignals.kAll, 0); |
| 83 return "MojoHandle(h: $h, status: $mwr)"; | 77 return "MojoHandle(h: $h, status: $mwr)"; |
| 84 } | 78 } |
| 85 | 79 |
| 86 bool operator ==(MojoHandle other) { | 80 bool operator ==(MojoHandle other) { |
| 87 return _h == other._h; | 81 return _h == other._h; |
| 88 } | 82 } |
| 89 | 83 |
| 90 static MojoWaitManyResult waitMany( | 84 static MojoWaitManyResult waitMany( |
| 91 List<int> handles, List<int> signals, int deadline) { | 85 List<int> handles, List<int> signals, int deadline) { |
| 92 List result = _MojoHandleNatives.waitMany(handles, signals, deadline); | 86 List result = MojoHandleNatives.waitMany(handles, signals, deadline); |
| 87 List states = result[2] != null |
| 88 ? result[2].map((l) => new MojoHandleSignalsState(l[0], l[1])).toList() |
| 89 : null; |
| 93 return new MojoWaitManyResult( | 90 return new MojoWaitManyResult( |
| 94 new MojoResult(result[0]), result[1], result[2]); | 91 new MojoResult(result[0]), result[1], states); |
| 95 } | 92 } |
| 96 | 93 |
| 97 static MojoResult register(MojoEventStream eventStream) { | 94 static MojoResult register(MojoEventStream eventStream) { |
| 98 return new MojoResult(_MojoHandleNatives.register(eventStream)); | 95 return new MojoResult(MojoHandleNatives.register( |
| 96 eventStream, eventStream._handle.h)); |
| 99 } | 97 } |
| 100 | 98 |
| 101 static HashMap<int, _HandleCreationRecord> _unclosedHandles = new HashMap(); | 99 static HashMap<int, _HandleCreationRecord> _unclosedHandles = new HashMap(); |
| 102 | 100 |
| 103 // _addUnclosedHandle(), _removeUnclosedHandle(), and dumpLeakedHandles() | 101 // _addUnclosedHandle(), _removeUnclosedHandle(), and dumpLeakedHandles() |
| 104 // should only be used inside of assert() statements. | 102 // should only be used inside of assert() statements. |
| 105 static bool _addUnclosedHandle(MojoHandle handle) { | 103 static bool _addUnclosedHandle(MojoHandle handle) { |
| 106 var stack; | 104 var stack; |
| 107 try { | 105 try { |
| 108 assert(false); | 106 assert(false); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 126 var handleCreation = MojoHandle._unclosedHandles[handle]; | 124 var handleCreation = MojoHandle._unclosedHandles[handle]; |
| 127 if (handleCreation != null) { | 125 if (handleCreation != null) { |
| 128 print("HANDLE LEAK: handle: $handle, created at:"); | 126 print("HANDLE LEAK: handle: $handle, created at:"); |
| 129 print("${handleCreation.stack}"); | 127 print("${handleCreation.stack}"); |
| 130 noleaks = false; | 128 noleaks = false; |
| 131 } | 129 } |
| 132 } | 130 } |
| 133 return noleaks; | 131 return noleaks; |
| 134 } | 132 } |
| 135 } | 133 } |
| OLD | NEW |