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