| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2012 The Native Client Authors. All rights reserved. | 2 * Copyright (c) 2012 The Native Client Authors. All rights reserved. |
| 3 * Use of this source code is governed by a BSD-style license that can be | 3 * Use of this source code is governed by a BSD-style license that can be |
| 4 * found in the LICENSE file. | 4 * found in the LICENSE file. |
| 5 */ | 5 */ |
| 6 | 6 |
| 7 #include "native_client/src/shared/platform/nacl_clock.h" | 7 #include "native_client/src/shared/platform/nacl_clock.h" |
| 8 | 8 |
| 9 #include "native_client/src/include/nacl_macros.h" | 9 #include "native_client/src/include/nacl_macros.h" |
| 10 #include "native_client/src/include/portability.h" | 10 #include "native_client/src/include/portability.h" |
| 11 #include "native_client/src/shared/platform/nacl_host_desc.h" | 11 #include "native_client/src/shared/platform/nacl_host_desc.h" |
| 12 #include "native_client/src/shared/platform/nacl_log.h" | 12 #include "native_client/src/shared/platform/nacl_log.h" |
| 13 #include "native_client/src/shared/platform/nacl_time.h" | 13 #include "native_client/src/shared/platform/nacl_time.h" |
| 14 #include "native_client/src/trusted/service_runtime/include/sys/errno.h" | 14 #include "native_client/src/trusted/service_runtime/include/sys/errno.h" |
| 15 | 15 |
| 16 /* | 16 /* |
| 17 * OSX does not include POSIX.1-2011 functions, so we emulate using | 17 * OSX does not include POSIX.1-2011 functions, so we emulate using |
| 18 * Mach calls. | 18 * Mach calls. |
| 19 */ | 19 */ |
| 20 #include <mach/mach.h> | 20 #include <mach/mach.h> |
| 21 #include <mach/mach_time.h> | 21 #include <mach/mach_time.h> |
| 22 #include <mach/task.h> | 22 #include <mach/task.h> |
| 23 #include <mach/task_info.h> | 23 #include <mach/task_info.h> |
| 24 #include <mach/thread_info.h> | 24 #include <mach/thread_info.h> |
| 25 #include <pthread.h> |
| 25 | 26 |
| 26 static int g_NaClClock_is_initialized = 0; | 27 static int g_NaClClock_is_initialized = 0; |
| 27 static mach_timebase_info_data_t g_NaCl_time_base_info; | 28 static mach_timebase_info_data_t g_NaCl_time_base_info; |
| 28 | 29 |
| 29 int NaClClockInit(void) { | 30 int NaClClockInit(void) { |
| 30 g_NaClClock_is_initialized = (mach_timebase_info(&g_NaCl_time_base_info) | 31 g_NaClClock_is_initialized = (mach_timebase_info(&g_NaCl_time_base_info) |
| 31 == KERN_SUCCESS); | 32 == KERN_SUCCESS); |
| 32 | 33 |
| 33 return g_NaClClock_is_initialized; | 34 return g_NaClClock_is_initialized; |
| 34 } | 35 } |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 + absolutetime_info.total_system) | 125 + absolutetime_info.total_system) |
| 125 / NACL_NANOS_PER_UNIT); | 126 / NACL_NANOS_PER_UNIT); |
| 126 tp->tv_nsec = ((absolutetime_info.total_user | 127 tp->tv_nsec = ((absolutetime_info.total_user |
| 127 + absolutetime_info.total_system) | 128 + absolutetime_info.total_system) |
| 128 % NACL_NANOS_PER_UNIT); | 129 % NACL_NANOS_PER_UNIT); |
| 129 rv = 0; | 130 rv = 0; |
| 130 break; | 131 break; |
| 131 case NACL_CLOCK_THREAD_CPUTIME_ID: | 132 case NACL_CLOCK_THREAD_CPUTIME_ID: |
| 132 count = THREAD_BASIC_INFO_COUNT; | 133 count = THREAD_BASIC_INFO_COUNT; |
| 133 | 134 |
| 134 if (KERN_SUCCESS == thread_info(mach_thread_self(), | 135 /* |
| 136 * Don't use mach_thread_self() because it requires a separate |
| 137 * mach_port_deallocate() system call to release it. Instead, rely on |
| 138 * pthread's cached copy of the port. |
| 139 */ |
| 140 if (KERN_SUCCESS == thread_info(pthread_mach_thread_np(pthread_self()), |
| 135 THREAD_BASIC_INFO, | 141 THREAD_BASIC_INFO, |
| 136 (thread_info_t) basic_info, | 142 (thread_info_t) basic_info, |
| 137 &count)) { | 143 &count)) { |
| 138 tick_ns = ((basic_info->user_time.microseconds | 144 tick_ns = ((basic_info->user_time.microseconds |
| 139 + basic_info->system_time.microseconds) | 145 + basic_info->system_time.microseconds) |
| 140 * NACL_NANOS_PER_MICRO); | 146 * NACL_NANOS_PER_MICRO); |
| 141 tp->tv_sec = (basic_info->user_time.seconds | 147 tp->tv_sec = (basic_info->user_time.seconds |
| 142 + basic_info->system_time.seconds | 148 + basic_info->system_time.seconds |
| 143 + (tick_ns / NACL_NANOS_PER_UNIT)); | 149 + (tick_ns / NACL_NANOS_PER_UNIT)); |
| 144 tp->tv_nsec = tick_ns % NACL_NANOS_PER_UNIT; | 150 tp->tv_nsec = tick_ns % NACL_NANOS_PER_UNIT; |
| 145 rv = 0; | 151 rv = 0; |
| 146 } | 152 } |
| 147 break; | 153 break; |
| 148 } | 154 } |
| 149 return rv; | 155 return rv; |
| 150 } | 156 } |
| OLD | NEW |