2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef _LIBS_CUTILS_THREADS_H
18 #define _LIBS_CUTILS_THREADS_H
24 /***********************************************************************/
25 /***********************************************************************/
27 /***** local thread storage *****/
29 /***********************************************************************/
30 /***********************************************************************/
43 #define THREAD_STORE_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0, 0 }
45 #elif defined HAVE_WIN32_THREADS
53 CRITICAL_SECTION lock
;
57 #define THREAD_STORE_INITIALIZER { 0, 0, 0, {0, 0, 0, 0, 0, 0} }
60 # error "no thread_store_t implementation for your platform !!"
63 typedef void (*thread_store_destruct_t
)(void* value
);
65 extern void* thread_store_get(thread_store_t
* store
);
67 extern void thread_store_set(thread_store_t
* store
,
69 thread_store_destruct_t destroy
);
71 /***********************************************************************/
72 /***********************************************************************/
76 /***********************************************************************/
77 /***********************************************************************/
81 typedef pthread_mutex_t mutex_t
;
83 #define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
85 static __inline__
void mutex_lock(mutex_t
* lock
)
87 pthread_mutex_lock(lock
);
89 static __inline__
void mutex_unlock(mutex_t
* lock
)
91 pthread_mutex_unlock(lock
);
93 static __inline__
int mutex_init(mutex_t
* lock
)
95 return pthread_mutex_init(lock
, NULL
);
97 static __inline__
void mutex_destroy(mutex_t
* lock
)
99 pthread_mutex_destroy(lock
);
103 #ifdef HAVE_WIN32_THREADS
106 CRITICAL_SECTION lock
[1];
109 #define MUTEX_INITIALIZER { 0, {{ NULL, 0, 0, NULL, NULL, 0 }} }
111 static __inline__
void mutex_lock(mutex_t
* lock
)
115 InitializeCriticalSection( lock
->lock
);
117 } else while (lock
->init
!= 2)
120 EnterCriticalSection(lock
->lock
);
123 static __inline__
void mutex_unlock(mutex_t
* lock
)
125 LeaveCriticalSection(lock
->lock
);
127 static __inline__
int mutex_init(mutex_t
* lock
)
129 InitializeCriticalSection(lock
->lock
);
133 static __inline__
void mutex_destroy(mutex_t
* lock
)
137 DeleteCriticalSection(lock
->lock
);
146 #endif /* _LIBS_CUTILS_THREADS_H */