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 #include <errno.h> |
| 6 #include <time.h> |
| 7 |
| 8 #include "components/nacl/loader/nonsfi/abi_conversion.h" |
| 9 #include "components/nacl/loader/nonsfi/irt_interfaces.h" |
| 10 #include "native_client/src/trusted/service_runtime/include/sys/time.h" |
| 11 |
| 12 namespace nacl { |
| 13 namespace nonsfi { |
| 14 namespace { |
| 15 |
| 16 bool NaClAbiClockIdToClockId(int nacl_clk_id, clockid_t* host_clk_id) { |
| 17 switch (nacl_clk_id) { |
| 18 case NACL_ABI_CLOCK_REALTIME: |
| 19 *host_clk_id = CLOCK_REALTIME; |
| 20 break; |
| 21 case NACL_ABI_CLOCK_MONOTONIC: |
| 22 *host_clk_id = CLOCK_MONOTONIC; |
| 23 break; |
| 24 case NACL_ABI_CLOCK_PROCESS_CPUTIME_ID: |
| 25 *host_clk_id = CLOCK_PROCESS_CPUTIME_ID; |
| 26 break; |
| 27 case NACL_ABI_CLOCK_THREAD_CPUTIME_ID: |
| 28 *host_clk_id = CLOCK_THREAD_CPUTIME_ID; |
| 29 break; |
| 30 default: |
| 31 // Unknown clock id. |
| 32 return false; |
| 33 } |
| 34 return true; |
| 35 } |
| 36 |
| 37 int IrtClockGetRes(int clk_id, struct nacl_abi_timespec* res) { |
| 38 clockid_t host_clk_id; |
| 39 if (!NaClAbiClockIdToClockId(clk_id, &host_clk_id)) |
| 40 return EINVAL; |
| 41 |
| 42 struct timespec host_res; |
| 43 if (clock_getres(host_clk_id, &host_res)) |
| 44 return errno; |
| 45 |
| 46 // clock_getres() requires a NULL check but clock_gettime() doesn't. |
| 47 if (res) |
| 48 TimeSpecToNaClAbiTimeSpec(host_res, res); |
| 49 return 0; |
| 50 } |
| 51 |
| 52 int IrtClockGetTime(int clk_id, struct nacl_abi_timespec* tp) { |
| 53 clockid_t host_clk_id; |
| 54 if (!NaClAbiClockIdToClockId(clk_id, &host_clk_id)) |
| 55 return EINVAL; |
| 56 |
| 57 struct timespec host_tp; |
| 58 if (clock_gettime(host_clk_id, &host_tp)) |
| 59 return errno; |
| 60 |
| 61 TimeSpecToNaClAbiTimeSpec(host_tp, tp); |
| 62 return 0; |
| 63 } |
| 64 |
| 65 } // namespace |
| 66 |
| 67 // Cast away the distinction between host types and NaCl ABI types. |
| 68 const nacl_irt_clock kIrtClock = { |
| 69 reinterpret_cast<int(*)(clockid_t, struct timespec*)>(IrtClockGetRes), |
| 70 reinterpret_cast<int(*)(clockid_t, struct timespec*)>(IrtClockGetTime), |
| 71 }; |
| 72 |
| 73 } // namespace nonsfi |
| 74 } // namespace nacl |
OLD | NEW |