Index: components/nacl/loader/nonsfi/irt_clock.cc |
diff --git a/components/nacl/loader/nonsfi/irt_clock.cc b/components/nacl/loader/nonsfi/irt_clock.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..5e7f581e0725b73117aabc9bfdb5e63ab2ad7588 |
--- /dev/null |
+++ b/components/nacl/loader/nonsfi/irt_clock.cc |
@@ -0,0 +1,75 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include <errno.h> |
+#include <time.h> |
+ |
+#include "components/nacl/loader/nonsfi/abi_conversion.h" |
+#include "components/nacl/loader/nonsfi/irt_interfaces.h" |
+#include "native_client/src/trusted/service_runtime/include/sys/time.h" |
+ |
+namespace nacl { |
+namespace nonsfi { |
+namespace { |
+ |
+bool NaClAbiClockIdToClockId(int nacl_clk_id, clockid_t* host_clk_id) { |
+ switch (nacl_clk_id) { |
+ case NACL_ABI_CLOCK_REALTIME: |
+ *host_clk_id = CLOCK_REALTIME; |
+ break; |
+ case NACL_ABI_CLOCK_MONOTONIC: |
+ *host_clk_id = CLOCK_MONOTONIC; |
+ break; |
+ case NACL_ABI_CLOCK_PROCESS_CPUTIME_ID: |
+ *host_clk_id = CLOCK_PROCESS_CPUTIME_ID; |
+ break; |
+ case NACL_ABI_CLOCK_THREAD_CPUTIME_ID: |
+ *host_clk_id = CLOCK_THREAD_CPUTIME_ID; |
+ break; |
+ default: |
+ // Unknown clock id. |
+ return false; |
+ } |
+ return true; |
+} |
+ |
+int IrtClockGetRes(int clk_id, struct nacl_abi_timespec* rem) { |
Mark Seaborn
2014/01/15 19:49:28
Should be "res", not "rem"
hidehiko
2014/01/16 05:30:12
Oops, done.
|
+ clockid_t host_clk_id; |
+ if (!NaClAbiClockIdToClockId(clk_id, &host_clk_id)) |
+ return EINVAL; |
+ |
+ struct timespec host_rem; |
+ if (clock_getres(host_clk_id, &host_rem)) |
+ return errno; |
+ |
+ if (rem) |
Mark Seaborn
2014/01/15 19:49:28
Hmm, interesting that clock_getres() checks for NU
hidehiko
2014/01/16 05:30:12
Done to add the comment.
What I found are:
https:
|
+ TimeSpecToNaClAbiTimeSpec(host_rem, rem); |
+ return 0; |
+} |
+ |
+int IrtClockGetTime(int clk_id, struct nacl_abi_timespec* tp) { |
+ clockid_t host_clk_id; |
+ if (!NaClAbiClockIdToClockId(clk_id, &host_clk_id)) |
+ return EINVAL; |
+ |
+ struct timespec host_tp; |
+ if (clock_gettime(host_clk_id, &host_tp)) |
+ return errno; |
+ |
+ TimeSpecToNaClAbiTimeSpec(host_tp, tp); |
+ return 0; |
+} |
+ |
+} // namespace |
+ |
+// Both clock_getres and clock_gettime should use nacl's ABI, nacl_clockid_t |
Mark Seaborn
2014/01/15 19:49:28
You probably don't need to comment about the casti
hidehiko
2014/01/16 05:30:12
Done. I'll send a follow up CL for other files, wh
Mark Seaborn
2014/01/16 19:14:13
You don't have to change the existing ones unless
|
+// and struct nacl_abi_timespec. However, the definition of nacl_irt_clock |
+// uses host type, clockid_t and timespec, so here we need to cast them. |
+const nacl_irt_clock kIrtClock = { |
+ reinterpret_cast<int(*)(clockid_t, struct timespec*)>(IrtClockGetRes), |
+ reinterpret_cast<int(*)(clockid_t, struct timespec*)>(IrtClockGetTime), |
+}; |
+ |
+} // namespace nonsfi |
+} // namespace nacl |