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.
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"
16 bool NaClAbiClockIdToClockId(nacl_irt_clockid_t nacl_clk_id
,
17 clockid_t
* host_clk_id
) {
18 switch (nacl_clk_id
) {
19 case NACL_ABI_CLOCK_REALTIME
:
20 *host_clk_id
= CLOCK_REALTIME
;
22 case NACL_ABI_CLOCK_MONOTONIC
:
23 *host_clk_id
= CLOCK_MONOTONIC
;
25 case NACL_ABI_CLOCK_PROCESS_CPUTIME_ID
:
26 *host_clk_id
= CLOCK_PROCESS_CPUTIME_ID
;
28 case NACL_ABI_CLOCK_THREAD_CPUTIME_ID
:
29 *host_clk_id
= CLOCK_THREAD_CPUTIME_ID
;
38 int IrtClockGetRes(nacl_irt_clockid_t clk_id
, struct nacl_abi_timespec
* res
) {
39 clockid_t host_clk_id
;
40 if (!NaClAbiClockIdToClockId(clk_id
, &host_clk_id
))
43 struct timespec host_res
;
44 if (clock_getres(host_clk_id
, &host_res
))
47 // clock_getres() requires a NULL check but clock_gettime() doesn't.
49 TimeSpecToNaClAbiTimeSpec(host_res
, res
);
53 int IrtClockGetTime(nacl_irt_clockid_t clk_id
, struct nacl_abi_timespec
* tp
) {
54 clockid_t host_clk_id
;
55 if (!NaClAbiClockIdToClockId(clk_id
, &host_clk_id
))
58 struct timespec host_tp
;
59 if (clock_gettime(host_clk_id
, &host_tp
))
62 TimeSpecToNaClAbiTimeSpec(host_tp
, tp
);
68 // Cast away the distinction between host types and NaCl ABI types.
69 const nacl_irt_clock kIrtClock
= {
70 reinterpret_cast<int(*)(nacl_irt_clockid_t
, struct timespec
*)>(
72 reinterpret_cast<int(*)(nacl_irt_clockid_t
, struct timespec
*)>(