bozo: Introduce bnode_Wait()
[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
14 #include "afs/sysincludes.h"    /* Standard vendor system headers */
15 #include "afsincludes.h"        /* Afs-based standard headers */
16 #include "afs/afs_stats.h"      /* afs statistics */
17
18 static char waitV;
19
20 void
21 afs_osi_InitWaitHandle(struct afs_osi_WaitHandle *achandle)
22 {
23     AFS_STATCNT(osi_InitWaitHandle);
24     achandle->proc = (caddr_t) 0;
25 }
26
27 /* cancel osi_Wait */
28 void
29 afs_osi_CancelWait(struct afs_osi_WaitHandle *achandle)
30 {
31     caddr_t proc;
32
33     AFS_STATCNT(osi_CancelWait);
34     proc = achandle->proc;
35     if (proc == 0)
36         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
46 afs_osi_Wait(afs_int32 ams, struct afs_osi_WaitHandle *ahandle, int aintok)
47 {
48     int code;
49     afs_int32 endTime, tid;
50
51     AFS_STATCNT(osi_Wait);
52     endTime = osi_Time() + (ams / 1000);
53     if (ahandle)
54         ahandle->proc = (caddr_t) curthread;
55     do {
56         AFS_ASSERT_GLOCK();
57         code = 0;
58         code = afs_osi_TimedSleep(&waitV, ams, aintok);
59
60         if (code)
61             break;              /* if something happened, quit now */
62         /* if we we're cancelled, quit now */
63         if (ahandle && (ahandle->proc == (caddr_t) 0)) {
64             /* we've been signalled */
65             break;
66         }
67     } while (osi_Time() < endTime);
68     return code;
69 }
70
71
72
73
74 afs_event_t *afs_evhasht[AFS_EVHASHSIZE];       /* Hash table for events */
75 #define afs_evhash(event)       (afs_uint32) ((((long)event)>>2) & (AFS_EVHASHSIZE-1))
76 int afs_evhashcnt = 0;
77
78 /* Get and initialize event structure corresponding to lwp event (i.e. address)
79  * */
80 static afs_event_t *
81 afs_getevent(char *event)
82 {
83     afs_event_t *evp, *newp = 0;
84     int hashcode;
85
86     AFS_ASSERT_GLOCK();
87     hashcode = afs_evhash(event);
88     evp = afs_evhasht[hashcode];
89     while (evp) {
90         if (evp->event == event) {
91             evp->refcount++;
92             return evp;
93         }
94         if (evp->refcount == 0)
95             newp = evp;
96         evp = evp->next;
97     }
98     if (!newp) {
99         newp = osi_AllocSmallSpace(sizeof(afs_event_t));
100         afs_evhashcnt++;
101         newp->next = afs_evhasht[hashcode];
102         afs_evhasht[hashcode] = newp;
103         cv_init(&newp->cond, "event cond var", CV_DEFAULT, NULL);
104         newp->seq = 0;
105     }
106     newp->event = event;
107     newp->refcount = 1;
108     return newp;
109 }
110
111 /* Release the specified event */
112 #define relevent(evp) ((evp)->refcount--)
113
114
115 void
116 afs_osi_Sleep(void *event)
117 {
118     struct afs_event *evp;
119     int seq;
120
121     evp = afs_getevent(event);
122     seq = evp->seq;
123     while (seq == evp->seq) {
124         AFS_ASSERT_GLOCK();
125         cv_wait(&evp->cond, &afs_global_lock);
126     }
127     relevent(evp);
128 }
129
130 int
131 afs_osi_SleepSig(void *event)
132 {
133     struct afs_event *evp;
134     int seq, code = 0;
135
136     evp = afs_getevent(event);
137     seq = evp->seq;
138     while (seq == evp->seq) {
139         AFS_ASSERT_GLOCK();
140         if (cv_wait_sig(&evp->cond, &afs_global_lock) == 0) {
141             code = EINTR;
142             break;
143         }
144     }
145     relevent(evp);
146     return code;
147 }
148
149 /* afs_osi_TimedSleep
150  * 
151  * Arguments:
152  * event - event to sleep on
153  * ams --- max sleep time in milliseconds
154  * aintok - 1 if should sleep interruptibly
155  *
156  * Returns 0 if timeout and EINTR if signalled.
157  */
158 int
159 afs_osi_TimedSleep(void *event, afs_int32 ams, int aintok)
160 {
161     int code = 0;
162     struct afs_event *evp;
163     clock_t ticks;
164
165     ticks = (ams * afs_hz) / 1000;
166 #if defined(AFS_SUN510_ENV)
167     ticks = ticks + ddi_get_lbolt();
168 #else
169     ticks = ticks + lbolt;
170 #endif
171
172     evp = afs_getevent(event);
173
174     AFS_ASSERT_GLOCK();
175     if (aintok) {
176         if (cv_timedwait_sig(&evp->cond, &afs_global_lock, ticks) == 0)
177             code = EINTR;
178     } else {
179         cv_timedwait(&evp->cond, &afs_global_lock, ticks);
180     }
181
182     relevent(evp);
183     return code;
184 }
185
186
187 int
188 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 }