blob: 3133cdb04c506d7d71a1848052f542a0928f9307 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -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#ifndef _LIBS_CUTILS_THREADS_H
18#define _LIBS_CUTILS_THREADS_H
19
20#ifdef __cplusplus
21extern "C" {
22#endif
23
24/***********************************************************************/
25/***********************************************************************/
26/***** *****/
27/***** local thread storage *****/
28/***** *****/
29/***********************************************************************/
30/***********************************************************************/
31
Yabin Cui4a6e5a32015-01-26 19:48:54 -080032#if !defined(_WIN32)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080033
34#include <pthread.h>
Christopher Ferrisfc3576f2015-03-23 21:35:56 -070035#include <sys/types.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080036
37typedef struct {
38 pthread_mutex_t lock;
39 int has_tls;
40 pthread_key_t tls;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080041} thread_store_t;
42
Dan Albert7dfb61d2015-03-20 13:46:28 -070043extern pid_t gettid();
44
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080045#define THREAD_STORE_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0, 0 }
46
Yabin Cui4a6e5a32015-01-26 19:48:54 -080047#else // !defined(_WIN32)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080048
49#include <windows.h>
50
51typedef struct {
52 int lock_init;
53 int has_tls;
54 DWORD tls;
55 CRITICAL_SECTION lock;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080056} thread_store_t;
57
58#define THREAD_STORE_INITIALIZER { 0, 0, 0, {0, 0, 0, 0, 0, 0} }
59
Yabin Cui4a6e5a32015-01-26 19:48:54 -080060#endif // !defined(_WIN32)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080061
62typedef void (*thread_store_destruct_t)(void* value);
63
64extern void* thread_store_get(thread_store_t* store);
65
Yabin Cui4a6e5a32015-01-26 19:48:54 -080066extern void thread_store_set(thread_store_t* store,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080067 void* value,
68 thread_store_destruct_t destroy);
69
70/***********************************************************************/
71/***********************************************************************/
72/***** *****/
73/***** mutexes *****/
74/***** *****/
75/***********************************************************************/
76/***********************************************************************/
77
Yabin Cui4a6e5a32015-01-26 19:48:54 -080078#if !defined(_WIN32)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080079
80typedef pthread_mutex_t mutex_t;
81
82#define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
83
84static __inline__ void mutex_lock(mutex_t* lock)
85{
86 pthread_mutex_lock(lock);
87}
88static __inline__ void mutex_unlock(mutex_t* lock)
89{
90 pthread_mutex_unlock(lock);
91}
92static __inline__ int mutex_init(mutex_t* lock)
93{
94 return pthread_mutex_init(lock, NULL);
95}
96static __inline__ void mutex_destroy(mutex_t* lock)
97{
98 pthread_mutex_destroy(lock);
99}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800100
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800101#else // !defined(_WIN32)
102
103typedef struct {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800104 int init;
105 CRITICAL_SECTION lock[1];
106} mutex_t;
107
108#define MUTEX_INITIALIZER { 0, {{ NULL, 0, 0, NULL, NULL, 0 }} }
109
110static __inline__ void mutex_lock(mutex_t* lock)
111{
112 if (!lock->init) {
113 lock->init = 1;
114 InitializeCriticalSection( lock->lock );
115 lock->init = 2;
116 } else while (lock->init != 2)
117 Sleep(10);
118
119 EnterCriticalSection(lock->lock);
120}
121
122static __inline__ void mutex_unlock(mutex_t* lock)
123{
124 LeaveCriticalSection(lock->lock);
125}
126static __inline__ int mutex_init(mutex_t* lock)
127{
128 InitializeCriticalSection(lock->lock);
129 lock->init = 2;
130 return 0;
131}
132static __inline__ void mutex_destroy(mutex_t* lock)
133{
134 if (lock->init) {
135 lock->init = 0;
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800136 DeleteCriticalSection(lock->lock);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800137 }
138}
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800139#endif // !defined(_WIN32)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800140
141#ifdef __cplusplus
142}
143#endif
144
145#endif /* _LIBS_CUTILS_THREADS_H */