Commit | Line | Data |
---|---|---|
1e494cf4 JB |
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 | ||
32 | #ifdef HAVE_PTHREADS | |
33 | ||
34 | #include <pthread.h> | |
35 | ||
36 | typedef struct { | |
37 | pthread_mutex_t lock; | |
38 | int has_tls; | |
39 | pthread_key_t tls; | |
40 | ||
41 | } thread_store_t; | |
42 | ||
43 | #define THREAD_STORE_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0, 0 } | |
44 | ||
45 | #elif defined HAVE_WIN32_THREADS | |
46 | ||
47 | #include <windows.h> | |
48 | ||
49 | typedef struct { | |
50 | int lock_init; | |
51 | int has_tls; | |
52 | DWORD tls; | |
53 | CRITICAL_SECTION lock; | |
54 | ||
55 | } thread_store_t; | |
56 | ||
57 | #define THREAD_STORE_INITIALIZER { 0, 0, 0, {0, 0, 0, 0, 0, 0} } | |
58 | ||
59 | #else | |
60 | # error "no thread_store_t implementation for your platform !!" | |
61 | #endif | |
62 | ||
63 | typedef void (*thread_store_destruct_t)(void* value); | |
64 | ||
65 | extern void* thread_store_get(thread_store_t* store); | |
66 | ||
67 | extern void thread_store_set(thread_store_t* store, | |
68 | void* value, | |
69 | thread_store_destruct_t destroy); | |
70 | ||
71 | /***********************************************************************/ | |
72 | /***********************************************************************/ | |
73 | /***** *****/ | |
74 | /***** mutexes *****/ | |
75 | /***** *****/ | |
76 | /***********************************************************************/ | |
77 | /***********************************************************************/ | |
78 | ||
79 | #ifdef HAVE_PTHREADS | |
80 | ||
81 | typedef pthread_mutex_t mutex_t; | |
82 | ||
83 | #define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER | |
84 | ||
85 | static __inline__ void mutex_lock(mutex_t* lock) | |
86 | { | |
87 | pthread_mutex_lock(lock); | |
88 | } | |
89 | static __inline__ void mutex_unlock(mutex_t* lock) | |
90 | { | |
91 | pthread_mutex_unlock(lock); | |
92 | } | |
93 | static __inline__ int mutex_init(mutex_t* lock) | |
94 | { | |
95 | return pthread_mutex_init(lock, NULL); | |
96 | } | |
97 | static __inline__ void mutex_destroy(mutex_t* lock) | |
98 | { | |
99 | pthread_mutex_destroy(lock); | |
100 | } | |
101 | #endif | |
102 | ||
103 | #ifdef HAVE_WIN32_THREADS | |
104 | typedef struct { | |
105 | int init; | |
106 | CRITICAL_SECTION lock[1]; | |
107 | } mutex_t; | |
108 | ||
109 | #define MUTEX_INITIALIZER { 0, {{ NULL, 0, 0, NULL, NULL, 0 }} } | |
110 | ||
111 | static __inline__ void mutex_lock(mutex_t* lock) | |
112 | { | |
113 | if (!lock->init) { | |
114 | lock->init = 1; | |
115 | InitializeCriticalSection( lock->lock ); | |
116 | lock->init = 2; | |
117 | } else while (lock->init != 2) | |
118 | Sleep(10); | |
119 | ||
120 | EnterCriticalSection(lock->lock); | |
121 | } | |
122 | ||
123 | static __inline__ void mutex_unlock(mutex_t* lock) | |
124 | { | |
125 | LeaveCriticalSection(lock->lock); | |
126 | } | |
127 | static __inline__ int mutex_init(mutex_t* lock) | |
128 | { | |
129 | InitializeCriticalSection(lock->lock); | |
130 | lock->init = 2; | |
131 | return 0; | |
132 | } | |
133 | static __inline__ void mutex_destroy(mutex_t* lock) | |
134 | { | |
135 | if (lock->init) { | |
136 | lock->init = 0; | |
137 | DeleteCriticalSection(lock->lock); | |
138 | } | |
139 | } | |
140 | #endif | |
141 | ||
142 | #ifdef __cplusplus | |
143 | } | |
144 | #endif | |
145 | ||
146 | #endif /* _LIBS_CUTILS_THREADS_H */ |