linux-ext3-adds-items-to-struct-inode-and-so-do-we-20010807
[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 #if defined(AFS_LINUX24_ENV)
133     wait_queue_head_t cond;
134 #else
135     struct wait_queue *cond;
136 #endif
137 } afs_event_t;
138
139 #define HASHSIZE 128
140 afs_event_t *afs_evhasht[HASHSIZE];/* Hash table for events */
141 #define afs_evhash(event)       (afs_uint32) ((((long)event)>>2) & (HASHSIZE-1));
142 int afs_evhashcnt = 0;
143
144 /* Get and initialize event structure corresponding to lwp event (i.e. address)
145  * */
146 static afs_event_t *afs_getevent(char *event)
147 {
148     afs_event_t *evp, *newp = 0;
149     int hashcode;
150
151     AFS_ASSERT_GLOCK();
152     hashcode = afs_evhash(event);
153     evp = afs_evhasht[hashcode];
154     while (evp) {
155         if (evp->event == event) {
156             evp->refcount++;
157             return evp;
158         }
159         if (evp->refcount == 0)
160             newp = evp;
161         evp = evp->next;
162     }
163     if (!newp) {
164         newp = (afs_event_t *) osi_AllocSmallSpace(sizeof (afs_event_t));
165         afs_evhashcnt++;
166         newp->next = afs_evhasht[hashcode];
167         afs_evhasht[hashcode] = newp;
168 #if defined(AFS_LINUX24_ENV)
169         init_waitqueue_head(&newp->cond);
170 #else
171         init_waitqueue(&newp->cond);
172 #endif
173         newp->seq = 0;
174     }
175     newp->event = event;
176     newp->refcount = 1;
177     return newp;
178 }
179
180 /* Release the specified event */
181 #define relevent(evp) ((evp)->refcount--)
182
183
184 void afs_osi_Sleep(char *event)
185 {
186     struct afs_event *evp;
187     int seq;
188
189     evp = afs_getevent(event);
190     seq = evp->seq;
191     while (seq == evp->seq) {
192         AFS_ASSERT_GLOCK();
193         AFS_GUNLOCK();
194         interruptible_sleep_on(&evp->cond);
195         AFS_GLOCK();
196     }
197     relevent(evp);
198 }
199
200 /* osi_TimedSleep
201  * 
202  * Arguments:
203  * event - event to sleep on
204  * ams --- max sleep time in milliseconds
205  * aintok - 1 if should sleep interruptibly
206  *
207  * Returns 0 if timeout and EINTR if signalled.
208  *
209  * While the Linux kernel still has a global lock, we can use the standard
210  * sleep calls and drop our locks early. The kernel lock will protect us
211  * until we get to sleep.
212  */
213 static int osi_TimedSleep(char *event, afs_int32 ams, int aintok)
214 {
215     long t = ams * HZ / 1000;
216     struct afs_event *evp;
217
218     evp = afs_getevent(event);
219
220     AFS_GUNLOCK();
221     if (aintok)
222         t = interruptible_sleep_on_timeout(&evp->cond, t);
223     else
224         t = sleep_on_timeout(&evp->cond, t);
225     AFS_GLOCK();
226
227     return t ? EINTR : 0;
228 }
229
230
231 void afs_osi_Wakeup(char *event)
232 {
233     struct afs_event *evp;
234
235     evp = afs_getevent(event);
236     if (evp->refcount > 1) {
237         evp->seq++;    
238         wake_up(&evp->cond);
239     }
240     relevent(evp);
241 }