include-afsconfig-before-param-h-20010712
[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 void afs_osi_Wakeup(char *event);
21 void afs_osi_Sleep(char *event);
22
23 static char waitV;
24
25 void 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 afs_osi_CancelWait(struct afs_osi_WaitHandle *achandle)
33 {
34     caddr_t proc;
35
36     AFS_STATCNT(osi_CancelWait);
37     proc = achandle->proc;
38     if (proc == 0) 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 afs_osi_Wait(afs_int32 ams, struct afs_osi_WaitHandle *ahandle, int aintok)
48 {
49     int code;
50     afs_int32 endTime, tid;
51
52     AFS_STATCNT(osi_Wait);
53     endTime = osi_Time() + (ams/1000);
54     if (ahandle)
55         ahandle->proc = (caddr_t) curthread;
56     do {
57         AFS_ASSERT_GLOCK();
58         code = 0;
59         code = osi_TimedSleep(&waitV, ams, aintok);
60
61         if (code) 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 *afs_getevent(char *event)
92 {
93     afs_event_t *evp, *newp = 0;
94     int hashcode;
95
96     AFS_ASSERT_GLOCK();
97     hashcode = afs_evhash(event);
98     evp = afs_evhasht[hashcode];
99     while (evp) {
100         if (evp->event == event) {
101             evp->refcount++;
102             return evp;
103         }
104         if (evp->refcount == 0)
105             newp = evp;
106         evp = evp->next;
107     }
108     if (!newp) {
109         newp = (afs_event_t *) osi_AllocSmallSpace(sizeof (afs_event_t));
110         afs_evhashcnt++;
111         newp->next = afs_evhasht[hashcode];
112         afs_evhasht[hashcode] = newp;
113         cv_init(&newp->cond, "event cond var", CV_DEFAULT, NULL);
114         newp->seq = 0;
115     }
116     newp->event = event;
117     newp->refcount = 1;
118     return newp;
119 }
120
121 /* Release the specified event */
122 #define relevent(evp) ((evp)->refcount--)
123
124
125 void afs_osi_Sleep(char *event)
126 {
127     struct afs_event *evp;
128     int seq;
129
130     evp = afs_getevent(event);
131     seq = evp->seq;
132     while (seq == evp->seq) {
133         AFS_ASSERT_GLOCK();
134         cv_wait(&evp->cond, &afs_global_lock);
135     }
136     relevent(evp);
137 }
138
139 /* osi_TimedSleep
140  * 
141  * Arguments:
142  * event - event to sleep on
143  * ams --- max sleep time in milliseconds
144  * aintok - 1 if should sleep interruptibly
145  *
146  * Returns 0 if timeout and EINTR if signalled.
147  */
148 static int osi_TimedSleep(char *event, afs_int32 ams, int aintok)
149 {
150     int code = 0;
151     struct afs_event *evp;
152     clock_t ticks;
153
154     ticks = ( ams * afs_hz )/1000;
155     ticks = ticks  + lbolt;
156
157     evp = afs_getevent(event);
158
159     AFS_ASSERT_GLOCK();
160     if (aintok) {
161         if (cv_timedwait_sig(&evp->cond, &afs_global_lock, ticks) == -1)
162             code = EINTR;
163     } else {
164         cv_timedwait(&evp->cond, &afs_global_lock, ticks);
165     }
166     
167     relevent(evp);
168     return code;
169 }
170
171
172 void afs_osi_Wakeup(char *event)
173 {
174     struct afs_event *evp;
175
176     evp = afs_getevent(event);
177     if (evp->refcount > 1) {
178         evp->seq++;    
179         cv_broadcast(&evp->cond);
180     }
181     relevent(evp);
182 }