linux-global-sunlock-always-20050424
[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 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     add_wait_queue(cv, &wait);
84     set_current_state(TASK_INTERRUPTIBLE);
85
86     if (isAFSGlocked)
87         AFS_GUNLOCK();
88     MUTEX_EXIT(l);
89
90     if (!sigok) {
91         SIG_LOCK(current);
92         saved_set = current->blocked;
93         sigfillset(&current->blocked);
94         RECALC_SIGPENDING(current);
95         SIG_UNLOCK(current);
96     }
97
98     schedule();
99     remove_wait_queue(cv, &wait);
100
101     if (!sigok) {
102         SIG_LOCK(current);
103         current->blocked = saved_set;
104         RECALC_SIGPENDING(current);
105         SIG_UNLOCK(current);
106     }
107
108     if (isAFSGlocked)
109         AFS_GLOCK();
110     MUTEX_ENTER(l);
111
112     return (sigok && signal_pending(current)) ? EINTR : 0;
113 }
114
115 void
116 afs_cv_timedwait(afs_kcondvar_t * cv, afs_kmutex_t * l, int waittime)
117 {
118     int isAFSGlocked = ISAFS_GLOCK();
119     long t = waittime * HZ / 1000;
120 #ifdef DECLARE_WAITQUEUE
121     DECLARE_WAITQUEUE(wait, current);
122 #else
123     struct wait_queue wait = { current, NULL };
124 #endif
125
126     add_wait_queue(cv, &wait);
127     set_current_state(TASK_INTERRUPTIBLE);
128
129     if (isAFSGlocked)
130         AFS_GUNLOCK();
131     MUTEX_EXIT(l);
132
133     t = schedule_timeout(t);
134     remove_wait_queue(cv, &wait);
135
136     if (isAFSGlocked)
137         AFS_GLOCK();
138     MUTEX_ENTER(l);
139 }