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