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 <sched.h> | |
7 #include <stdlib.h> | |
8 #include <sys/time.h> | |
9 #include <time.h> | |
10 #include <unistd.h> | |
11 | |
12 #include "components/nacl/loader/nonsfi/irt_interfaces.h" | |
13 #include "native_client/src/trusted/service_runtime/include/sys/time.h" | |
14 #include "native_client/src/trusted/service_runtime/include/sys/unistd.h" | |
15 | |
16 namespace nacl { | |
17 namespace nonsfi { | |
18 namespace { | |
19 | |
20 void IrtExit(int status) { | |
21 ::_exit(status); | |
Mark Seaborn
2014/01/10 05:12:20
Maybe leave off the "::" prefixes unless there's a
hidehiko
2014/01/10 07:27:47
Done.
| |
22 } | |
23 | |
24 int IrtGetToD(struct nacl_abi_timeval* tv) { | |
25 struct timeval host_tv; | |
26 if (::gettimeofday(&host_tv, NULL) == -1) | |
Mark Seaborn
2014/01/10 05:12:20
Nit: in general, "!= 0" is slightly more defensive
hidehiko
2014/01/10 07:27:47
Done.
| |
27 return errno; | |
28 tv->nacl_abi_tv_sec = host_tv.tv_sec; | |
29 tv->nacl_abi_tv_usec = host_tv.tv_usec; | |
30 return 0; | |
31 } | |
32 | |
33 int IrtClock(nacl_abi_clock_t* ticks) { | |
34 clock_t host_ticks = ::clock(); | |
35 if (host_ticks == static_cast<nacl_abi_clock_t>(-1)) | |
36 return EOVERFLOW; | |
37 *ticks = host_ticks; | |
38 return 0; | |
39 } | |
40 | |
41 int IrtNanoSleep(const struct nacl_abi_timespec* req, | |
42 struct nacl_abi_timespec* rem) { | |
43 if (!req) | |
44 return EFAULT; | |
45 | |
46 struct timespec host_req; | |
47 host_req.tv_sec = req->tv_sec; | |
48 host_req.tv_nsec = req->tv_nsec; | |
49 struct timespec host_rem; | |
50 if (::nanosleep(&host_req, &host_rem) == -1) | |
Mark Seaborn
2014/01/10 05:12:20
"!= 0" (nit)
hidehiko
2014/01/10 07:27:47
Done.
| |
51 return errno; | |
52 | |
53 if (rem) { | |
54 rem->tv_sec = host_rem.tv_sec; | |
55 rem->tv_nsec = host_rem.tv_nsec; | |
56 } | |
57 return 0; | |
58 } | |
59 | |
60 int IrtSchedYield() { | |
61 if (::sched_yield() == -1) | |
62 return errno; | |
63 | |
64 return 0; | |
65 } | |
66 | |
67 int IrtSysconf(int name, int* value) { | |
68 int result; | |
69 switch (name) { | |
70 case NACL_ABI__SC_NPROCESSORS_ONLN: | |
71 errno = 0; | |
72 result = ::sysconf(_SC_NPROCESSORS_ONLN); | |
73 break; | |
74 case NACL_ABI__SC_PAGESIZE: | |
75 errno = 0; | |
76 result = ::sysconf(_SC_PAGESIZE); | |
77 break; | |
78 default: | |
79 errno = EINVAL; | |
Mark Seaborn
2014/01/10 05:12:20
Just "return EINVAL" here?
hidehiko
2014/01/10 07:27:47
Done.
| |
80 result = -1; | |
81 } | |
82 | |
83 if (result == -1 && errno == EINVAL) | |
84 return EINVAL; | |
85 | |
86 *value = result; | |
87 return 0; | |
88 } | |
89 | |
90 } // namespace | |
91 | |
92 // For gettod, clock and nanosleep, their argument types should be nacl_abi_X, | |
93 // rather than host type, such as timeval or clock_t etc. However, the | |
94 // definition of nacl_irt_basic uses host types, so here we need to cast them. | |
95 const nacl_irt_basic kIrtBasic = { | |
96 IrtExit, | |
97 reinterpret_cast<int(*)(struct timeval*)>(IrtGetToD), | |
98 reinterpret_cast<int(*)(clock_t*)>(IrtClock), | |
99 reinterpret_cast<int(*)(const struct timespec*, struct timespec*)>( | |
100 IrtNanoSleep), | |
101 IrtSchedYield, | |
102 IrtSysconf, | |
103 }; | |
104 | |
105 } // namespace nonsfi | |
106 } // namespace nacl | |
OLD | NEW |