1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
/**************************************************************************
*
* Copyright 2008 Tungsten Graphics, Inc., Cedar Park, TX., USA
* All Rights Reserved.
* Copyright 2009 VMware, Inc., Palo Alto, CA., USA
* All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sub license, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice (including the
* next paragraph) shall be included in all copies or substantial portions
* of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
* THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
* USE OR OTHER DEALINGS IN THE SOFTWARE.
*
**************************************************************************/
/*
* Authors:
* Thomas Hellström <thomas-at-tungstengraphics-dot-com>
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <stddef.h>
#include <errno.h>
#include "wsbm_driver.h"
struct _WsbmThreadFuncs *wsbmCurThreadFunc = NULL;
struct _WsbmVNodeFuncs *wsbmCurVNodeFunc = NULL;
/*
* Single-threaded implementation.
*/
static int
n_mutexInit(struct _WsbmMutex *mutex, struct _WsbmThreadFuncs *func)
{
mutex->func = func;
return 0;
}
static int
n_condInit(struct _WsbmCond *cond, struct _WsbmThreadFuncs *func)
{
cond->func = func;
return 0;
}
static void
n_mutexNone(struct _WsbmMutex *mutex)
{
;
}
static void
n_condNone(struct _WsbmCond *cond)
{
;
}
static void
n_condWait(struct _WsbmCond *cond, struct _WsbmMutex *mutex)
{
;
}
static struct _WsbmThreadFuncs nullFunc = {
.mutexInit = n_mutexInit,
.mutexFree = n_mutexNone,
.mutexLock = n_mutexNone,
.mutexUnlock = n_mutexNone,
.condInit = n_condInit,
.condFree = n_condNone,
.condWait = n_condWait,
.condBroadcast = n_condNone
};
struct _WsbmThreadFuncs *
wsbmNullThreadFuncs(void)
{
return &nullFunc;
}
#if (HAVE_PTHREADS == 1)
#include "pthread.h"
/*
* pthreads implementation:
*/
struct _WsbmPMutex {
struct _WsbmThreadFuncs *func;
pthread_mutex_t mutex;
};
struct _WsbmPCond {
struct _WsbmThreadFuncs *func;
pthread_cond_t cond;
};
static inline struct _WsbmPMutex *
pMutexConvert(struct _WsbmMutex *m)
{
union _PMutexConverter {
struct _WsbmMutex wm;
struct _WsbmPMutex pm;
} *um = containerOf(m, union _PMutexConverter, wm);
return &um->pm;
}
static inline struct _WsbmPCond *
pCondConvert(struct _WsbmCond *c)
{
union _PCondConverter {
struct _WsbmCond wc;
struct _WsbmPCond pc;
} *uc = containerOf(c, union _PCondConverter, wc);
return &uc->pc;
}
static int
p_mutexInit(struct _WsbmMutex *mutex, struct _WsbmThreadFuncs *func)
{
struct _WsbmPMutex *pMutex = pMutexConvert(mutex);
if (sizeof(struct _WsbmMutex) < sizeof(struct _WsbmPMutex))
return -EINVAL;
pMutex->func = func;
pthread_mutex_init(&pMutex->mutex, NULL);
return 0;
}
static void
p_mutexFree(struct _WsbmMutex *mutex)
{
struct _WsbmPMutex *pMutex = pMutexConvert(mutex);
pthread_mutex_destroy(&pMutex->mutex);
}
static void
p_mutexLock(struct _WsbmMutex *mutex)
{
struct _WsbmPMutex *pMutex = pMutexConvert(mutex);
pthread_mutex_lock(&pMutex->mutex);
}
static void
p_mutexUnlock(struct _WsbmMutex *mutex)
{
struct _WsbmPMutex *pMutex = pMutexConvert(mutex);
pthread_mutex_unlock(&pMutex->mutex);
}
static int
p_condInit(struct _WsbmCond *cond, struct _WsbmThreadFuncs *func)
{
struct _WsbmPCond *pCond = pCondConvert(cond);
if (sizeof(struct _WsbmCond) < sizeof(struct _WsbmPCond))
return -EINVAL;
pCond->func = func;
pthread_cond_init(&pCond->cond, NULL);
return 0;
}
static void
p_condFree(struct _WsbmCond *cond)
{
struct _WsbmPCond *pCond = pCondConvert(cond);
pthread_cond_destroy(&pCond->cond);
}
static void
p_condBroadcast(struct _WsbmCond *cond)
{
struct _WsbmPCond *pCond = pCondConvert(cond);
pthread_cond_broadcast(&pCond->cond);
}
static void
p_condWait(struct _WsbmCond *cond, struct _WsbmMutex *mutex)
{
struct _WsbmPCond *pCond = pCondConvert(cond);
struct _WsbmPMutex *pMutex = pMutexConvert(mutex);
pthread_cond_wait(&pCond->cond, &pMutex->mutex);
}
static struct _WsbmThreadFuncs pthreadFunc = {
.mutexInit = p_mutexInit,
.mutexFree = p_mutexFree,
.mutexLock = p_mutexLock,
.mutexUnlock = p_mutexUnlock,
.condInit = p_condInit,
.condFree = p_condFree,
.condWait = p_condWait,
.condBroadcast = p_condBroadcast
};
struct _WsbmThreadFuncs *
wsbmPThreadFuncs(void)
{
return &pthreadFunc;
}
#else
#warning Pthreads is not present. Compiling without.
struct _WsbmThreadFuncs *
wsbmPThreadFuncs(void)
{
return &pthreadFunc;
}
#endif
|