OLD | NEW |
1 // Copyright 2014 The Crashpad Authors. All rights reserved. | 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
13 // limitations under the License. | 13 // limitations under the License. |
14 | 14 |
15 #include "util/mach/mach_message.h" | 15 #include "util/mach/mach_message.h" |
16 | 16 |
17 #include <limits> | 17 #include <limits> |
18 | 18 |
19 #include "base/basictypes.h" | 19 #include "base/basictypes.h" |
20 #include "util/misc/clock.h" | 20 #include "util/misc/clock.h" |
21 | 21 |
22 namespace crashpad { | 22 namespace crashpad { |
23 | 23 |
24 namespace { | 24 namespace { |
25 | 25 |
26 const int kNanosecondsPerMillisecond = 1E6; | 26 const int kNanosecondsPerMillisecond = 1E6; |
27 | 27 |
28 // TimerRunning() determines whether |deadline| has passed. If |deadline| is | 28 // TimerRunning() determines whether |deadline| has passed. If |deadline| is |
29 // kMachMessageWaitIndefinitely, |*timeout_options| is set to | 29 // kMachMessageDeadlineWaitIndefinitely, |*timeout_options| is set to |
30 // MACH_MSG_OPTION_NONE, |*remaining_ms| is set to MACH_MSG_TIMEOUT_NONE, and | 30 // MACH_MSG_OPTION_NONE, |*remaining_ms| is set to MACH_MSG_TIMEOUT_NONE, and |
31 // this function returns true. When used with mach_msg(), this will cause | 31 // this function returns true. When used with mach_msg(), this will cause |
32 // indefinite waiting. In any other case, |*timeout_options| is set to | 32 // indefinite waiting. In any other case, |*timeout_options| is set to |
33 // MACH_SEND_TIMEOUT | MACH_RCV_TIMEOUT, so mach_msg() will enforce a timeout | 33 // MACH_SEND_TIMEOUT | MACH_RCV_TIMEOUT, so mach_msg() will enforce a timeout |
34 // specified by |*remaining_ms|. If |deadline| is in the future, |*remaining_ms| | 34 // specified by |*remaining_ms|. If |deadline| is in the future, |*remaining_ms| |
35 // is set to the number of milliseconds remaining, which will always be a | 35 // is set to the number of milliseconds remaining, which will always be a |
36 // positive value, and this function returns true. If |deadline| is | 36 // positive value, and this function returns true. If |deadline| is |
37 // kMachMessageNonblocking (indicating that no timer is in effect), | 37 // kMachMessageDeadlineNonblocking (indicating that no timer is in effect), |
38 // |*remaining_ms| is set to zero and this function returns true. Otherwise, | 38 // |*remaining_ms| is set to zero and this function returns true. Otherwise, |
39 // this function sets |*remaining_ms| to zero and returns false. | 39 // this function sets |*remaining_ms| to zero and returns false. |
40 bool TimerRunning(uint64_t deadline, | 40 bool TimerRunning(uint64_t deadline, |
41 mach_msg_timeout_t* remaining_ms, | 41 mach_msg_timeout_t* remaining_ms, |
42 mach_msg_option_t* timeout_options) { | 42 mach_msg_option_t* timeout_options) { |
43 if (deadline == kMachMessageWaitIndefinitely) { | 43 if (deadline == kMachMessageDeadlineWaitIndefinitely) { |
44 *remaining_ms = MACH_MSG_TIMEOUT_NONE; | 44 *remaining_ms = MACH_MSG_TIMEOUT_NONE; |
45 *timeout_options = MACH_MSG_OPTION_NONE; | 45 *timeout_options = MACH_MSG_OPTION_NONE; |
46 return true; | 46 return true; |
47 } | 47 } |
48 | 48 |
49 *timeout_options = MACH_SEND_TIMEOUT | MACH_RCV_TIMEOUT; | 49 *timeout_options = MACH_SEND_TIMEOUT | MACH_RCV_TIMEOUT; |
50 | 50 |
51 if (deadline == kMachMessageNonblocking) { | 51 if (deadline == kMachMessageDeadlineNonblocking) { |
52 *remaining_ms = 0; | 52 *remaining_ms = 0; |
53 return true; | 53 return true; |
54 } | 54 } |
55 | 55 |
56 uint64_t now = ClockMonotonicNanoseconds(); | 56 uint64_t now = ClockMonotonicNanoseconds(); |
57 | 57 |
58 if (now >= deadline) { | 58 if (now >= deadline) { |
59 *remaining_ms = 0; | 59 *remaining_ms = 0; |
60 } else { | 60 } else { |
61 uint64_t remaining = deadline - now; | 61 uint64_t remaining = deadline - now; |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 receive_size, | 110 receive_size, |
111 receive_port, | 111 receive_port, |
112 remaining_ms, | 112 remaining_ms, |
113 notify_port); | 113 notify_port); |
114 } | 114 } |
115 | 115 |
116 } // namespace | 116 } // namespace |
117 | 117 |
118 MachMessageDeadline MachMessageDeadlineFromTimeout( | 118 MachMessageDeadline MachMessageDeadlineFromTimeout( |
119 mach_msg_timeout_t timeout_ms) { | 119 mach_msg_timeout_t timeout_ms) { |
120 if (timeout_ms == 0) { | 120 switch (timeout_ms) { |
121 return kMachMessageNonblocking; | 121 case kMachMessageTimeoutNonblocking: |
| 122 return kMachMessageDeadlineNonblocking; |
| 123 case kMachMessageTimeoutWaitIndefinitely: |
| 124 return kMachMessageDeadlineWaitIndefinitely; |
| 125 default: |
| 126 return ClockMonotonicNanoseconds() + |
| 127 implicit_cast<uint64_t>(timeout_ms) * kNanosecondsPerMillisecond; |
122 } | 128 } |
123 | |
124 return ClockMonotonicNanoseconds() + | |
125 implicit_cast<uint64_t>(timeout_ms) * kNanosecondsPerMillisecond; | |
126 } | 129 } |
127 | 130 |
128 mach_msg_return_t MachMessageWithDeadline(mach_msg_header_t* message, | 131 mach_msg_return_t MachMessageWithDeadline(mach_msg_header_t* message, |
129 mach_msg_option_t options, | 132 mach_msg_option_t options, |
130 mach_msg_size_t receive_size, | 133 mach_msg_size_t receive_size, |
131 mach_port_name_t receive_port, | 134 mach_port_name_t receive_port, |
132 MachMessageDeadline deadline, | 135 MachMessageDeadline deadline, |
133 mach_port_name_t notify_port, | 136 mach_port_name_t notify_port, |
134 bool run_even_if_expired) { | 137 bool run_even_if_expired) { |
135 // mach_msg() actaully does return MACH_MSG_SUCCESS when not asked to send or | 138 // mach_msg() actaully does return MACH_MSG_SUCCESS when not asked to send or |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 } | 207 } |
205 | 208 |
206 const mach_msg_trailer_t* MachMessageTrailerFromHeader( | 209 const mach_msg_trailer_t* MachMessageTrailerFromHeader( |
207 const mach_msg_header_t* header) { | 210 const mach_msg_header_t* header) { |
208 vm_address_t header_address = reinterpret_cast<vm_address_t>(header); | 211 vm_address_t header_address = reinterpret_cast<vm_address_t>(header); |
209 vm_address_t trailer_address = header_address + round_msg(header->msgh_size); | 212 vm_address_t trailer_address = header_address + round_msg(header->msgh_size); |
210 return reinterpret_cast<const mach_msg_trailer_t*>(trailer_address); | 213 return reinterpret_cast<const mach_msg_trailer_t*>(trailer_address); |
211 } | 214 } |
212 | 215 |
213 } // namespace crashpad | 216 } // namespace crashpad |
OLD | NEW |