f1b75d89a67d16bb575913dff76dafaadbecacfe
[openafs.git] / src / afs / LINUX / osi_sleep.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 #include "../afs/param.h"       /* Should be always first */
11 #include "../afs/sysincludes.h" /* Standard vendor system headers */
12 #include "../afs/afsincludes.h" /* Afs-based standard headers */
13 #include "../afs/afs_stats.h"   /* afs statistics */
14
15
16
17 #if defined(AFS_GLOBAL_SUNLOCK)
18 static int osi_TimedSleep(char *event, afs_int32 ams, int aintok);
19 void afs_osi_Wakeup(char *event);
20 void afs_osi_Sleep(char *event);
21 #endif
22
23 static char waitV;
24
25 #if ! defined(AFS_GLOBAL_SUNLOCK)
26
27 /* call procedure aproc with arock as an argument, in ams milliseconds */
28 static struct timer_list *afs_osi_CallProc(void *aproc, void *arock, int ams)
29 {
30     struct timer_list *timer = NULL;
31     
32     timer = (struct timer_list*)osi_Alloc(sizeof(struct timer_list));
33     if (timer) {
34         init_timer(timer);
35         timer->expires = (ams*afs_hz)/1000 + 1;
36         timer->data = (unsigned long)arock;
37         timer->function = aproc;
38         add_timer(timer);
39     }
40     return timer;
41 }
42
43 /* cancel a timeout, whether or not it has already occurred */
44 static int afs_osi_CancelProc(struct timer_list *timer)
45 {
46     if (timer) {
47         del_timer(timer);
48         osi_Free(timer, sizeof(struct timer_list));
49     }
50     return 0;
51 }
52
53 static AfsWaitHack()
54 {
55     AFS_STATCNT(WaitHack);
56     wakeup(&waitV);
57 }
58
59 #endif
60
61 void afs_osi_InitWaitHandle(struct afs_osi_WaitHandle *achandle)
62 {
63     AFS_STATCNT(osi_InitWaitHandle);
64     achandle->proc = (caddr_t) 0;
65 }
66
67 /* cancel osi_Wait */
68 void afs_osi_CancelWait(struct afs_osi_WaitHandle *achandle)
69 {
70     caddr_t proc;
71
72     AFS_STATCNT(osi_CancelWait);
73     proc = achandle->proc;
74     if (proc == 0) return;
75     achandle->proc = (caddr_t) 0;   /* so dude can figure out he was signalled */
76     afs_osi_Wakeup(&waitV);
77 }
78
79 /* afs_osi_Wait
80  * Waits for data on ahandle, or ams ms later.  ahandle may be null.
81  * Returns 0 if timeout and EINTR if signalled.
82  */
83 int afs_osi_Wait(afs_int32 ams, struct afs_osi_WaitHandle *ahandle, int aintok)
84 {
85     int code;
86     afs_int32 endTime, tid;
87     struct timer_list *timer = NULL;
88
89     AFS_STATCNT(osi_Wait);
90     endTime = osi_Time() + (ams/1000);
91     if (ahandle)
92         ahandle->proc = (caddr_t) current;
93
94     do {
95         AFS_ASSERT_GLOCK();
96         code = 0;
97 #if     defined(AFS_GLOBAL_SUNLOCK)
98         code = osi_TimedSleep(&waitV, ams, 1);
99         if (code) {
100                 if (aintok) break;
101                 flush_signals(current);
102                 code = 0;
103         }
104 #else
105         timer = afs_osi_CallProc(AfsWaitHack, (char *) current, ams);
106         afs_osi_Sleep(&waitV);
107         afs_osi_CancelProc(timer);
108 #endif /* AFS_GLOBAL_SUNLOCK */
109         if (ahandle && (ahandle->proc == (caddr_t) 0)) {
110             /* we've been signalled */
111             break;
112         }
113     } while (osi_Time() < endTime);
114     return code;
115 }
116
117
118
119
120 typedef struct afs_event {
121     struct afs_event *next;     /* next in hash chain */
122     char *event;                /* lwp event: an address */
123     int refcount;               /* Is it in use? */
124     int seq;                    /* Sequence number: this is incremented
125                                    by wakeup calls; wait will not return until
126                                    it changes */
127 #if defined(AFS_LINUX24_ENV)
128     wait_queue_head_t cond;
129 #else
130     struct wait_queue *cond;
131 #endif
132 } afs_event_t;
133
134 #define HASHSIZE 128
135 afs_event_t *afs_evhasht[HASHSIZE];/* Hash table for events */
136 #define afs_evhash(event)       (afs_uint32) ((((long)event)>>2) & (HASHSIZE-1));
137 int afs_evhashcnt = 0;
138
139 /* Get and initialize event structure corresponding to lwp event (i.e. address)
140  * */
141 static afs_event_t *afs_getevent(char *event)
142 {
143     afs_event_t *evp, *newp = 0;
144     int hashcode;
145
146     AFS_ASSERT_GLOCK();
147     hashcode = afs_evhash(event);
148     evp = afs_evhasht[hashcode];
149     while (evp) {
150         if (evp->event == event) {
151             evp->refcount++;
152             return evp;
153         }
154         if (evp->refcount == 0)
155             newp = evp;
156         evp = evp->next;
157     }
158     if (!newp) {
159         newp = (afs_event_t *) osi_AllocSmallSpace(sizeof (afs_event_t));
160         afs_evhashcnt++;
161         newp->next = afs_evhasht[hashcode];
162         afs_evhasht[hashcode] = newp;
163 #if defined(AFS_LINUX24_ENV)
164         init_waitqueue_head(&newp->cond);
165 #else
166         init_waitqueue(&newp->cond);
167 #endif
168         newp->seq = 0;
169     }
170     newp->event = event;
171     newp->refcount = 1;
172     return newp;
173 }
174
175 /* Release the specified event */
176 #define relevent(evp) ((evp)->refcount--)
177
178
179 void afs_osi_Sleep(char *event)
180 {
181     struct afs_event *evp;
182     int seq;
183
184     evp = afs_getevent(event);
185     seq = evp->seq;
186     while (seq == evp->seq) {
187         AFS_ASSERT_GLOCK();
188         AFS_GUNLOCK();
189         interruptible_sleep_on(&evp->cond);
190         AFS_GLOCK();
191     }
192     relevent(evp);
193 }
194
195 /* osi_TimedSleep
196  * 
197  * Arguments:
198  * event - event to sleep on
199  * ams --- max sleep time in milliseconds
200  * aintok - 1 if should sleep interruptibly
201  *
202  * Returns 0 if timeout and EINTR if signalled.
203  *
204  * While the Linux kernel still has a global lock, we can use the standard
205  * sleep calls and drop our locks early. The kernel lock will protect us
206  * until we get to sleep.
207  */
208 static int osi_TimedSleep(char *event, afs_int32 ams, int aintok)
209 {
210     long t = ams * HZ / 1000;
211     struct afs_event *evp;
212
213     evp = afs_getevent(event);
214
215     AFS_GUNLOCK();
216     if (aintok)
217         t = interruptible_sleep_on_timeout(&evp->cond, t);
218     else
219         t = sleep_on_timeout(&evp->cond, t);
220     AFS_GLOCK();
221
222     return t ? EINTR : 0;
223 }
224
225
226 void afs_osi_Wakeup(char *event)
227 {
228     struct afs_event *evp;
229
230     evp = afs_getevent(event);
231     if (evp->refcount > 1) {
232         evp->seq++;    
233         wake_up(&evp->cond);
234     }
235     relevent(evp);
236 }