blob: 5f5577b426983e12f65594ea3ad1f9c76997b9c0 [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
Christopher Ferrisfc3576f2015-03-23 21:35:56 -070024#include <stdint.h>
25#include <stdlib.h>
Dan Albert7dfb61d2015-03-20 13:46:28 -070026#include <sys/syscall.h>
27#include <sys/time.h>
Dan Albert48306022015-03-23 16:38:34 -070028#include <unistd.h>
Dan Albert7dfb61d2015-03-20 13:46:28 -070029#elif defined(__linux__) && !defined(__ANDROID__)
30#include <syscall.h>
31#include <unistd.h>
32#elif defined(_WIN32)
33#include <Windows.h>
34#endif
35
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080036void* thread_store_get( thread_store_t* store )
37{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080038 if (!store->has_tls)
39 return NULL;
40
41 return pthread_getspecific( store->tls );
42}
Yabin Cui4a6e5a32015-01-26 19:48:54 -080043
44extern void thread_store_set( thread_store_t* store,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080045 void* value,
46 thread_store_destruct_t destroy)
47{
48 pthread_mutex_lock( &store->lock );
49 if (!store->has_tls) {
50 if (pthread_key_create( &store->tls, destroy) != 0) {
51 pthread_mutex_unlock(&store->lock);
52 return;
53 }
54 store->has_tls = 1;
55 }
56 pthread_mutex_unlock( &store->lock );
57
58 pthread_setspecific( store->tls, value );
59}
60
Dan Albert7dfb61d2015-03-20 13:46:28 -070061// No definition needed for Android because we'll just pick up bionic's copy.
62#ifndef __ANDROID__
63pid_t gettid() {
64#if defined(__APPLE__)
65 uint64_t owner;
Dan Alberte3081042015-03-23 16:07:42 -070066 int rc = pthread_threadid_np(NULL, &owner);
67 if (rc != 0) {
68 abort();
69 }
Dan Albert7dfb61d2015-03-20 13:46:28 -070070 return owner;
71#elif defined(__linux__)
72 return syscall(__NR_gettid);
73#elif defined(_WIN32)
74 return (pid_t)GetCurrentThreadId();
75#endif
76}
77#endif // __ANDROID__
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) */