blob: 5727494079f1431fdef106e48d240a61210eb643 [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
Dan Albertcd206b32015-04-29 17:13:32 -070020#include <sys/types.h>
21
22#if !defined(_WIN32)
23#include <pthread.h>
24#else
25#include <windows.h>
26#endif
27
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080028#ifdef __cplusplus
29extern "C" {
30#endif
31
32/***********************************************************************/
33/***********************************************************************/
34/***** *****/
35/***** local thread storage *****/
36/***** *****/
37/***********************************************************************/
38/***********************************************************************/
39
Dan Albertcd206b32015-04-29 17:13:32 -070040extern pid_t gettid();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080041
Dan Albertcd206b32015-04-29 17:13:32 -070042#if !defined(_WIN32)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080043
44typedef struct {
45 pthread_mutex_t lock;
46 int has_tls;
47 pthread_key_t tls;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080048} thread_store_t;
49
50#define THREAD_STORE_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0, 0 }
51
Yabin Cui4a6e5a32015-01-26 19:48:54 -080052#else // !defined(_WIN32)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080053
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080054typedef struct {
55 int lock_init;
56 int has_tls;
57 DWORD tls;
58 CRITICAL_SECTION lock;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080059} thread_store_t;
60
61#define THREAD_STORE_INITIALIZER { 0, 0, 0, {0, 0, 0, 0, 0, 0} }
62
Yabin Cui4a6e5a32015-01-26 19:48:54 -080063#endif // !defined(_WIN32)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080064
65typedef void (*thread_store_destruct_t)(void* value);
66
67extern void* thread_store_get(thread_store_t* store);
68
Yabin Cui4a6e5a32015-01-26 19:48:54 -080069extern void thread_store_set(thread_store_t* store,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080070 void* value,
71 thread_store_destruct_t destroy);
72
73/***********************************************************************/
74/***********************************************************************/
75/***** *****/
76/***** mutexes *****/
77/***** *****/
78/***********************************************************************/
79/***********************************************************************/
80
Yabin Cui4a6e5a32015-01-26 19:48:54 -080081#if !defined(_WIN32)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080082
83typedef pthread_mutex_t mutex_t;
84
85#define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
86
87static __inline__ void mutex_lock(mutex_t* lock)
88{
89 pthread_mutex_lock(lock);
90}
91static __inline__ void mutex_unlock(mutex_t* lock)
92{
93 pthread_mutex_unlock(lock);
94}
95static __inline__ int mutex_init(mutex_t* lock)
96{
97 return pthread_mutex_init(lock, NULL);
98}
99static __inline__ void mutex_destroy(mutex_t* lock)
100{
101 pthread_mutex_destroy(lock);
102}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800103
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800104#else // !defined(_WIN32)
105
106typedef struct {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800107 int init;
108 CRITICAL_SECTION lock[1];
109} mutex_t;
110
111#define MUTEX_INITIALIZER { 0, {{ NULL, 0, 0, NULL, NULL, 0 }} }
112
113static __inline__ void mutex_lock(mutex_t* lock)
114{
115 if (!lock->init) {
116 lock->init = 1;
117 InitializeCriticalSection( lock->lock );
118 lock->init = 2;
119 } else while (lock->init != 2)
120 Sleep(10);
121
122 EnterCriticalSection(lock->lock);
123}
124
125static __inline__ void mutex_unlock(mutex_t* lock)
126{
127 LeaveCriticalSection(lock->lock);
128}
129static __inline__ int mutex_init(mutex_t* lock)
130{
131 InitializeCriticalSection(lock->lock);
132 lock->init = 2;
133 return 0;
134}
135static __inline__ void mutex_destroy(mutex_t* lock)
136{
137 if (lock->init) {
138 lock->init = 0;
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800139 DeleteCriticalSection(lock->lock);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800140 }
141}
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800142#endif // !defined(_WIN32)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800143
144#ifdef __cplusplus
145}
146#endif
147
148#endif /* _LIBS_CUTILS_THREADS_H */