-
Notifications
You must be signed in to change notification settings - Fork 149
/
XrdSysPthread.cc
393 lines (328 loc) · 13.3 KB
/
XrdSysPthread.cc
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/******************************************************************************/
/* */
/* X r d S y s P t h r e a d . c c */
/* */
/* (c) 2004 by the Board of Trustees of the Leland Stanford, Jr., University */
/* Produced by Andrew Hanushevsky for Stanford University under contract */
/* DE-AC02-76-SFO0515 with the Department of Energy */
/* */
/* This file is part of the XRootD software suite. */
/* */
/* XRootD is free software: you can redistribute it and/or modify it under */
/* the terms of the GNU Lesser General Public License as published by the */
/* Free Software Foundation, either version 3 of the License, or (at your */
/* option) any later version. */
/* */
/* XRootD is distributed in the hope that it will be useful, but WITHOUT */
/* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
/* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
/* License for more details. */
/* */
/* You should have received a copy of the GNU Lesser General Public License */
/* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
/* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
/* */
/* The copyright holder's institutional names and contributor's names may not */
/* be used to endorse or promote products derived from this software without */
/* specific prior written permission of the institution or contributor. */
/******************************************************************************/
#include <cerrno>
#include <pthread.h>
#ifndef WIN32
#include <unistd.h>
#include <sys/time.h>
#else
#undef ETIMEDOUT // Make sure that the definition from Winsock2.h is used ...
#include <Winsock2.h>
#include <ctime>
#include "XrdSys/XrdWin32.hh"
#endif
#include <sys/types.h>
#if defined(__linux__)
#include <sys/syscall.h>
#endif
#include "XrdSys/XrdSysPthread.hh"
/******************************************************************************/
/* L o c a l S t r u c t s */
/******************************************************************************/
struct XrdSysThreadArgs
{
XrdSysError *eDest;
const char *tDesc;
void *(*proc)(void *);
void *arg;
XrdSysThreadArgs(XrdSysError *ed, const char *td,
void *(*p)(void *), void *a)
: eDest(ed), tDesc(td), proc(p), arg(a) {}
~XrdSysThreadArgs() {}
};
/******************************************************************************/
/* G l o b a l D a t a */
/******************************************************************************/
XrdSysError *XrdSysThread::eDest = 0;
size_t XrdSysThread::stackSize = 0;
/******************************************************************************/
/* T h r e a d I n t e r f a c e P r o g r a m s */
/******************************************************************************/
extern "C"
{
void *XrdSysThread_Xeq(void *myargs)
{
XrdSysThreadArgs *ap = (XrdSysThreadArgs *)myargs;
void *retc;
if (ap->eDest && ap->tDesc)
ap->eDest->Emsg("Xeq", ap->tDesc, "thread started");
retc = ap->proc(ap->arg);
delete ap;
return retc;
}
}
/******************************************************************************/
/* X r d S y s C o n d V a r */
/******************************************************************************/
/******************************************************************************/
/* W a i t */
/******************************************************************************/
int XrdSysCondVar::Wait()
{
int retc;
// Wait for the condition
//
if (relMutex) Lock();
retc = pthread_cond_wait(&cvar, &cmut);
if (relMutex) UnLock();
return retc;
}
/******************************************************************************/
int XrdSysCondVar::Wait(int sec) {return WaitMS(sec*1000);}
/******************************************************************************/
/* W a i t M S */
/******************************************************************************/
int XrdSysCondVar::WaitMS(int msec)
{
int sec, retc, usec;
struct timeval tnow;
struct timespec tval;
// Adjust millseconds
//
if (msec < 1000) sec = 0;
else {sec = msec / 1000; msec = msec % 1000;}
usec = msec * 1000;
// Get the mutex before getting the time
//
if (relMutex) Lock();
// Get current time of day
//
gettimeofday(&tnow, 0);
// Add the second and microseconds
//
tval.tv_sec = tnow.tv_sec + sec;
tval.tv_nsec = tnow.tv_usec + usec;
if (tval.tv_nsec >= 1000000)
{tval.tv_sec += tval.tv_nsec / 1000000;
tval.tv_nsec = tval.tv_nsec % 1000000;
}
tval.tv_nsec *= 1000;
// Now wait for the condition or timeout
//
do {retc = pthread_cond_timedwait(&cvar, &cmut, &tval);}
while (retc && (retc == EINTR));
if (relMutex) UnLock();
// Determine how to return
//
if (retc && retc != ETIMEDOUT) {throw "cond_timedwait() failed";}
return retc == ETIMEDOUT;
}
/******************************************************************************/
/* X r d S y s C o n d V a r 2 */
/******************************************************************************/
/******************************************************************************/
/* W a i t M S */
/******************************************************************************/
bool XrdSysCondVar2::WaitMS(int msec)
{
int sec, retc, usec;
struct timeval tnow;
struct timespec tval;
// Adjust millseconds
//
if (msec < 1000) sec = 0;
else {sec = msec / 1000; msec = msec % 1000;}
usec = msec * 1000;
// Get current time of day
//
gettimeofday(&tnow, 0);
// Add the second and microseconds
//
tval.tv_sec = tnow.tv_sec + sec;
tval.tv_nsec = tnow.tv_usec + usec;
if (tval.tv_nsec >= 1000000)
{tval.tv_sec += tval.tv_nsec / 1000000;
tval.tv_nsec = tval.tv_nsec % 1000000;
}
tval.tv_nsec *= 1000;
// Now wait for the condition or timeout
//
do {retc = pthread_cond_timedwait(&cvar, mtxP, &tval);}
while (retc && (retc == EINTR));
// Determine how to return
//
if (retc && retc != ETIMEDOUT) {throw "cond_timedwait() failed";}
return retc == ETIMEDOUT;
}
/******************************************************************************/
/* X r d S y s S e m a p h o r e */
/******************************************************************************/
/******************************************************************************/
/* C o n d W a i t */
/******************************************************************************/
#if defined(__APPLE__) || defined(__GNU__)
int XrdSysSemaphore::CondWait()
{
int rc;
// Get the semaphore only we can get it without waiting
//
semVar.Lock();
if ((rc = (semVal > 0) && !semWait)) semVal--;
semVar.UnLock();
return rc;
}
/******************************************************************************/
/* P o s t */
/******************************************************************************/
void XrdSysSemaphore::Post()
{
// Add one to the semaphore counter. If we the value is > 0 and there is a
// thread waiting for the semaphore, signal it to get the semaphore.
//
semVar.Lock();
semVal++;
if (semVal && semWait) semVar.Signal();
semVar.UnLock();
}
/******************************************************************************/
/* W a i t */
/******************************************************************************/
void XrdSysSemaphore::Wait()
{
// Wait until the semaphore value is positive. This will not be starvation
// free if the OS implements an unfair mutex.
// Adding a cleanup handler to the stack here enables threads using this OSX
// semaphore to be canceled (which is rare). A scoped lock won't work here
// because OSX is broken and doesn't call destructors properly.
//
semVar.Lock();
pthread_cleanup_push(&XrdSysSemaphore::CleanUp, (void *) &semVar);
if (semVal < 1 || semWait)
while(semVal < 1)
{semWait++;
semVar.Wait();
semWait--;
}
// Decrement the semaphore value, unlock the underlying cond var and return
//
semVal--;
pthread_cleanup_pop(1);
}
/******************************************************************************/
/* C l e a n U p */
/******************************************************************************/
void XrdSysSemaphore::CleanUp(void *semVar)
{
XrdSysCondVar *sv = (XrdSysCondVar *) semVar;
sv->UnLock();
}
#endif
/******************************************************************************/
/* T h r e a d M e t h o d s */
/******************************************************************************/
/******************************************************************************/
/* N u m */
/******************************************************************************/
unsigned long XrdSysThread::Num()
{
#if defined(__linux__)
return static_cast<unsigned long>(syscall(SYS_gettid));
#elif defined(__solaris__)
return static_cast<unsigned long>(pthread_self());
#elif defined(__APPLE__)
return static_cast<unsigned long>(pthread_mach_thread_np(pthread_self()));
#else
return static_cast<unsigned long>(getpid());
#endif
}
/******************************************************************************/
/* R u n */
/******************************************************************************/
int XrdSysThread::Run(pthread_t *tid, void *(*proc)(void *), void *arg,
int opts, const char *tDesc)
{
pthread_attr_t tattr;
XrdSysThreadArgs *myargs;
myargs = new XrdSysThreadArgs(eDest, tDesc, proc, arg);
pthread_attr_init(&tattr);
if ( opts & XRDSYSTHREAD_BIND)
pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM);
if (!(opts & XRDSYSTHREAD_HOLD))
pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
if (stackSize)
pthread_attr_setstacksize(&tattr, stackSize);
return pthread_create(tid, &tattr, XrdSysThread_Xeq,
static_cast<void *>(myargs));
}
/******************************************************************************/
/* s e t S t a c k S i z e */
/******************************************************************************/
void XrdSysThread::setStackSize(size_t stksz, bool force)
{
// If not being forced, then we set the stacksize only if the requested
// size is greater than the default size.
//
if (!force)
{pthread_attr_t tattr;
size_t dflt_stk_sz;
pthread_attr_init(&tattr);
if (pthread_attr_getstacksize(&tattr, &dflt_stk_sz) || stksz <= dflt_stk_sz)
{stackSize = 0;
return;
}
}
// Record the stack size for future use
//
stackSize = stksz;
}
/******************************************************************************/
/* W a i t */
/******************************************************************************/
int XrdSysThread::Wait(pthread_t tid)
{
int retc, *tstat;
if ((retc = pthread_join(tid, reinterpret_cast<void **>(&tstat)))) return retc;
return *tstat;
}
/******************************************************************************/
/* X r d S y s R e c M u t e x */
/******************************************************************************/
XrdSysRecMutex::XrdSysRecMutex()
{
InitRecMutex();
}
int XrdSysRecMutex::InitRecMutex()
{
int rc;
pthread_mutexattr_t attr;
rc = pthread_mutexattr_init( &attr );
if( !rc )
{
pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_RECURSIVE );
pthread_mutex_destroy( &cs );
rc = pthread_mutex_init( &cs, &attr );
}
pthread_mutexattr_destroy(&attr);
return rc;
}
int XrdSysRecMutex::ReInitRecMutex()
{
pthread_mutex_destroy( &cs );
return InitRecMutex();
}