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