Remove LINUX24 from src/afs
[openafs.git] / src / rx / LINUX24 / rx_kmutex.c
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 /*
11  * rx_kmutex.c - mutex and condition variable macros for kernel environment.
12  *
13  * Linux implementation.
14  */
15
16 #include <afsconfig.h>
17 #include "afs/param.h"
18
19
20 #include "rx/rx_kcommon.h"
21 #include "rx_kmutex.h"
22 #include "rx/rx_kernel.h"
23
24 void
25 afs_mutex_init(afs_kmutex_t * l)
26 {
27 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
28     mutex_init(&l->mutex);
29 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
30     init_MUTEX(&l->sem);
31 #else
32     l->sem = MUTEX;
33 #endif
34     l->owner = 0;
35 }
36
37 void
38 afs_mutex_enter(afs_kmutex_t * l)
39 {
40 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
41     mutex_lock(&l->mutex);
42 #else
43     down(&l->sem);
44 #endif
45     if (l->owner)
46         osi_Panic("mutex_enter: 0x%lx held by %d", (unsigned long)l, l->owner);
47     l->owner = current->pid;
48 }
49
50 int
51 afs_mutex_tryenter(afs_kmutex_t * l)
52 {
53 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
54     if (mutex_trylock(&l->mutex) == 0)
55 #else
56     if (down_trylock(&l->sem))
57 #endif
58         return 0;
59     l->owner = current->pid;
60     return 1;
61 }
62
63 void
64 afs_mutex_exit(afs_kmutex_t * l)
65 {
66     if (l->owner != current->pid)
67         osi_Panic("mutex_exit: 0x%lx held by %d", (unsigned long)l, l->owner);
68     l->owner = 0;
69 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
70     mutex_unlock(&l->mutex);
71 #else
72     up(&l->sem);
73 #endif
74 }
75
76 /* CV_WAIT and CV_TIMEDWAIT sleep until the specified event occurs, or, in the
77  * case of CV_TIMEDWAIT, until the specified timeout occurs.
78  * - NOTE: that on Linux, there are circumstances in which TASK_INTERRUPTIBLE
79  *   can wake up, even if all signals are blocked
80  * - TODO: handle signals correctly by passing an indication back to the
81  *   caller that the wait has been interrupted and the stack should be cleaned
82  *   up preparatory to signal delivery
83  */
84 int
85 afs_cv_wait(afs_kcondvar_t * cv, afs_kmutex_t * l, int sigok)
86 {
87     int seq, isAFSGlocked = ISAFS_GLOCK();
88     sigset_t saved_set;
89 #ifdef DECLARE_WAITQUEUE
90     DECLARE_WAITQUEUE(wait, current);
91 #else
92     struct wait_queue wait = { current, NULL };
93 #endif
94     sigemptyset(&saved_set);
95     seq = cv->seq;
96     
97     set_current_state(TASK_INTERRUPTIBLE);
98     add_wait_queue(&cv->waitq, &wait);
99
100     if (isAFSGlocked)
101         AFS_GUNLOCK();
102     MUTEX_EXIT(l);
103
104     if (!sigok) {
105         SIG_LOCK(current);
106         saved_set = current->blocked;
107         sigfillset(&current->blocked);
108         RECALC_SIGPENDING(current);
109         SIG_UNLOCK(current);
110     }
111
112     while(seq == cv->seq) {
113         schedule();
114     }
115
116     remove_wait_queue(&cv->waitq, &wait);
117     set_current_state(TASK_RUNNING);
118
119     if (!sigok) {
120         SIG_LOCK(current);
121         current->blocked = saved_set;
122         RECALC_SIGPENDING(current);
123         SIG_UNLOCK(current);
124     }
125
126     if (isAFSGlocked)
127         AFS_GLOCK();
128     MUTEX_ENTER(l);
129
130     return (sigok && signal_pending(current)) ? EINTR : 0;
131 }
132
133 void
134 afs_cv_timedwait(afs_kcondvar_t * cv, afs_kmutex_t * l, int waittime)
135 {
136     int seq, isAFSGlocked = ISAFS_GLOCK();
137     long t = waittime * HZ / 1000;
138 #ifdef DECLARE_WAITQUEUE
139     DECLARE_WAITQUEUE(wait, current);
140 #else
141     struct wait_queue wait = { current, NULL };
142 #endif
143     seq = cv->seq;
144
145     set_current_state(TASK_INTERRUPTIBLE);
146     add_wait_queue(&cv->waitq, &wait);
147
148     if (isAFSGlocked)
149         AFS_GUNLOCK();
150     MUTEX_EXIT(l);
151
152     while(seq == cv->seq) {
153         t = schedule_timeout(t);
154         if (!t)         /* timeout */
155             break;
156     }
157     
158     remove_wait_queue(&cv->waitq, &wait);
159     set_current_state(TASK_RUNNING);
160
161     if (isAFSGlocked)
162         AFS_GLOCK();
163     MUTEX_ENTER(l);
164 }