6ae4af7684892542ea878041b0b90e242dd209bd
[openafs.git] / src / rx / rx_pthread.h
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /* rx_pthread.h defines the lock and cv primitives required for a thread
11  * safe user mode rx. The current implemenation is only tested on Solaris.
12  */
13
14 #ifndef RX_PTHREAD_H
15 #define RX_PTHREAD_H
16
17 #ifndef RX_ENABLE_LOCKS
18 #define RX_ENABLE_LOCKS 1
19 #endif
20
21 /* This turns out to be necessary even for fine grain locking. */
22 #ifndef AFS_GLOBAL_RXLOCK_KERNEL
23 #define AFS_GLOBAL_RXLOCK_KERNEL 1
24 #endif
25
26 /* Block signals to child threads. */
27 #include <afs/pthread_nosigs.h>
28
29 #ifdef AFS_NT40_ENV
30 #include <wtypes.h>
31 #include <winbase.h>
32 #include <winsock2.h>
33 #include <pthread.h>
34
35 typedef pthread_mutex_t afs_kmutex_t;
36 typedef pthread_cond_t afs_kcondvar_t;
37 #define MUTEX_ISMINE
38 #define pthread_yield() Sleep(0)
39
40 #else /* AFS_NT40_ENV */
41
42 #include <pthread.h>
43 typedef pthread_mutex_t afs_kmutex_t;
44 typedef pthread_cond_t afs_kcondvar_t;
45
46 #if !defined(pthread_yield) && defined(AFS_SUN5_ENV)
47 #define pthread_yield() thr_yield()
48 #endif
49 #if !defined(pthread_yield) && !defined(AFS_AIX_ENV)
50 #define pthread_yield() sleep(0)
51 #endif
52 #if !defined(pthread_yield) && (_XOPEN_SOURCE + 0) >= 500
53 #define pthread_yield() sched_yield()
54 #endif
55
56
57 #ifndef MUTEX_ISMINE
58 /* Only used for debugging. */
59 #ifdef AFS_SUN5_ENV
60 /* synch.h says mutex_t and pthread_mutex_t are always the same */
61 #include <synch.h>
62 #define MUTEX_ISMINE(l) MUTEX_HELD((mutex_t *) l)
63 #else /* AFS_SUN5_ENV */
64 #define MUTEX_ISMINE(l) (1)
65 #endif /* AFS_SUN5_ENV */
66 #endif /* !MUTEX_ISMINE */
67 #endif /* AFS_NT40_ENV */
68
69 extern void osirx_AssertMine(afs_kmutex_t * lockaddr, char *msg);
70
71 #ifdef AFS_PTHREAD_ENV
72 #ifdef MUTEX_INIT
73 #undef MUTEX_INIT
74 #endif
75 #define MUTEX_INIT(a, b, c, d) osi_Assert(pthread_mutex_init(a, NULL) == 0)
76
77 #ifdef MUTEX_DESTROY
78 #undef MUTEX_DESTROY
79 #endif
80 #define MUTEX_DESTROY(l) osi_Assert(pthread_mutex_destroy(l) == 0)
81
82 #ifdef MUTEX_ENTER
83 #undef MUTEX_ENTER
84 #endif
85 #define MUTEX_ENTER(l) osi_Assert(pthread_mutex_lock(l) == 0)
86
87 #ifdef MUTEX_TRYENTER
88 #undef MUTEX_TRYENTER
89 #endif
90 #define MUTEX_TRYENTER(l) pthread_mutex_trylock(l) ? 0 : 1
91
92 #ifdef MUTEX_EXIT
93 #undef MUTEX_EXIT
94 #endif
95 #define MUTEX_EXIT(l) osi_Assert(pthread_mutex_unlock(l) == 0)
96
97 #ifdef CV_INIT
98 #undef CV_INIT
99 #endif
100 #define CV_INIT(cv, a, b, c) osi_Assert(pthread_cond_init(cv, NULL) == 0)
101
102 #ifdef CV_DESTROY
103 #undef CV_DESTROY
104 #endif
105 #define CV_DESTROY(cv) osi_Assert(pthread_cond_destroy(cv) == 0)
106
107 #ifdef CV_WAIT
108 #undef CV_WAIT
109 #endif
110 #define CV_WAIT(cv, l) osi_Assert(pthread_cond_wait(cv, l) == 0)
111
112 #ifdef CV_SIGNAL
113 #undef CV_SIGNAL
114 #endif
115 #define CV_SIGNAL(cv) osi_Assert(pthread_cond_signal(cv) == 0)
116
117 #ifdef CV_BROADCAST
118 #undef CV_BROADCAST
119 #endif
120 #define CV_BROADCAST(cv) osi_Assert(pthread_cond_broadcast(cv) == 0)
121
122 #endif /* AFS_PTHREAD_ENV */
123
124
125 #endif /* RX_PTHREAD_H */