1 #ifndef __SYS_PTHREAD__
2 #define __SYS_PTHREAD__
34 #define HAVE_STRUCT_TIMESPEC 1
41 #include <semaphore.h>
45 #ifndef CLOCK_REALTIME
46 #include <mach/clock.h>
47 #include <mach/mach.h>
51 template<
typename TYPE >
52 void get_apple_realtime( TYPE & wait )
55 clock_gettime(CLOCK_REALTIME, &wait);
59 host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
60 clock_get_time(cclock, &mts);
61 mach_port_deallocate(mach_task_self(), cclock);
62 wait.tv_sec = mts.tv_sec;
63 wait.tv_nsec = mts.tv_nsec;
82 inline void Lock() {pthread_mutex_lock(&cmut);}
84 inline void Signal() {
if (relMutex) pthread_mutex_lock(&cmut);
85 pthread_cond_signal(&cvar);
86 if (relMutex) pthread_mutex_unlock(&cmut);
89 inline void Broadcast() {
if (relMutex) pthread_mutex_lock(&cmut);
90 pthread_cond_broadcast(&cvar);
91 if (relMutex) pthread_mutex_unlock(&cmut);
94 inline void UnLock() {pthread_mutex_unlock(&cmut);}
102 ) {pthread_cond_init(&cvar, NULL);
103 pthread_mutex_init(&cmut, NULL);
104 relMutex = relm; condID = (cid ? cid :
"unk");
107 pthread_mutex_destroy(&cmut);
112 pthread_mutex_t cmut;
133 {
if (cnd) {
if (cnd != CndVar) cnd->
UnLock();
143 {
if (CndVar) CndVar->Lock();
170 {
if (pthread_mutex_trylock( &
cs ))
return 0;
176 struct timespec wait, cur, dur;
177 get_apple_realtime(wait);
178 wait.tv_sec += (wait_ms / 1000);
179 wait.tv_nsec += (wait_ms % 1000) * 1000000;
180 wait.tv_sec += (wait.tv_nsec / 1000000000);
181 wait.tv_nsec = wait.tv_nsec % 1000000000;
184 while( ( rc = pthread_mutex_trylock( &
cs ) ) == EBUSY )
186 get_apple_realtime(cur);
187 if( ( cur.tv_sec > wait.tv_sec ) ||
188 ( ( cur.tv_sec == wait.tv_sec ) && ( cur.tv_nsec >= wait.tv_nsec ) ) )
191 dur.tv_sec = wait.tv_sec - cur.tv_sec;
192 dur.tv_nsec = wait.tv_nsec - cur.tv_nsec;
193 if( dur.tv_nsec < 0 )
196 dur.tv_nsec += 1000000000;
199 if( ( dur.tv_sec != 0 ) || ( dur.tv_nsec > 1000000 ) )
202 dur.tv_nsec = 1000000;
205 nanosleep( &dur, 0 );
212 {
struct timespec wait;
213 clock_gettime(CLOCK_REALTIME, &wait);
214 wait.tv_sec += (wait_ms / 1000);
215 wait.tv_nsec += (wait_ms % 1000) * 1000000;
216 wait.tv_sec += (wait.tv_nsec / 1000000000);
217 wait.tv_nsec = wait.tv_nsec % 1000000000;
218 return !pthread_mutex_timedlock(&
cs, &wait);
222 inline void Lock() {pthread_mutex_lock(&
cs);}
267 {
if (mtx) {
if (mtx != Mutex) mtx->
UnLock();
277 {
if (mutex) mutex->Lock();
312 {pthread_cond_init(&
cvar, NULL);}
334 {
if (pthread_rwlock_tryrdlock( &
lock ))
return 0;
338 {
if (pthread_rwlock_trywrlock( &
lock ))
return 0;
345 inline void ReadLock(
int &status ) {status = pthread_rwlock_rdlock(&
lock);}
346 inline void WriteLock(
int &status ) {status = pthread_rwlock_wrlock(&
lock);}
354 #if defined(__linux__) && (defined(__GLIBC__) || defined(__UCLIBC__))
355 pthread_rwlockattr_t attr;
356 pthread_rwlockattr_setkind_np(&attr,
357 PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP);
358 pthread_rwlock_init(&
lock, &attr);
360 pthread_rwlock_init(&
lock, NULL);
369 pthread_rwlock_destroy(&
lock);
370 #if defined(__linux__) && (defined(__GLIBC__) || defined(__UCLIBC__))
371 pthread_rwlockattr_t attr;
372 pthread_rwlockattr_setkind_np(&attr,
373 PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP);
374 pthread_rwlock_init(&
lock, &attr);
376 pthread_rwlock_init(&
lock, NULL);
382 pthread_rwlock_destroy(&
lock);
383 pthread_rwlock_init(&
lock, NULL);
402 {
if (lck) {
if (lck != lock) lck->
UnLock();
413 {
if (l) {
if (rd) l->ReadLock();
437 inline void Lock() {isRW ? rwLok->WriteLock() : mutex->Lock();}
439 inline void ReadLock() {isRW ? rwLok->ReadLock() : mutex->Lock();}
441 inline void WriteLock() {isRW ? rwLok->WriteLock() : mutex->Lock();}
443 inline void UnLock() {isRW ? rwLok->UnLock() : mutex->UnLock();}
446 : rwLok(&mtx), isRW(true) {}
449 : mutex(&mtx), isRW(false) {}
467 #if defined(__APPLE__) || defined(__GNU__)
478 static void CleanUp(
void *semVar);
481 {semVal = semval; semWait = 0;}
498 {
while(sem_trywait( &h_semaphore ))
499 {
if (errno == EAGAIN)
return 0;
500 if (errno != EINTR) {
throw "sem_CondWait() failed";}
505 inline void Post() {
if (sem_post(&h_semaphore))
506 {
throw "sem_post() failed";}
509 inline void Wait() {
while (sem_wait(&h_semaphore))
511 {
throw "sem_wait() failed";}
516 {
if (sem_init(&h_semaphore, 0, semval))
517 {
throw "sem_init() failed";}
543 #define XRDSYSTHREAD_BIND 0x001
548 #define XRDSYSTHREAD_HOLD 0x002
554 static int Cancel(pthread_t tid) {
return pthread_cancel(tid);}
556 static int Detach(pthread_t tid) {
return pthread_detach(tid);}
560 return pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, 0);
563 static int Join(pthread_t tid,
void **ret) {
564 return pthread_join(tid, ret);
568 return pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, 0);
572 return pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0);
576 return pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, 0);
580 pthread_testcancel();
584 static pthread_t
ID(
void) {
return pthread_self();}
586 static int Kill(pthread_t tid) {
return pthread_cancel(tid);}
588 static unsigned long Num(
void);
590 static int Run(pthread_t *,
void *(*proc)(
void *),
void *arg,
591 int opts=0,
const char *desc = 0);
593 static int Same(pthread_t t1, pthread_t t2)
594 {
return pthread_equal(t1, t2);}
598 static void setStackSize(
size_t stsz,
bool force=
false);
600 static int Signal(pthread_t tid,
int snum)
601 {
return pthread_kill(tid, snum);}
603 static int Wait(pthread_t tid);
610 static size_t stackSize;
XrdSysCondVar2(XrdSysMutex &mtx)
void Lock(XrdSysCondVar *CndVar)
XrdSysCondVarHelper(XrdSysCondVar *CndVar=0)
XrdSysCondVarHelper(XrdSysCondVar &CndVar)
XrdSysCondVar(int relm=1, const char *cid=0)
XrdSysFusedMutex(XrdSysMutex &mtx)
XrdSysFusedMutex(XrdSysRWLock &mtx)
XrdSysMutexHelper(XrdSysMutex *mutex=0)
void Lock(XrdSysMutex *Mutex)
XrdSysMutexHelper(XrdSysMutex &mutex)
int TimedLock(int wait_ms)
XrdSysRWLockHelper(XrdSysRWLock *l=0, bool rd=1)
void Lock(XrdSysRWLock *lock, bool rd=1)
XrdSysRWLockHelper(XrdSysRWLock &l, bool rd=1)
void ReadLock(int &status)
void WriteLock(int &status)
void ReInitialize(PrefType)
XrdSysSemaphore(int semval=1, const char *=0)
static void setDebug(XrdSysError *erp)
static void CancelPoint()
static int Wait(pthread_t tid)
static int Same(pthread_t t1, pthread_t t2)
static int SetCancelAsynchronous()
static int Join(pthread_t tid, void **ret)
static int Run(pthread_t *, void *(*proc)(void *), void *arg, int opts=0, const char *desc=0)
static int Cancel(pthread_t tid)
static pthread_t ID(void)
static int Signal(pthread_t tid, int snum)
static int Kill(pthread_t tid)
static int Detach(pthread_t tid)
static void setStackSize(size_t stsz, bool force=false)
static unsigned long Num(void)
static int SetCancelOff()
static int SetCancelDeferred()