p_thread.h

Go to the documentation of this file.
00001 /**************************************************************************
00002  * 
00003  * Copyright 1999-2006 Brian Paul
00004  * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
00005  * All Rights Reserved.
00006  * 
00007  * Permission is hereby granted, free of charge, to any person obtaining a
00008  * copy of this software and associated documentation files (the "Software"),
00009  * to deal in the Software without restriction, including without limitation
00010  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00011  * and/or sell copies of the Software, and to permit persons to whom the
00012  * Software is furnished to do so, subject to the following conditions:
00013  *
00014  * The above copyright notice and this permission notice shall be included
00015  * in all copies or substantial portions of the Software.
00016  *
00017  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00018  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00019  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
00020  * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
00021  * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00022  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00023  * 
00024  **************************************************************************/
00025 
00026 
00032 #ifndef _P_THREAD2_H_
00033 #define _P_THREAD2_H_
00034 
00035 
00036 #include "pipe/p_compiler.h"
00037 #include "pipe/p_debug.h"
00038 
00039 
00040 #if defined(PIPE_OS_LINUX)
00041 
00042 #include <pthread.h> /* POSIX threads headers */
00043 #include <stdio.h> /* for perror() */
00044 
00045 typedef pthread_t pipe_thread;
00046 
00047 #define PIPE_THREAD_ROUTINE( name, param ) \
00048    void *name( void *param )
00049 
00050 static INLINE pipe_thread pipe_thread_create( void *(* routine)( void *), void *param )
00051 {
00052    pipe_thread thread;
00053    if (pthread_create( &thread, NULL, routine, param ))
00054       return 0;
00055    return thread;
00056 }
00057 
00058 static INLINE int pipe_thread_wait( pipe_thread thread )
00059 {
00060    return pthread_join( thread, NULL );
00061 }
00062 
00063 static INLINE int pipe_thread_destroy( pipe_thread thread )
00064 {
00065    return pthread_detach( thread );
00066 }
00067 
00068 typedef pthread_mutex_t pipe_mutex;
00069 typedef pthread_cond_t pipe_condvar;
00070 
00071 #define pipe_static_mutex(mutex) \
00072    static pipe_mutex mutex = PTHREAD_MUTEX_INITIALIZER
00073 
00074 #define pipe_mutex_init(mutex) \
00075    pthread_mutex_init(&(mutex), NULL)
00076 
00077 #define pipe_mutex_destroy(mutex) \
00078    pthread_mutex_destroy(&(mutex))
00079 
00080 #define pipe_mutex_lock(mutex) \
00081    (void) pthread_mutex_lock(&(mutex))
00082 
00083 #define pipe_mutex_unlock(mutex) \
00084    (void) pthread_mutex_unlock(&(mutex))
00085 
00086 #define pipe_static_condvar(mutex) \
00087    static pipe_condvar mutex = PTHREAD_COND_INITIALIZER
00088 
00089 #define pipe_condvar_init(cond) \
00090    pthread_cond_init(&(cond), NULL)
00091 
00092 #define pipe_condvar_destroy(cond) \
00093    pthread_cond_destroy(&(cond))
00094 
00095 #define pipe_condvar_wait(cond, mutex) \
00096   pthread_cond_wait(&(cond), &(mutex))
00097 
00098 #define pipe_condvar_signal(cond) \
00099   pthread_cond_signal(&(cond))
00100 
00101 #define pipe_condvar_broadcast(cond) \
00102   pthread_cond_broadcast(&(cond))
00103 
00104 
00105 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
00106 
00107 #include <windows.h>
00108 
00109 typedef HANDLE pipe_thread;
00110 
00111 #define PIPE_THREAD_ROUTINE( name, param ) \
00112    void * WINAPI name( void *param )
00113 
00114 static INLINE pipe_thread pipe_thread_create( void *(WINAPI * routine)( void *), void *param )
00115 {
00116    DWORD id;
00117    return CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE) routine, param, 0, &id );
00118 }
00119 
00120 static INLINE int pipe_thread_wait( pipe_thread thread )
00121 {
00122    if (WaitForSingleObject( thread, INFINITE ) == WAIT_OBJECT_0)
00123       return 0;
00124    return -1;
00125 }
00126 
00127 static INLINE int pipe_thread_destroy( pipe_thread thread )
00128 {
00129    if (CloseHandle( thread ))
00130       return 0;
00131    return -1;
00132 }
00133 
00134 typedef CRITICAL_SECTION pipe_mutex;
00135 
00136 #define pipe_static_mutex(name) \
00137    /*static*/ pipe_mutex name = {0,0,0,0,0,0}
00138 
00139 #define pipe_mutex_init(name) \
00140    InitializeCriticalSection(&name)
00141 
00142 #define pipe_mutex_destroy(name) \
00143    DeleteCriticalSection(&name)
00144 
00145 #define pipe_mutex_lock(name) \
00146    EnterCriticalSection(&name)
00147 
00148 #define pipe_mutex_unlock(name) \
00149    LeaveCriticalSection(&name)
00150 
00151 /* XXX: dummy definitions, make it compile */
00152 
00153 typedef unsigned pipe_condvar;
00154 
00155 #define pipe_condvar_init(condvar) \
00156    (void) condvar
00157 
00158 #define pipe_condvar_broadcast(condvar) \
00159    (void) condvar
00160 
00161 #else
00162 
00165 typedef unsigned pipe_thread;
00166 typedef unsigned pipe_mutex;
00167 typedef unsigned pipe_condvar;
00168 
00169 #define pipe_static_mutex(mutex) \
00170    static pipe_mutex mutex = 0
00171 
00172 #define pipe_mutex_init(mutex) \
00173    (void) mutex
00174 
00175 #define pipe_mutex_destroy(mutex) \
00176    (void) mutex
00177 
00178 #define pipe_mutex_lock(mutex) \
00179    (void) mutex
00180 
00181 #define pipe_mutex_unlock(mutex) \
00182    (void) mutex
00183 
00184 #define pipe_static_condvar(condvar) \
00185    static unsigned condvar = 0
00186 
00187 #define pipe_condvar_init(condvar) \
00188    (void) condvar
00189 
00190 #define pipe_condvar_destroy(condvar) \
00191    (void) condvar
00192 
00193 #define pipe_condvar_wait(condvar, mutex) \
00194    (void) condvar
00195 
00196 #define pipe_condvar_signal(condvar) \
00197    (void) condvar
00198 
00199 #define pipe_condvar_broadcast(condvar) \
00200    (void) condvar
00201 
00202 
00203 #endif  /* PIPE_OS_? */
00204 
00205 
00206 
00207 /*
00208  * Thread-specific data.
00209  */
00210 
00211 typedef struct {
00212 #if defined(PIPE_OS_LINUX)
00213    pthread_key_t key;
00214 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
00215    DWORD key;
00216 #endif
00217    int initMagic;
00218 } pipe_tsd;
00219 
00220 
00221 #define PIPE_TSD_INIT_MAGIC 0xff8adc98
00222 
00223 
00224 static INLINE void
00225 pipe_tsd_init(pipe_tsd *tsd)
00226 {
00227 #if defined(PIPE_OS_LINUX)
00228    if (pthread_key_create(&tsd->key, NULL/*free*/) != 0) {
00229       perror("pthread_key_create(): failed to allocate key for thread specific data");
00230       exit(-1);
00231    }
00232 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
00233    assert(0);
00234 #endif
00235    tsd->initMagic = PIPE_TSD_INIT_MAGIC;
00236 }
00237 
00238 static INLINE void *
00239 pipe_tsd_get(pipe_tsd *tsd)
00240 {
00241    if (tsd->initMagic != (int) PIPE_TSD_INIT_MAGIC) {
00242       pipe_tsd_init(tsd);
00243    }
00244 #if defined(PIPE_OS_LINUX)
00245    return pthread_getspecific(tsd->key);
00246 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
00247    assert(0);
00248    return NULL;
00249 #else
00250    assert(0);
00251    return NULL;
00252 #endif
00253 }
00254 
00255 static INLINE void
00256 pipe_tsd_set(pipe_tsd *tsd, void *value)
00257 {
00258    if (tsd->initMagic != (int) PIPE_TSD_INIT_MAGIC) {
00259       pipe_tsd_init(tsd);
00260    }
00261 #if defined(PIPE_OS_LINUX)
00262    if (pthread_setspecific(tsd->key, value) != 0) {
00263       perror("pthread_set_specific() failed");
00264       exit(-1);
00265    }
00266 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
00267    assert(0);
00268 #else
00269    assert(0);
00270 #endif
00271 }
00272 
00273 
00274 
00275 #endif /* _P_THREAD2_H_ */

Generated on Tue Sep 29 06:25:17 2009 for Gallium3D by  doxygen 1.5.4