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