rx-tq-waiter-rework-20050914
[openafs.git] / src / rx / LINUX / 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 RCSID
20     ("$Header$");
21
22 #include "rx/rx_kcommon.h"
23 #include "rx_kmutex.h"
24 #include "rx/rx_kernel.h"
25
26 void
27 afs_mutex_init(afs_kmutex_t * l)
28 {
29 #if defined(AFS_LINUX24_ENV)
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     down(&l->sem);
41     if (l->owner)
42         osi_Panic("mutex_enter: 0x%x held by %d", l, l->owner);
43     l->owner = current->pid;
44 }
45
46 int
47 afs_mutex_tryenter(afs_kmutex_t * l)
48 {
49     if (down_trylock(&l->sem))
50         return 0;
51     l->owner = current->pid;
52     return 1;
53 }
54
55 void
56 afs_mutex_exit(afs_kmutex_t * l)
57 {
58     if (l->owner != current->pid)
59         osi_Panic("mutex_exit: 0x%x held by %d", l, l->owner);
60     l->owner = 0;
61     up(&l->sem);
62 }
63
64 /* CV_WAIT and CV_TIMEDWAIT sleep until the specified event occurs, or, in the
65  * case of CV_TIMEDWAIT, until the specified timeout occurs.
66  * - NOTE: that on Linux, there are circumstances in which TASK_INTERRUPTIBLE
67  *   can wake up, even if all signals are blocked
68  * - TODO: handle signals correctly by passing an indication back to the
69  *   caller that the wait has been interrupted and the stack should be cleaned
70  *   up preparatory to signal delivery
71  */
72 int
73 afs_cv_wait(afs_kcondvar_t * cv, afs_kmutex_t * l, int sigok)
74 {
75     int seq, isAFSGlocked = ISAFS_GLOCK();
76     sigset_t saved_set;
77 #ifdef DECLARE_WAITQUEUE
78     DECLARE_WAITQUEUE(wait, current);
79 #else
80     struct wait_queue wait = { current, NULL };
81 #endif
82
83     seq = cv->seq;
84     
85     set_current_state(TASK_INTERRUPTIBLE);
86     add_wait_queue(&cv->waitq, &wait);
87
88     if (isAFSGlocked)
89         AFS_GUNLOCK();
90     MUTEX_EXIT(l);
91
92     if (!sigok) {
93         SIG_LOCK(current);
94         saved_set = current->blocked;
95         sigfillset(&current->blocked);
96         RECALC_SIGPENDING(current);
97         SIG_UNLOCK(current);
98     }
99
100     while(seq == cv->seq) {
101         schedule();
102         /* should we refrigerate? */
103     }
104
105     remove_wait_queue(&cv->waitq, &wait);
106     set_current_state(TASK_RUNNING);
107
108     if (!sigok) {
109         SIG_LOCK(current);
110         current->blocked = saved_set;
111         RECALC_SIGPENDING(current);
112         SIG_UNLOCK(current);
113     }
114
115     if (isAFSGlocked)
116         AFS_GLOCK();
117     MUTEX_ENTER(l);
118
119     return (sigok && signal_pending(current)) ? EINTR : 0;
120 }
121
122 void
123 afs_cv_timedwait(afs_kcondvar_t * cv, afs_kmutex_t * l, int waittime)
124 {
125     int seq, isAFSGlocked = ISAFS_GLOCK();
126     long t = waittime * HZ / 1000;
127 #ifdef DECLARE_WAITQUEUE
128     DECLARE_WAITQUEUE(wait, current);
129 #else
130     struct wait_queue wait = { current, NULL };
131 #endif
132     seq = cv->seq;
133
134     set_current_state(TASK_INTERRUPTIBLE);
135     add_wait_queue(&cv->waitq, &wait);
136
137     if (isAFSGlocked)
138         AFS_GUNLOCK();
139     MUTEX_EXIT(l);
140
141     while(seq == cv->seq) {
142         t = schedule_timeout(t);
143         if (!t)         /* timeout */
144             break;
145     }
146     
147     remove_wait_queue(&cv->waitq, &wait);
148     set_current_state(TASK_RUNNING);
149
150     if (isAFSGlocked)
151         AFS_GLOCK();
152     MUTEX_ENTER(l);
153 }