f90b598bacbd84df8e8b5bec5ac243a21719fdee
[openafs.git] / src / afs / DUX / 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 static int osi_TimedSleep(char *event, afs_int32 ams, int aintok);
22
23 static char waitV;
24
25
26 void afs_osi_InitWaitHandle(struct afs_osi_WaitHandle *achandle)
27 {
28     AFS_STATCNT(osi_InitWaitHandle);
29     achandle->proc = (caddr_t) 0;
30 }
31
32 /* cancel osi_Wait */
33 void afs_osi_CancelWait(struct afs_osi_WaitHandle *achandle)
34 {
35     caddr_t proc;
36
37     AFS_STATCNT(osi_CancelWait);
38     proc = achandle->proc;
39     if (proc == 0) return;
40     achandle->proc = (caddr_t) 0;   /* so dude can figure out he was signalled */
41     afs_osi_Wakeup(&waitV);
42 }
43
44 /* afs_osi_Wait
45  * Waits for data on ahandle, or ams ms later.  ahandle may be null.
46  * Returns 0 if timeout and EINTR if signalled.
47  */
48 int afs_osi_Wait(afs_int32 ams, struct afs_osi_WaitHandle *ahandle, int aintok)
49 {
50     int code;
51     afs_int32 endTime, tid;
52
53     AFS_STATCNT(osi_Wait);
54     endTime = osi_Time() + (ams/1000);
55     if (ahandle)
56         ahandle->proc = (caddr_t) u.u_procp;
57     do {
58         AFS_ASSERT_GLOCK();
59         code = 0;
60         code = osi_TimedSleep(&waitV, ams, aintok);
61
62         if (code) break;        /* if something happened, quit now */
63         /* if we we're cancelled, quit now */
64         if (ahandle && (ahandle->proc == (caddr_t) 0)) {
65             /* we've been signalled */
66             break;
67         }
68     } while (osi_Time() < endTime);
69     return code;
70 }
71
72
73
74
75 typedef struct afs_event {
76     struct afs_event *next;     /* next in hash chain */
77     char *event;                /* lwp event: an address */
78     int refcount;               /* Is it in use? */
79     int seq;                    /* Sequence number: this is incremented
80                                    by wakeup calls; wait will not return until
81                                    it changes */
82     int cond;
83 } afs_event_t;
84
85 #define HASHSIZE 128
86 afs_event_t *afs_evhasht[HASHSIZE];/* Hash table for events */
87 #define afs_evhash(event)       (afs_uint32) ((((long)event)>>2) & (HASHSIZE-1));
88 int afs_evhashcnt = 0;
89
90 /* Get and initialize event structure corresponding to lwp event (i.e. address)
91  * */
92 static afs_event_t *afs_getevent(char *event)
93 {
94     afs_event_t *evp, *newp = 0;
95     int hashcode;
96
97     AFS_ASSERT_GLOCK();
98     hashcode = afs_evhash(event);
99     evp = afs_evhasht[hashcode];
100     while (evp) {
101         if (evp->event == event) {
102             evp->refcount++;
103             return evp;
104         }
105         if (evp->refcount == 0)
106             newp = evp;
107         evp = evp->next;
108     }
109     if (!newp) {
110         newp = (afs_event_t *) osi_AllocSmallSpace(sizeof (afs_event_t));
111         afs_evhashcnt++;
112         newp->next = afs_evhasht[hashcode];
113         afs_evhasht[hashcode] = newp;
114         newp->seq = 0;
115     }
116     newp->event = event;
117     newp->refcount = 1;
118     return newp;
119 }
120
121 /* Release the specified event */
122 #define relevent(evp) ((evp)->refcount--)
123
124
125 void afs_osi_Sleep(void *event)
126 {
127     struct afs_event *evp;
128     int seq;
129
130     evp = afs_getevent(event);
131     seq = evp->seq;
132     while (seq == evp->seq) {
133         AFS_ASSERT_GLOCK();
134         assert_wait((vm_offset_t)(&evp->cond), 0);
135         AFS_GUNLOCK();
136         thread_block();
137         AFS_GLOCK();
138     }
139     relevent(evp);
140 }
141
142 int afs_osi_SleepSig(void *event)
143 {
144     afs_osi_Sleep(event);
145     return 0;
146 }
147
148 /* osi_TimedSleep
149  * 
150  * Arguments:
151  * event - event to sleep on
152  * ams --- max sleep time in milliseconds
153  * aintok - 1 if should sleep interruptibly
154  *
155  * Returns 0 if timeout and EINTR if signalled.
156  */
157 static int osi_TimedSleep(char *event, afs_int32 ams, int aintok)
158 {
159     int code = 0;
160     struct afs_event *evp;
161     int ticks;
162
163     ticks = ( ams * afs_hz )/1000;
164
165
166     evp = afs_getevent(event);
167
168     assert_wait((vm_offset_t)(&evp->cond), aintok);
169     AFS_GUNLOCK();
170     thread_set_timeout(ticks);
171     thread_block();
172     AFS_GLOCK();
173     if (current_thread()->wait_result != THREAD_AWAKENED)
174         code = EINTR;
175     
176     relevent(evp);
177     return code;
178 }
179
180
181 int afs_osi_Wakeup(void *event)
182 {
183     int ret=1;
184     struct afs_event *evp;
185     
186     evp = afs_getevent(event);
187     if (evp->refcount > 1) {
188         evp->seq++;    
189         thread_wakeup((vm_offset_t)(&evp->cond));
190         ret=0;
191     }
192     relevent(evp);
193     return ret;
194 }