Mark Salyzyn | 1271716 | 2014-04-29 15:49:14 -0700 | [diff] [blame] | 1 | /* |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 2 | ** Copyright (C) 2007, The Android Open Source Project |
| 3 | ** |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 4 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | ** you may not use this file except in compliance with the License. |
| 6 | ** You may obtain a copy of the License at |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 7 | ** |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 8 | ** http://www.apache.org/licenses/LICENSE-2.0 |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 9 | ** |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 10 | ** Unless required by applicable law or agreed to in writing, software |
| 11 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | ** See the License for the specific language governing permissions and |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 14 | ** limitations under the License. |
| 15 | */ |
Mark Salyzyn | 1271716 | 2014-04-29 15:49:14 -0700 | [diff] [blame] | 16 | |
Dan Albert | 7dfb61d | 2015-03-20 13:46:28 -0700 | [diff] [blame] | 17 | #include "cutils/threads.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 18 | |
Dan Albert | 7dfb61d | 2015-03-20 13:46:28 -0700 | [diff] [blame] | 19 | // For gettid. |
| 20 | #if defined(__APPLE__) |
| 21 | #include "AvailabilityMacros.h" // For MAC_OS_X_VERSION_MAX_ALLOWED |
Christopher Ferris | fc3576f | 2015-03-23 21:35:56 -0700 | [diff] [blame] | 22 | #include <stdint.h> |
| 23 | #include <stdlib.h> |
Dan Albert | 7dfb61d | 2015-03-20 13:46:28 -0700 | [diff] [blame] | 24 | #include <sys/syscall.h> |
| 25 | #include <sys/time.h> |
Dan Albert | 4830602 | 2015-03-23 16:38:34 -0700 | [diff] [blame] | 26 | #include <unistd.h> |
Dan Albert | 7dfb61d | 2015-03-20 13:46:28 -0700 | [diff] [blame] | 27 | #elif defined(__linux__) && !defined(__ANDROID__) |
| 28 | #include <syscall.h> |
| 29 | #include <unistd.h> |
| 30 | #elif defined(_WIN32) |
Dan Albert | b3a36ca | 2015-04-29 17:13:32 -0700 | [diff] [blame] | 31 | #include <windows.h> |
Dan Albert | 7dfb61d | 2015-03-20 13:46:28 -0700 | [diff] [blame] | 32 | #endif |
| 33 | |
Dan Albert | b3a36ca | 2015-04-29 17:13:32 -0700 | [diff] [blame] | 34 | // No definition needed for Android because we'll just pick up bionic's copy. |
| 35 | #ifndef __ANDROID__ |
| 36 | pid_t gettid() { |
| 37 | #if defined(__APPLE__) |
Dan Albert | 23f750b | 2015-04-30 12:52:21 -0700 | [diff] [blame] | 38 | return syscall(SYS_thread_selfid); |
Dan Albert | b3a36ca | 2015-04-29 17:13:32 -0700 | [diff] [blame] | 39 | #elif defined(__linux__) |
| 40 | return syscall(__NR_gettid); |
| 41 | #elif defined(_WIN32) |
| 42 | return GetCurrentThreadId(); |
| 43 | #endif |
| 44 | } |
| 45 | #endif // __ANDROID__ |
| 46 | |
| 47 | #if !defined(_WIN32) |
| 48 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 49 | void* thread_store_get( thread_store_t* store ) |
| 50 | { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 51 | if (!store->has_tls) |
| 52 | return NULL; |
| 53 | |
| 54 | return pthread_getspecific( store->tls ); |
| 55 | } |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 56 | |
| 57 | extern void thread_store_set( thread_store_t* store, |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 58 | void* value, |
| 59 | thread_store_destruct_t destroy) |
| 60 | { |
| 61 | pthread_mutex_lock( &store->lock ); |
| 62 | if (!store->has_tls) { |
| 63 | if (pthread_key_create( &store->tls, destroy) != 0) { |
| 64 | pthread_mutex_unlock(&store->lock); |
| 65 | return; |
| 66 | } |
| 67 | store->has_tls = 1; |
| 68 | } |
| 69 | pthread_mutex_unlock( &store->lock ); |
| 70 | |
| 71 | pthread_setspecific( store->tls, value ); |
| 72 | } |
| 73 | |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 74 | #else /* !defined(_WIN32) */ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 75 | void* thread_store_get( thread_store_t* store ) |
| 76 | { |
| 77 | if (!store->has_tls) |
| 78 | return NULL; |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 79 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 80 | return (void*) TlsGetValue( store->tls ); |
| 81 | } |
| 82 | |
| 83 | void thread_store_set( thread_store_t* store, |
| 84 | void* value, |
| 85 | thread_store_destruct_t destroy ) |
| 86 | { |
| 87 | /* XXX: can't use destructor on thread exit */ |
| 88 | if (!store->lock_init) { |
| 89 | store->lock_init = -1; |
| 90 | InitializeCriticalSection( &store->lock ); |
| 91 | store->lock_init = -2; |
| 92 | } else while (store->lock_init != -2) { |
| 93 | Sleep(10); /* 10ms */ |
| 94 | } |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 95 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 96 | EnterCriticalSection( &store->lock ); |
| 97 | if (!store->has_tls) { |
| 98 | store->tls = TlsAlloc(); |
| 99 | if (store->tls == TLS_OUT_OF_INDEXES) { |
| 100 | LeaveCriticalSection( &store->lock ); |
| 101 | return; |
| 102 | } |
| 103 | store->has_tls = 1; |
| 104 | } |
| 105 | LeaveCriticalSection( &store->lock ); |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 106 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 107 | TlsSetValue( store->tls, value ); |
| 108 | } |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 109 | #endif /* !defined(_WIN32) */ |