blob: 3d8dd484896c1b0e92ffc3c3f84db2d7b78d94d4 [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
Dan Albert7dfb61d2015-03-20 13:46:28 -070019// For gettid.
20#if defined(__APPLE__)
21#include "AvailabilityMacros.h" // For MAC_OS_X_VERSION_MAX_ALLOWED
Christopher Ferrisfc3576f2015-03-23 21:35:56 -070022#include <stdint.h>
23#include <stdlib.h>
Dan Albert7dfb61d2015-03-20 13:46:28 -070024#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)
Dan Albertb3a36ca2015-04-29 17:13:32 -070031#include <windows.h>
Dan Albert7dfb61d2015-03-20 13:46:28 -070032#endif
33
Dan Albertb3a36ca2015-04-29 17:13:32 -070034// No definition needed for Android because we'll just pick up bionic's copy.
35#ifndef __ANDROID__
36pid_t gettid() {
37#if defined(__APPLE__)
38 uint64_t owner;
39 int rc = pthread_threadid_np(NULL, &owner);
40 if (rc != 0) {
41 abort();
42 }
43 return owner;
44#elif defined(__linux__)
45 return syscall(__NR_gettid);
46#elif defined(_WIN32)
47 return GetCurrentThreadId();
48#endif
49}
50#endif // __ANDROID__
51
52#if !defined(_WIN32)
53
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080054void* thread_store_get( thread_store_t* store )
55{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080056 if (!store->has_tls)
57 return NULL;
58
59 return pthread_getspecific( store->tls );
60}
Yabin Cui4a6e5a32015-01-26 19:48:54 -080061
62extern void thread_store_set( thread_store_t* store,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080063 void* value,
64 thread_store_destruct_t destroy)
65{
66 pthread_mutex_lock( &store->lock );
67 if (!store->has_tls) {
68 if (pthread_key_create( &store->tls, destroy) != 0) {
69 pthread_mutex_unlock(&store->lock);
70 return;
71 }
72 store->has_tls = 1;
73 }
74 pthread_mutex_unlock( &store->lock );
75
76 pthread_setspecific( store->tls, value );
77}
78
Yabin Cui4a6e5a32015-01-26 19:48:54 -080079#else /* !defined(_WIN32) */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080080void* thread_store_get( thread_store_t* store )
81{
82 if (!store->has_tls)
83 return NULL;
Yabin Cui4a6e5a32015-01-26 19:48:54 -080084
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080085 return (void*) TlsGetValue( store->tls );
86}
87
88void thread_store_set( thread_store_t* store,
89 void* value,
90 thread_store_destruct_t destroy )
91{
92 /* XXX: can't use destructor on thread exit */
93 if (!store->lock_init) {
94 store->lock_init = -1;
95 InitializeCriticalSection( &store->lock );
96 store->lock_init = -2;
97 } else while (store->lock_init != -2) {
98 Sleep(10); /* 10ms */
99 }
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800100
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800101 EnterCriticalSection( &store->lock );
102 if (!store->has_tls) {
103 store->tls = TlsAlloc();
104 if (store->tls == TLS_OUT_OF_INDEXES) {
105 LeaveCriticalSection( &store->lock );
106 return;
107 }
108 store->has_tls = 1;
109 }
110 LeaveCriticalSection( &store->lock );
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800111
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800112 TlsSetValue( store->tls, value );
113}
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800114#endif /* !defined(_WIN32) */