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 | |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 19 | #if !defined(_WIN32) |
Dan Albert | 7dfb61d | 2015-03-20 13:46:28 -0700 | [diff] [blame] | 20 | |
| 21 | // For gettid. |
| 22 | #if defined(__APPLE__) |
| 23 | #include "AvailabilityMacros.h" // For MAC_OS_X_VERSION_MAX_ALLOWED |
| 24 | #include <sys/syscall.h> |
| 25 | #include <sys/time.h> |
Dan Albert | 7dfb61d | 2015-03-20 13:46:28 -0700 | [diff] [blame] | 26 | #elif defined(__linux__) && !defined(__ANDROID__) |
| 27 | #include <syscall.h> |
| 28 | #include <unistd.h> |
| 29 | #elif defined(_WIN32) |
| 30 | #include <Windows.h> |
| 31 | #endif |
| 32 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 33 | void* thread_store_get( thread_store_t* store ) |
| 34 | { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 35 | if (!store->has_tls) |
| 36 | return NULL; |
| 37 | |
| 38 | return pthread_getspecific( store->tls ); |
| 39 | } |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 40 | |
| 41 | extern void thread_store_set( thread_store_t* store, |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 42 | void* value, |
| 43 | thread_store_destruct_t destroy) |
| 44 | { |
| 45 | pthread_mutex_lock( &store->lock ); |
| 46 | if (!store->has_tls) { |
| 47 | if (pthread_key_create( &store->tls, destroy) != 0) { |
| 48 | pthread_mutex_unlock(&store->lock); |
| 49 | return; |
| 50 | } |
| 51 | store->has_tls = 1; |
| 52 | } |
| 53 | pthread_mutex_unlock( &store->lock ); |
| 54 | |
| 55 | pthread_setspecific( store->tls, value ); |
| 56 | } |
| 57 | |
Dan Albert | 7dfb61d | 2015-03-20 13:46:28 -0700 | [diff] [blame] | 58 | // No definition needed for Android because we'll just pick up bionic's copy. |
| 59 | #ifndef __ANDROID__ |
| 60 | pid_t gettid() { |
| 61 | #if defined(__APPLE__) |
| 62 | uint64_t owner; |
Dan Albert | e308104 | 2015-03-23 16:07:42 -0700 | [diff] [blame^] | 63 | int rc = pthread_threadid_np(NULL, &owner); |
| 64 | if (rc != 0) { |
| 65 | abort(); |
| 66 | } |
Dan Albert | 7dfb61d | 2015-03-20 13:46:28 -0700 | [diff] [blame] | 67 | return owner; |
| 68 | #elif defined(__linux__) |
| 69 | return syscall(__NR_gettid); |
| 70 | #elif defined(_WIN32) |
| 71 | return (pid_t)GetCurrentThreadId(); |
| 72 | #endif |
| 73 | } |
| 74 | #endif // __ANDROID__ |
| 75 | |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 76 | #else /* !defined(_WIN32) */ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 77 | void* thread_store_get( thread_store_t* store ) |
| 78 | { |
| 79 | if (!store->has_tls) |
| 80 | return NULL; |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 81 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 82 | return (void*) TlsGetValue( store->tls ); |
| 83 | } |
| 84 | |
| 85 | void thread_store_set( thread_store_t* store, |
| 86 | void* value, |
| 87 | thread_store_destruct_t destroy ) |
| 88 | { |
| 89 | /* XXX: can't use destructor on thread exit */ |
| 90 | if (!store->lock_init) { |
| 91 | store->lock_init = -1; |
| 92 | InitializeCriticalSection( &store->lock ); |
| 93 | store->lock_init = -2; |
| 94 | } else while (store->lock_init != -2) { |
| 95 | Sleep(10); /* 10ms */ |
| 96 | } |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 97 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 98 | EnterCriticalSection( &store->lock ); |
| 99 | if (!store->has_tls) { |
| 100 | store->tls = TlsAlloc(); |
| 101 | if (store->tls == TLS_OUT_OF_INDEXES) { |
| 102 | LeaveCriticalSection( &store->lock ); |
| 103 | return; |
| 104 | } |
| 105 | store->has_tls = 1; |
| 106 | } |
| 107 | LeaveCriticalSection( &store->lock ); |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 108 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 109 | TlsSetValue( store->tls, value ); |
| 110 | } |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 111 | #endif /* !defined(_WIN32) */ |