blob: 9b13a489e6b0f7fce6e7ac1138b8b214f7900e08 [file] [log] [blame]
Tim Yao72bc0fb2019-12-03 11:04:19 -08001/*
2** Copyright (C) 2007, The Android Open Source Project
3**
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
7**
8** http://www.apache.org/licenses/LICENSE-2.0
9**
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
14** limitations under the License.
15*/
16
17#include <cutils/threads.h>
18
19// For gettid.
20#if defined(__APPLE__)
21#include "AvailabilityMacros.h" // For MAC_OS_X_VERSION_MAX_ALLOWED
22#include <stdint.h>
23#include <stdlib.h>
24#include <sys/syscall.h>
25#include <sys/time.h>
26#include <unistd.h>
27#elif defined(__linux__) && !defined(__ANDROID__)
28#include <syscall.h>
29#include <unistd.h>
30#elif defined(_WIN32)
31#include <windows.h>
32#endif
33
34// No definition needed for Android because we'll just pick up bionic's copy.
Blance Tang3dce4f82020-11-27 23:11:12 +080035#if !defined(__ANDROID__) || !defined(__GLIBC__) || __GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 30)
Tim Yao72bc0fb2019-12-03 11:04:19 -080036pid_t gettid() {
37#if defined(__APPLE__)
38 uint64_t tid;
39 pthread_threadid_np(NULL, &tid);
40 return tid;
41#elif defined(__linux__)
42 return syscall(__NR_gettid);
43#elif defined(_WIN32)
44 return GetCurrentThreadId();
45#endif
46}
47#endif // __ANDROID__
48
49#if !defined(_WIN32)
50
51void* thread_store_get( thread_store_t* store )
52{
yuliang.hu3e3ab242024-07-29 14:52:31 +080053 /*coverity[missing_lock]: Used for conditional judgment, no lock is required*/
Tim Yao72bc0fb2019-12-03 11:04:19 -080054 if (!store->has_tls)
55 return NULL;
56
57 return pthread_getspecific( store->tls );
58}
59
60extern void thread_store_set( thread_store_t* store,
61 void* value,
62 thread_store_destruct_t destroy)
63{
64 pthread_mutex_lock( &store->lock );
65 if (!store->has_tls) {
66 if (pthread_key_create( &store->tls, destroy) != 0) {
67 pthread_mutex_unlock(&store->lock);
68 return;
69 }
70 store->has_tls = 1;
71 }
72 pthread_mutex_unlock( &store->lock );
73
74 pthread_setspecific( store->tls, value );
75}
76
77#else /* !defined(_WIN32) */
78void* thread_store_get( thread_store_t* store )
79{
80 if (!store->has_tls)
81 return NULL;
82
83 return (void*) TlsGetValue( store->tls );
84}
85
86void thread_store_set( thread_store_t* store,
87 void* value,
88 thread_store_destruct_t /*destroy*/ )
89{
90 /* XXX: can't use destructor on thread exit */
91 if (!store->lock_init) {
92 store->lock_init = -1;
93 InitializeCriticalSection( &store->lock );
94 store->lock_init = -2;
95 } else while (store->lock_init != -2) {
96 Sleep(10); /* 10ms */
97 }
98
99 EnterCriticalSection( &store->lock );
100 if (!store->has_tls) {
101 store->tls = TlsAlloc();
102 if (store->tls == TLS_OUT_OF_INDEXES) {
103 LeaveCriticalSection( &store->lock );
104 return;
105 }
106 store->has_tls = 1;
107 }
108 LeaveCriticalSection( &store->lock );
109
110 TlsSetValue( store->tls, value );
111}
112#endif /* !defined(_WIN32) */