blob: 9400441997f94590bc87582b6118b96c2203988d [file] [log] [blame]
Mark Salyzyn12717162014-04-29 15:49:14 -07001/*
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08002** Copyright (C) 2007, The Android Open Source Project
3**
Yabin Cui4a6e5a32015-01-26 19:48:54 -08004** 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 Projectdd7bc332009-03-03 19:32:55 -08007**
Yabin Cui4a6e5a32015-01-26 19:48:54 -08008** http://www.apache.org/licenses/LICENSE-2.0
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08009**
Yabin Cui4a6e5a32015-01-26 19:48:54 -080010** 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 Projectdd7bc332009-03-03 19:32:55 -080014** limitations under the License.
15*/
Mark Salyzyn12717162014-04-29 15:49:14 -070016
Dan Albert7dfb61d2015-03-20 13:46:28 -070017#include "cutils/threads.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080018
Yabin Cui4a6e5a32015-01-26 19:48:54 -080019#if !defined(_WIN32)
Dan Albert7dfb61d2015-03-20 13:46:28 -070020
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 Albert48306022015-03-23 16:38:34 -070026#include <unistd.h>
Dan Albert7dfb61d2015-03-20 13:46:28 -070027#elif defined(__linux__) && !defined(__ANDROID__)
28#include <syscall.h>
29#include <unistd.h>
30#elif defined(_WIN32)
31#include <Windows.h>
32#endif
33
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080034void* thread_store_get( thread_store_t* store )
35{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080036 if (!store->has_tls)
37 return NULL;
38
39 return pthread_getspecific( store->tls );
40}
Yabin Cui4a6e5a32015-01-26 19:48:54 -080041
42extern void thread_store_set( thread_store_t* store,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080043 void* value,
44 thread_store_destruct_t destroy)
45{
46 pthread_mutex_lock( &store->lock );
47 if (!store->has_tls) {
48 if (pthread_key_create( &store->tls, destroy) != 0) {
49 pthread_mutex_unlock(&store->lock);
50 return;
51 }
52 store->has_tls = 1;
53 }
54 pthread_mutex_unlock( &store->lock );
55
56 pthread_setspecific( store->tls, value );
57}
58
Dan Albert7dfb61d2015-03-20 13:46:28 -070059// No definition needed for Android because we'll just pick up bionic's copy.
60#ifndef __ANDROID__
61pid_t gettid() {
62#if defined(__APPLE__)
63 uint64_t owner;
Dan Alberte3081042015-03-23 16:07:42 -070064 int rc = pthread_threadid_np(NULL, &owner);
65 if (rc != 0) {
66 abort();
67 }
Dan Albert7dfb61d2015-03-20 13:46:28 -070068 return owner;
69#elif defined(__linux__)
70 return syscall(__NR_gettid);
71#elif defined(_WIN32)
72 return (pid_t)GetCurrentThreadId();
73#endif
74}
75#endif // __ANDROID__
76
Yabin Cui4a6e5a32015-01-26 19:48:54 -080077#else /* !defined(_WIN32) */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080078void* thread_store_get( thread_store_t* store )
79{
80 if (!store->has_tls)
81 return NULL;
Yabin Cui4a6e5a32015-01-26 19:48:54 -080082
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080083 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 }
Yabin Cui4a6e5a32015-01-26 19:48:54 -080098
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080099 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 );
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800109
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800110 TlsSetValue( store->tls, value );
111}
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800112#endif /* !defined(_WIN32) */