OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2013 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 #include "native_client/src/trusted/service_runtime/osx/mach_thread_map.h" |
| 8 |
| 9 #include <pthread.h> |
| 10 |
| 11 #include "native_client/src/shared/platform/nacl_check.h" |
| 12 #include "native_client/src/trusted/service_runtime/arch/sel_ldr_arch.h" |
| 13 #include "native_client/src/trusted/service_runtime/nacl_globals.h" |
| 14 |
| 15 #if NACL_ARCH(NACL_BUILD_ARCH) == NACL_x86 && NACL_BUILD_SUBARCH == 64 |
| 16 |
| 17 static mach_port_t mach_threads[NACL_THREAD_MAX]; |
| 18 |
| 19 uint32_t NaClGetThreadIndexForMachThread(mach_port_t mach_thread) { |
| 20 uint32_t nacl_thread_index; |
| 21 |
| 22 DCHECK(mach_thread != MACH_PORT_NULL); |
| 23 |
| 24 CHECK(NACL_TLS_INDEX_INVALID < NACL_THREAD_MAX); /* Skip the invalid slot. */ |
| 25 for (nacl_thread_index = NACL_TLS_INDEX_INVALID + 1; |
| 26 nacl_thread_index < NACL_THREAD_MAX; |
| 27 ++nacl_thread_index) { |
| 28 if (mach_threads[nacl_thread_index] == mach_thread) { |
| 29 break; |
| 30 } |
| 31 } |
| 32 |
| 33 return nacl_thread_index == NACL_THREAD_MAX ? NACL_TLS_INDEX_INVALID : |
| 34 nacl_thread_index; |
| 35 } |
| 36 |
| 37 void NaClSetCurrentMachThreadForThreadIndex(uint32_t nacl_thread_index) { |
| 38 /* |
| 39 * This implementation relies on the Mach port for the thread stored by the |
| 40 * pthread library, and assumes that the pthread library does not close and |
| 41 * re-acquire the Mach port for the thread. If that happens, Mach could |
| 42 * theoretically assign the port a different number in the process' port |
| 43 * table. This approach avoids having to deal with ownership of the port and |
| 44 * the system call overhad to obtain and deallocate it as would be the case |
| 45 * with mach_thread_self(). |
| 46 * |
| 47 * When used by the Mach exception handler, this also assumes that the |
| 48 * thread port number when received for an exception will match the port |
| 49 * stored in the mach_threads table. This is guaranteed by how the kernel |
| 50 * coalesces ports in a single port namespace. (A task, or process, is a |
| 51 * single port namespace.) |
| 52 * |
| 53 * An alternative implementation that works on Mac OS X 10.6 or higher is to |
| 54 * use pthread_threadid_np() to obtain a thread ID to use as the key for |
| 55 * this thread map. Such thread IDs are unique system-wide. An exception |
| 56 * handler can find the thread ID for a Mach thread by calling thread_info() |
| 57 * with flavor THREAD_IDENTIFIER_INFO. This approach is not used here |
| 58 * because of the added system call overhead at exception time. |
| 59 */ |
| 60 mach_port_t mach_thread = pthread_mach_thread_np(pthread_self()); |
| 61 CHECK(mach_thread != MACH_PORT_NULL); |
| 62 |
| 63 DCHECK(nacl_thread_index > NACL_TLS_INDEX_INVALID && |
| 64 nacl_thread_index < NACL_THREAD_MAX); |
| 65 DCHECK(mach_threads[nacl_thread_index] == MACH_PORT_NULL); |
| 66 DCHECK(NaClGetThreadIndexForMachThread(mach_thread) == |
| 67 NACL_TLS_INDEX_INVALID); |
| 68 |
| 69 mach_threads[nacl_thread_index] = mach_thread; |
| 70 } |
| 71 |
| 72 void NaClClearMachThreadForThreadIndex(uint32_t nacl_thread_index) { |
| 73 DCHECK(nacl_thread_index > NACL_TLS_INDEX_INVALID && |
| 74 nacl_thread_index < NACL_THREAD_MAX); |
| 75 DCHECK(mach_threads[nacl_thread_index] == |
| 76 pthread_mach_thread_np(pthread_self())); |
| 77 |
| 78 mach_threads[nacl_thread_index] = MACH_PORT_NULL; |
| 79 } |
| 80 |
| 81 #endif |
OLD | NEW |