1 : /*
2 : * Copyright (c) 2011 The Native Client Authors. All rights reserved.
3 : * Use of this source code is governed by a BSD-style license that can be
4 : * found in the LICENSE file.
5 : */
6 :
7 : /*
8 : * NaCl Server Runtime time abstraction layer.
9 : * This is the host-OS-dependent implementation.
10 : */
11 :
12 : #include <sys/time.h>
13 : #include <time.h>
14 :
15 : #include "native_client/src/include/nacl_macros.h"
16 : #include "native_client/src/shared/platform/nacl_log.h"
17 : #include "native_client/src/shared/platform/nacl_time.h"
18 : #include "native_client/src/shared/platform/linux/nacl_time_types.h"
19 :
20 : #include "native_client/src/trusted/service_runtime/linux/nacl_syscall_inl.h"
21 :
22 : #define NANOS_PER_UNIT (1000*1000*1000)
23 :
24 : static struct NaClTimeState gNaClTimeState;
25 :
26 0 : void NaClAllowLowResolutionTimeOfDay() {
27 : /* Always use high resolution timer. */
28 0 : }
29 :
30 46 : void NaClTimeInternalInit(struct NaClTimeState *ntsp) {
31 46 : ntsp->time_resolution_ns = NACL_NANOS_PER_MICRO;
32 46 : }
33 :
34 30 : void NaClTimeInternalFini(struct NaClTimeState *ntsp) {
35 : UNREFERENCED_PARAMETER(ntsp);
36 30 : }
37 :
38 0 : uint64_t NaClTimerResolutionNsInternal(struct NaClTimeState *ntsp) {
39 0 : return ntsp->time_resolution_ns;
40 : }
41 :
42 46 : void NaClTimeInit(void) {
43 46 : NaClTimeInternalInit(&gNaClTimeState);
44 46 : }
45 :
46 30 : void NaClTimeFini(void) {
47 30 : NaClTimeInternalFini(&gNaClTimeState);
48 30 : }
49 :
50 0 : uint64_t NaClTimerResolutionNanoseconds(void) {
51 0 : return NaClTimerResolutionNsInternal(&gNaClTimeState);
52 : }
53 :
54 116 : int NaClGetTimeOfDay(struct nacl_abi_timeval *tv) {
55 : struct timeval sys_tv;
56 : int retval;
57 :
58 116 : retval = gettimeofday(&sys_tv, NULL);
59 116 : if (0 == retval) {
60 116 : tv->nacl_abi_tv_sec = sys_tv.tv_sec;
61 116 : tv->nacl_abi_tv_usec = sys_tv.tv_usec;
62 : }
63 :
64 116 : retval = NaClXlateSysRet(retval);
65 116 : return retval;
66 : }
67 :
68 : int NaClNanosleep(struct nacl_abi_timespec const *req,
69 75 : struct nacl_abi_timespec *rem) {
70 : struct timespec host_req;
71 : struct timespec host_rem;
72 : struct timespec *host_remptr;
73 : int retval;
74 :
75 75 : host_req.tv_sec = req->tv_sec;
76 75 : host_req.tv_nsec = req->tv_nsec;
77 75 : if (NULL == rem) {
78 74 : host_remptr = NULL;
79 : } else {
80 1 : host_remptr = &host_rem;
81 : }
82 75 : NaClLog(4,
83 : "nanosleep(%"NACL_PRIxPTR", %"NACL_PRIxPTR")\n",
84 : (uintptr_t) &host_req,
85 : (uintptr_t) host_remptr);
86 75 : NaClLog(4, "nanosleep(time = %"NACL_PRId64".%09"NACL_PRId64" S)\n",
87 : (int64_t) host_req.tv_sec, (int64_t) host_req.tv_nsec);
88 75 : if (host_req.tv_nsec > NANOS_PER_UNIT) {
89 0 : NaClLog(0, "tv_nsec too large %"NACL_PRId64"\n",
90 : (int64_t) host_req.tv_nsec);
91 : }
92 75 : retval = nanosleep(&host_req, host_remptr);
93 75 : NaClLog(4, " returned %d\n", retval);
94 :
95 75 : if (0 != retval && EINTR == errno && NULL != rem) {
96 0 : rem->tv_sec = host_rem.tv_sec;
97 0 : rem->tv_nsec = host_rem.tv_nsec;
98 : }
99 75 : retval = NaClXlateSysRet(retval);
100 75 : return retval;
101 : }
|