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