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