source: trunk/src/sh_pthread.c@ 139

Last change on this file since 139 was 139, checked in by rainer, 17 years ago

Detect availability of recursive mutexes on Linux.

File size: 1.5 KB
Line 
1#include "config_xor.h"
2
3#include "sh_pthread.h"
4
5#ifdef HAVE_PTHREAD
6SH_MUTEX_INIT(mutex_skey, PTHREAD_MUTEX_INITIALIZER);
7SH_MUTEX_INIT(mutex_resolv, PTHREAD_MUTEX_INITIALIZER);
8SH_MUTEX_INIT(mutex_pwent, PTHREAD_MUTEX_INITIALIZER);
9SH_MUTEX_INIT(mutex_readdir,PTHREAD_MUTEX_INITIALIZER);
10
11void sh_pthread_mutex_unlock (void *arg)
12{
13 (void) pthread_mutex_unlock ((pthread_mutex_t *)arg);
14 return;
15}
16
17#if !defined(HAVE_PTHREAD_MUTEX_RECURSIVE)
18/**
19 * libxml2 threads.c: set of generic threading related routines
20 *
21 * Gary Pennington <Gary.Pennington@uk.sun.com>
22 * daniel@veillard.com
23 */
24
25void sh_InitRMutex(struct sh_RMutex * tok)
26{
27 pthread_mutex_init(&tok->lock, NULL);
28 tok->held = 0;
29 tok->waiters = 0;
30 pthread_cond_init(&tok->cv, NULL);
31
32 return;
33}
34
35void sh_RMutexLock(struct sh_RMutex * tok)
36{
37 if (tok == NULL)
38 return;
39
40 pthread_mutex_lock(&tok->lock);
41 if (tok->held) {
42 if (pthread_equal(tok->tid, pthread_self())) {
43 tok->held++;
44 pthread_mutex_unlock(&tok->lock);
45 return;
46 } else {
47 tok->waiters++;
48 while (tok->held)
49 pthread_cond_wait(&tok->cv, &tok->lock);
50 tok->waiters--;
51 }
52 }
53 tok->tid = pthread_self();
54 tok->held = 1;
55 pthread_mutex_unlock(&tok->lock);
56}
57
58void sh_RMutexUnlock(void * arg)
59{
60 struct sh_RMutex * tok = (struct sh_RMutex *) arg;
61
62 if (tok == NULL)
63 return;
64
65 pthread_mutex_lock(&tok->lock);
66 tok->held--;
67 if (tok->held == 0) {
68 if (tok->waiters)
69 pthread_cond_signal(&tok->cv);
70 tok->tid = 0;
71 }
72 pthread_mutex_unlock(&tok->lock);
73}
74#endif
75
76
77#endif
Note: See TracBrowser for help on using the repository browser.