The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* |
| 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 Albert | cd206b3 | 2015-04-29 17:13:32 -0700 | [diff] [blame^] | 20 | #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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 28 | #ifdef __cplusplus |
| 29 | extern "C" { |
| 30 | #endif |
| 31 | |
| 32 | /***********************************************************************/ |
| 33 | /***********************************************************************/ |
| 34 | /***** *****/ |
| 35 | /***** local thread storage *****/ |
| 36 | /***** *****/ |
| 37 | /***********************************************************************/ |
| 38 | /***********************************************************************/ |
| 39 | |
Dan Albert | cd206b3 | 2015-04-29 17:13:32 -0700 | [diff] [blame^] | 40 | extern pid_t gettid(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 41 | |
Dan Albert | cd206b3 | 2015-04-29 17:13:32 -0700 | [diff] [blame^] | 42 | #if !defined(_WIN32) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 43 | |
| 44 | typedef struct { |
| 45 | pthread_mutex_t lock; |
| 46 | int has_tls; |
| 47 | pthread_key_t tls; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 48 | } thread_store_t; |
| 49 | |
| 50 | #define THREAD_STORE_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0, 0 } |
| 51 | |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 52 | #else // !defined(_WIN32) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 53 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 54 | typedef struct { |
| 55 | int lock_init; |
| 56 | int has_tls; |
| 57 | DWORD tls; |
| 58 | CRITICAL_SECTION lock; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 59 | } thread_store_t; |
| 60 | |
| 61 | #define THREAD_STORE_INITIALIZER { 0, 0, 0, {0, 0, 0, 0, 0, 0} } |
| 62 | |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 63 | #endif // !defined(_WIN32) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 64 | |
| 65 | typedef void (*thread_store_destruct_t)(void* value); |
| 66 | |
| 67 | extern void* thread_store_get(thread_store_t* store); |
| 68 | |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 69 | extern void thread_store_set(thread_store_t* store, |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 70 | void* value, |
| 71 | thread_store_destruct_t destroy); |
| 72 | |
| 73 | /***********************************************************************/ |
| 74 | /***********************************************************************/ |
| 75 | /***** *****/ |
| 76 | /***** mutexes *****/ |
| 77 | /***** *****/ |
| 78 | /***********************************************************************/ |
| 79 | /***********************************************************************/ |
| 80 | |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 81 | #if !defined(_WIN32) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 82 | |
| 83 | typedef pthread_mutex_t mutex_t; |
| 84 | |
| 85 | #define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER |
| 86 | |
| 87 | static __inline__ void mutex_lock(mutex_t* lock) |
| 88 | { |
| 89 | pthread_mutex_lock(lock); |
| 90 | } |
| 91 | static __inline__ void mutex_unlock(mutex_t* lock) |
| 92 | { |
| 93 | pthread_mutex_unlock(lock); |
| 94 | } |
| 95 | static __inline__ int mutex_init(mutex_t* lock) |
| 96 | { |
| 97 | return pthread_mutex_init(lock, NULL); |
| 98 | } |
| 99 | static __inline__ void mutex_destroy(mutex_t* lock) |
| 100 | { |
| 101 | pthread_mutex_destroy(lock); |
| 102 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 103 | |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 104 | #else // !defined(_WIN32) |
| 105 | |
| 106 | typedef struct { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 107 | int init; |
| 108 | CRITICAL_SECTION lock[1]; |
| 109 | } mutex_t; |
| 110 | |
| 111 | #define MUTEX_INITIALIZER { 0, {{ NULL, 0, 0, NULL, NULL, 0 }} } |
| 112 | |
| 113 | static __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 | |
| 125 | static __inline__ void mutex_unlock(mutex_t* lock) |
| 126 | { |
| 127 | LeaveCriticalSection(lock->lock); |
| 128 | } |
| 129 | static __inline__ int mutex_init(mutex_t* lock) |
| 130 | { |
| 131 | InitializeCriticalSection(lock->lock); |
| 132 | lock->init = 2; |
| 133 | return 0; |
| 134 | } |
| 135 | static __inline__ void mutex_destroy(mutex_t* lock) |
| 136 | { |
| 137 | if (lock->init) { |
| 138 | lock->init = 0; |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 139 | DeleteCriticalSection(lock->lock); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 140 | } |
| 141 | } |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 142 | #endif // !defined(_WIN32) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 143 | |
| 144 | #ifdef __cplusplus |
| 145 | } |
| 146 | #endif |
| 147 | |
| 148 | #endif /* _LIBS_CUTILS_THREADS_H */ |