Move contents of afs_osi_gcpags to per-OS files
[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 typedef struct afs_event {
75     struct afs_event *next;     /* next in hash chain */
76     char *event;                /* lwp event: an address */
77     int refcount;               /* Is it in use? */
78     int seq;                    /* Sequence number: this is incremented
79                                  * by wakeup calls; wait will not return until
80                                  * it changes */
81     kcondvar_t cond;            /* Currently associated condition variable */
82 } afs_event_t;
83
84 #define HASHSIZE 128
85 afs_event_t *afs_evhasht[HASHSIZE];     /* Hash table for events */
86 #define afs_evhash(event)       (afs_uint32) ((((long)event)>>2) & (HASHSIZE-1));
87 int afs_evhashcnt = 0;
88
89 /* Get and initialize event structure corresponding to lwp event (i.e. address)
90  * */
91 static afs_event_t *
92 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         cv_init(&newp->cond, "event cond var", CV_DEFAULT, NULL);
115         newp->seq = 0;
116     }
117     newp->event = event;
118     newp->refcount = 1;
119     return newp;
120 }
121
122 /* Release the specified event */
123 #define relevent(evp) ((evp)->refcount--)
124
125
126 void
127 afs_osi_Sleep(void *event)
128 {
129     struct afs_event *evp;
130     int seq;
131
132     evp = afs_getevent(event);
133     seq = evp->seq;
134     while (seq == evp->seq) {
135         AFS_ASSERT_GLOCK();
136         cv_wait(&evp->cond, &afs_global_lock);
137     }
138     relevent(evp);
139 }
140
141 int
142 afs_osi_SleepSig(void *event)
143 {
144     struct afs_event *evp;
145     int seq, code = 0;
146
147     evp = afs_getevent(event);
148     seq = evp->seq;
149     while (seq == evp->seq) {
150         AFS_ASSERT_GLOCK();
151         if (cv_wait_sig(&evp->cond, &afs_global_lock) == 0) {
152             code = EINTR;
153             break;
154         }
155     }
156     relevent(evp);
157     return code;
158 }
159
160 /* afs_osi_TimedSleep
161  * 
162  * Arguments:
163  * event - event to sleep on
164  * ams --- max sleep time in milliseconds
165  * aintok - 1 if should sleep interruptibly
166  *
167  * Returns 0 if timeout and EINTR if signalled.
168  */
169 int
170 afs_osi_TimedSleep(void *event, afs_int32 ams, int aintok)
171 {
172     int code = 0;
173     struct afs_event *evp;
174     clock_t ticks;
175
176     ticks = (ams * afs_hz) / 1000;
177 #if defined(AFS_SUN510_ENV)
178     ticks = ticks + ddi_get_lbolt();
179 #else
180     ticks = ticks + lbolt;
181 #endif
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 }