John Grossman | c7f57c6 | 2012-06-26 12:50:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "utils.h" |
| 18 | |
| 19 | namespace android { |
| 20 | |
| 21 | void Timeout::setTimeout(int msec) { |
| 22 | if (msec < 0) { |
| 23 | mSystemEndTime = 0; |
| 24 | return; |
| 25 | } |
| 26 | |
| 27 | mSystemEndTime = systemTime() + (static_cast<nsecs_t>(msec) * 1000000); |
| 28 | } |
| 29 | |
| 30 | int Timeout::msecTillTimeout(nsecs_t nowTime) { |
| 31 | if (!mSystemEndTime) { |
| 32 | return -1; |
| 33 | } |
| 34 | |
| 35 | if (mSystemEndTime < nowTime) { |
| 36 | return 0; |
| 37 | } |
| 38 | |
| 39 | nsecs_t delta = mSystemEndTime - nowTime; |
| 40 | delta += 999999; |
| 41 | delta /= 1000000; |
| 42 | if (delta > 0x7FFFFFFF) { |
| 43 | return 0x7FFFFFFF; |
| 44 | } |
| 45 | |
| 46 | return static_cast<int>(delta); |
| 47 | } |
| 48 | |
| 49 | } // namespace android |