1 /**************************************************************************
3 * Copyright 1999-2006 Brian Paul
4 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 **************************************************************************/
28 * Thread, mutex, condition var and thread-specific data functions.
36 #include "pipe/p_compiler.h"
39 #if defined(PIPE_OS_LINUX)
41 #include <pthread.h> /* POSIX threads headers */
42 #include <stdio.h> /* for perror() */
45 typedef pthread_t pipe_thread
;
46 typedef pthread_mutex_t pipe_mutex
;
47 typedef pthread_cond_t pipe_condvar
;
49 #define pipe_static_mutex(mutex) \
50 static pipe_mutex mutex = PTHREAD_MUTEX_INITIALIZER
52 #define pipe_mutex_init(mutex) \
53 pthread_mutex_init(&(mutex), NULL)
55 #define pipe_mutex_destroy(mutex) \
56 pthread_mutex_destroy(&(mutex))
58 #define pipe_mutex_lock(mutex) \
59 (void) pthread_mutex_lock(&(mutex))
61 #define pipe_mutex_unlock(mutex) \
62 (void) pthread_mutex_unlock(&(mutex))
64 #define pipe_static_condvar(mutex) \
65 static pipe_condvar mutex = PTHREAD_COND_INITIALIZER
67 #define pipe_condvar_init(cond) \
68 pthread_cond_init(&(cond), NULL)
70 #define pipe_condvar_destroy(cond) \
71 pthread_cond_destroy(&(cond))
73 #define pipe_condvar_wait(cond, mutex) \
74 pthread_cond_wait(&(cond), &(mutex))
76 #define pipe_condvar_signal(cond) \
77 pthread_cond_signal(&(cond))
79 #define pipe_condvar_broadcast(cond) \
80 pthread_cond_broadcast(&(cond))
83 #elif defined(PIPE_OS_WINDOWS)
87 typedef HANDLE pipe_thread
;
88 typedef CRITICAL_SECTION pipe_mutex
;
90 #define pipe_static_mutex(name) \
91 /*static*/ pipe_mutex name = {0,0,0,0,0,0}
93 #define pipe_mutex_init(name) \
94 InitializeCriticalSection(&name)
96 #define pipe_mutex_destroy(name) \
97 DeleteCriticalSection(&name)
99 #define pipe_mutex_lock(name) \
100 EnterCriticalSection(&name)
102 #define pipe_mutex_unlock(name) \
103 LeaveCriticalSection(&name)
105 /* XXX: dummy definitions, make it compile */
107 typedef unsigned pipe_condvar
;
109 #define pipe_condvar_init(condvar) \
112 #define pipe_condvar_broadcast(condvar) \
117 /** Dummy definitions */
119 typedef unsigned pipe_thread
;
120 typedef unsigned pipe_mutex
;
121 typedef unsigned pipe_condvar
;
122 typedef unsigned pipe_tsd
;
124 #define pipe_static_mutex(mutex) \
125 static pipe_mutex mutex = 0
127 #define pipe_mutex_init(mutex) \
130 #define pipe_mutex_destroy(mutex) \
133 #define pipe_mutex_lock(mutex) \
136 #define pipe_mutex_unlock(mutex) \
139 #define pipe_static_condvar(condvar) \
140 static _glthread_Cond condvar = 0
142 #define pipe_condvar_init(condvar) \
145 #define pipe_condvar_destroy(condvar) \
148 #define pipe_condvar_wait(condvar, mutex) \
151 #define pipe_condvar_signal(condvar) \
154 #define pipe_condvar_broadcast(condvar) \
158 #endif /* PIPE_OS_? */
163 * Thread-specific data.
167 #if defined(PIPE_OS_LINUX)
169 #elif defined(PIPE_OS_WINDOWS)
176 #define PIPE_TSD_INIT_MAGIC 0xff8adc98
180 pipe_tsd_init(pipe_tsd
*tsd
)
182 #if defined(PIPE_OS_LINUX)
183 if (pthread_key_create(&tsd
->key
, NULL
/*free*/) != 0) {
184 perror("pthread_key_create(): failed to allocate key for thread specific data");
187 #elif defined(PIPE_OS_WINDOWS)
190 tsd
->initMagic
= PIPE_TSD_INIT_MAGIC
;
194 pipe_tsd_get(pipe_tsd
*tsd
)
196 if (tsd
->initMagic
!= (int) PIPE_TSD_INIT_MAGIC
) {
199 #if defined(PIPE_OS_LINUX)
200 return pthread_getspecific(tsd
->key
);
201 #elif defined(PIPE_OS_WINDOWS)
211 pipe_tsd_set(pipe_tsd
*tsd
, void *value
)
213 if (tsd
->initMagic
!= (int) PIPE_TSD_INIT_MAGIC
) {
216 #if defined(PIPE_OS_LINUX)
217 if (pthread_setspecific(tsd
->key
, value
) != 0) {
218 perror("pthread_set_specific() failed");
221 #elif defined(PIPE_OS_WINDOWS)
230 #endif /* _P_THREAD2_H_ */