OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 part of core; | |
6 | |
7 class _HandleCreationRecord { | |
8 final MojoHandle handle; | |
9 final StackTrace stack; | |
10 _HandleCreationRecord(this.handle, this.stack); | |
11 } | |
12 | |
13 class MojoHandle { | |
14 static const int INVALID = 0; | |
15 static const int DEADLINE_INDEFINITE = -1; | |
16 | |
17 int _h; | |
18 int get h => _h; | |
19 | |
20 MojoHandle(this._h) { | |
21 assert(_addUnclosedHandle(this)); | |
22 } | |
23 | |
24 MojoHandle._internal(this._h); | |
25 | |
26 MojoHandle.invalid() : this._internal(INVALID); | |
27 | |
28 MojoResult close() { | |
29 assert(_removeUnclosedHandle(this)); | |
30 int result = MojoHandleNatives.close(_h); | |
31 _h = INVALID; | |
32 return new MojoResult(result); | |
33 } | |
34 | |
35 MojoHandle pass() { | |
36 assert(_removeUnclosedHandle(this)); | |
37 return this; | |
38 } | |
39 | |
40 MojoWaitResult wait(int signals, int deadline) { | |
41 List result = MojoHandleNatives.wait(h, signals, deadline); | |
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); | |
46 } | |
47 | |
48 bool _ready(MojoHandleSignals signal) { | |
49 MojoWaitResult mwr = wait(signal.value, 0); | |
50 switch (mwr.result) { | |
51 case MojoResult.OK: | |
52 return true; | |
53 case MojoResult.DEADLINE_EXCEEDED: | |
54 case MojoResult.CANCELLED: | |
55 case MojoResult.INVALID_ARGUMENT: | |
56 case MojoResult.FAILED_PRECONDITION: | |
57 return false; | |
58 default: | |
59 // Should be unreachable. | |
60 throw "Unexpected result $mwr for wait on $h"; | |
61 } | |
62 } | |
63 | |
64 void _set(int value) { | |
65 _h = value; | |
66 } | |
67 | |
68 bool get readyRead => _ready(MojoHandleSignals.PEER_CLOSED_READABLE); | |
69 bool get readyWrite => _ready(MojoHandleSignals.WRITABLE); | |
70 bool get isValid => (_h != INVALID); | |
71 | |
72 String toString() { | |
73 if (!isValid) { | |
74 return "MojoHandle(INVALID)"; | |
75 } | |
76 var mwr = wait(MojoHandleSignals.kAll, 0); | |
77 return "MojoHandle(h: $h, status: $mwr)"; | |
78 } | |
79 | |
80 bool operator ==(MojoHandle other) { | |
81 return _h == other._h; | |
82 } | |
83 | |
84 static MojoWaitManyResult waitMany( | |
85 List<int> handles, List<int> signals, int 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; | |
90 return new MojoWaitManyResult(new MojoResult(result[0]), result[1], states); | |
91 } | |
92 | |
93 static MojoResult register(MojoEventStream eventStream) { | |
94 return new MojoResult( | |
95 MojoHandleNatives.register(eventStream, eventStream._handle.h)); | |
96 } | |
97 | |
98 static HashMap<int, _HandleCreationRecord> _unclosedHandles = new HashMap(); | |
99 | |
100 // _addUnclosedHandle(), _removeUnclosedHandle(), and dumpLeakedHandles() | |
101 // should only be used inside of assert() statements. | |
102 static bool _addUnclosedHandle(MojoHandle handle) { | |
103 var stack; | |
104 try { | |
105 assert(false); | |
106 } catch (_, s) { | |
107 stack = s; | |
108 } | |
109 | |
110 var handleCreate = new _HandleCreationRecord(handle, stack); | |
111 _unclosedHandles[handle.h] = handleCreate; | |
112 return true; | |
113 } | |
114 | |
115 static bool _removeUnclosedHandle(MojoHandle handle) { | |
116 _unclosedHandles.remove(handle._h); | |
117 return true; | |
118 } | |
119 | |
120 static bool reportLeakedHandles() { | |
121 var noleaks = true; | |
122 for (var handle in MojoHandle._unclosedHandles.keys) { | |
123 var handleCreation = MojoHandle._unclosedHandles[handle]; | |
124 if (handleCreation != null) { | |
125 print("HANDLE LEAK: handle: $handle, created at:"); | |
126 print("${handleCreation.stack}"); | |
127 noleaks = false; | |
128 } | |
129 } | |
130 return noleaks; | |
131 } | |
132 } | |
OLD | NEW |