blob: bb9fdb858ea9c38f9f2c2583cb1882490698edb6 [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>
35
36typedef struct {
37 pthread_mutex_t lock;
38 int has_tls;
39 pthread_key_t tls;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040} thread_store_t;
41
Dan Albert7dfb61d2015-03-20 13:46:28 -070042extern pid_t gettid();
43
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080044#define THREAD_STORE_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0, 0 }
45
Yabin Cui4a6e5a32015-01-26 19:48:54 -080046#else // !defined(_WIN32)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080047
48#include <windows.h>
49
50typedef struct {
51 int lock_init;
52 int has_tls;
53 DWORD tls;
54 CRITICAL_SECTION lock;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080055} thread_store_t;
56
57#define THREAD_STORE_INITIALIZER { 0, 0, 0, {0, 0, 0, 0, 0, 0} }
58
Yabin Cui4a6e5a32015-01-26 19:48:54 -080059#endif // !defined(_WIN32)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060
61typedef void (*thread_store_destruct_t)(void* value);
62
63extern void* thread_store_get(thread_store_t* store);
64
Yabin Cui4a6e5a32015-01-26 19:48:54 -080065extern void thread_store_set(thread_store_t* store,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080066 void* value,
67 thread_store_destruct_t destroy);
68
69/***********************************************************************/
70/***********************************************************************/
71/***** *****/
72/***** mutexes *****/
73/***** *****/
74/***********************************************************************/
75/***********************************************************************/
76
Yabin Cui4a6e5a32015-01-26 19:48:54 -080077#if !defined(_WIN32)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080078
79typedef pthread_mutex_t mutex_t;
80
81#define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
82
83static __inline__ void mutex_lock(mutex_t* lock)
84{
85 pthread_mutex_lock(lock);
86}
87static __inline__ void mutex_unlock(mutex_t* lock)
88{
89 pthread_mutex_unlock(lock);
90}
91static __inline__ int mutex_init(mutex_t* lock)
92{
93 return pthread_mutex_init(lock, NULL);
94}
95static __inline__ void mutex_destroy(mutex_t* lock)
96{
97 pthread_mutex_destroy(lock);
98}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080099
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800100#else // !defined(_WIN32)
101
102typedef struct {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800103 int init;
104 CRITICAL_SECTION lock[1];
105} mutex_t;
106
107#define MUTEX_INITIALIZER { 0, {{ NULL, 0, 0, NULL, NULL, 0 }} }
108
109static __inline__ void mutex_lock(mutex_t* lock)
110{
111 if (!lock->init) {
112 lock->init = 1;
113 InitializeCriticalSection( lock->lock );
114 lock->init = 2;
115 } else while (lock->init != 2)
116 Sleep(10);
117
118 EnterCriticalSection(lock->lock);
119}
120
121static __inline__ void mutex_unlock(mutex_t* lock)
122{
123 LeaveCriticalSection(lock->lock);
124}
125static __inline__ int mutex_init(mutex_t* lock)
126{
127 InitializeCriticalSection(lock->lock);
128 lock->init = 2;
129 return 0;
130}
131static __inline__ void mutex_destroy(mutex_t* lock)
132{
133 if (lock->init) {
134 lock->init = 0;
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800135 DeleteCriticalSection(lock->lock);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800136 }
137}
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800138#endif // !defined(_WIN32)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800139
140#ifdef __cplusplus
141}
142#endif
143
144#endif /* _LIBS_CUTILS_THREADS_H */