1 /**************************************************************************
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, TX., USA
5 * Copyright 2009 VMware, Inc., Palo Alto, CA., USA
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sub license, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
16 * The above copyright notice and this permission notice (including the
17 * next paragraph) shall be included in all copies or substantial portions
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
23 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
24 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
25 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
26 * USE OR OTHER DEALINGS IN THE SOFTWARE.
28 **************************************************************************/
31 * Thomas Hellström <thomas-at-tungstengraphics-dot-com>
41 #include "wsbm_driver.h"
43 struct _WsbmThreadFuncs *wsbmCurThreadFunc = NULL;
44 struct _WsbmVNodeFuncs *wsbmCurVNodeFunc = NULL;
47 * Single-threaded implementation.
51 n_mutexInit(struct _WsbmMutex *mutex, struct _WsbmThreadFuncs *func)
58 n_condInit(struct _WsbmCond *cond, struct _WsbmThreadFuncs *func)
65 n_mutexNone(struct _WsbmMutex *mutex)
71 n_condNone(struct _WsbmCond *cond)
77 n_condWait(struct _WsbmCond *cond, struct _WsbmMutex *mutex)
82 static struct _WsbmThreadFuncs nullFunc = {
83 .mutexInit = n_mutexInit,
84 .mutexFree = n_mutexNone,
85 .mutexLock = n_mutexNone,
86 .mutexUnlock = n_mutexNone,
87 .condInit = n_condInit,
88 .condFree = n_condNone,
89 .condWait = n_condWait,
90 .condBroadcast = n_condNone
93 struct _WsbmThreadFuncs *
94 wsbmNullThreadFuncs(void)
99 #if (HAVE_PTHREADS == 1)
103 * pthreads implementation:
108 struct _WsbmThreadFuncs *func;
109 pthread_mutex_t mutex;
114 struct _WsbmThreadFuncs *func;
118 static inline struct _WsbmPMutex *
119 pMutexConvert(struct _WsbmMutex *m)
121 union _PMutexConverter
123 struct _WsbmMutex wm;
124 struct _WsbmPMutex pm;
125 } *um = containerOf(m, union _PMutexConverter, wm);
130 static inline struct _WsbmPCond *
131 pCondConvert(struct _WsbmCond *c)
133 union _PCondConverter
136 struct _WsbmPCond pc;
137 } *uc = containerOf(c, union _PCondConverter, wc);
143 p_mutexInit(struct _WsbmMutex *mutex, struct _WsbmThreadFuncs *func)
145 struct _WsbmPMutex *pMutex = pMutexConvert(mutex);
147 if (sizeof(struct _WsbmMutex) < sizeof(struct _WsbmPMutex))
151 pthread_mutex_init(&pMutex->mutex, NULL);
156 p_mutexFree(struct _WsbmMutex *mutex)
158 struct _WsbmPMutex *pMutex = pMutexConvert(mutex);
160 pthread_mutex_destroy(&pMutex->mutex);
164 p_mutexLock(struct _WsbmMutex *mutex)
166 struct _WsbmPMutex *pMutex = pMutexConvert(mutex);
168 pthread_mutex_lock(&pMutex->mutex);
172 p_mutexUnlock(struct _WsbmMutex *mutex)
174 struct _WsbmPMutex *pMutex = pMutexConvert(mutex);
176 pthread_mutex_unlock(&pMutex->mutex);
180 p_condInit(struct _WsbmCond *cond, struct _WsbmThreadFuncs *func)
182 struct _WsbmPCond *pCond = pCondConvert(cond);
184 if (sizeof(struct _WsbmCond) < sizeof(struct _WsbmPCond))
188 pthread_cond_init(&pCond->cond, NULL);
193 p_condFree(struct _WsbmCond *cond)
195 struct _WsbmPCond *pCond = pCondConvert(cond);
197 pthread_cond_destroy(&pCond->cond);
201 p_condBroadcast(struct _WsbmCond *cond)
203 struct _WsbmPCond *pCond = pCondConvert(cond);
205 pthread_cond_broadcast(&pCond->cond);
209 p_condWait(struct _WsbmCond *cond, struct _WsbmMutex *mutex)
211 struct _WsbmPCond *pCond = pCondConvert(cond);
212 struct _WsbmPMutex *pMutex = pMutexConvert(mutex);
214 pthread_cond_wait(&pCond->cond, &pMutex->mutex);
217 static struct _WsbmThreadFuncs pthreadFunc = {
218 .mutexInit = p_mutexInit,
219 .mutexFree = p_mutexFree,
220 .mutexLock = p_mutexLock,
221 .mutexUnlock = p_mutexUnlock,
222 .condInit = p_condInit,
223 .condFree = p_condFree,
224 .condWait = p_condWait,
225 .condBroadcast = p_condBroadcast
228 struct _WsbmThreadFuncs *
229 wsbmPThreadFuncs(void)
235 #warning Pthreads is not present. Compiling without.
237 struct _WsbmThreadFuncs *
238 wsbmPThreadFuncs(void)