pull-prototypes-to-head-20020821
[openafs.git] / src / afs / AIX / 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
20
21 static int osi_TimedSleep(char *event, afs_int32 ams, int aintok);
22
23 static char waitV;
24
25 static void AfsWaitHack(struct trb *trb)
26 {
27     AFS_STATCNT(WaitHack);
28
29     e_clear_wait(trb->func_data, THREAD_TIMED_OUT);
30 }
31
32 void afs_osi_InitWaitHandle(struct afs_osi_WaitHandle *achandle)
33 {
34     AFS_STATCNT(osi_InitWaitHandle);
35     achandle->proc = (caddr_t) 0;
36 }
37
38 /* cancel osi_Wait */
39 void afs_osi_CancelWait(struct afs_osi_WaitHandle *achandle)
40 {
41     caddr_t proc;
42
43     AFS_STATCNT(osi_CancelWait);
44     proc = achandle->proc;
45     if (proc == 0) return;
46     achandle->proc = (caddr_t) 0;   /* so dude can figure out he was signalled */
47     afs_osi_Wakeup(&waitV);
48 }
49
50 /* afs_osi_Wait
51  * Waits for data on ahandle, or ams ms later.  ahandle may be null.
52  * Returns 0 if timeout and EINTR if signalled.
53  */
54 int afs_osi_Wait(afs_int32 ams, struct afs_osi_WaitHandle *ahandle, int aintok)
55 {
56     int code;
57     afs_int32 endTime, tid;
58
59     AFS_STATCNT(osi_Wait);
60     endTime = osi_Time() + (ams/1000);
61     if (ahandle)
62         ahandle->proc = (caddr_t) thread_self();
63     do {
64         AFS_ASSERT_GLOCK();
65         code = 0;
66         code = osi_TimedSleep(&waitV, ams, aintok);
67
68         if (code) break;        /* if something happened, quit now */
69         /* if we we're cancelled, quit now */
70         if (ahandle && (ahandle->proc == (caddr_t) 0)) {
71             /* we've been signalled */
72             break;
73         }
74     } while (osi_Time() < endTime);
75     return code;
76 }
77
78
79
80
81 typedef struct afs_event {
82     struct afs_event *next;     /* next in hash chain */
83     char *event;                /* lwp event: an address */
84     int refcount;               /* Is it in use? */
85     int seq;                    /* Sequence number: this is incremented
86                                    by wakeup calls; wait will not return until
87                                    it changes */
88     int cond;
89 } afs_event_t;
90
91 #define HASHSIZE 128
92 afs_event_t *afs_evhasht[HASHSIZE];/* Hash table for events */
93 #define afs_evhash(event)       (afs_uint32) ((((long)event)>>2) & (HASHSIZE-1));
94 int afs_evhashcnt = 0;
95
96 /* Get and initialize event structure corresponding to lwp event (i.e. address)
97  * */
98 static afs_event_t *afs_getevent(char *event)
99 {
100     afs_event_t *evp, *newp = 0;
101     int hashcode;
102
103     AFS_ASSERT_GLOCK();
104     hashcode = afs_evhash(event);
105     evp = afs_evhasht[hashcode];
106     while (evp) {
107         if (evp->event == event) {
108             evp->refcount++;
109             return evp;
110         }
111         if (evp->refcount == 0)
112             newp = evp;
113         evp = evp->next;
114     }
115     if (!newp) {
116         newp = (afs_event_t *) osi_AllocSmallSpace(sizeof (afs_event_t));
117         afs_evhashcnt++;
118         newp->next = afs_evhasht[hashcode];
119         afs_evhasht[hashcode] = newp;
120         newp->cond = EVENT_NULL;
121         newp->seq = 0;
122     }
123     newp->event = event;
124     newp->refcount = 1;
125     return newp;
126 }
127
128 /* Release the specified event */
129 #define relevent(evp) ((evp)->refcount--)
130
131
132 void afs_osi_Sleep(void *event)
133 {
134     struct afs_event *evp;
135     int seq;
136
137     evp = afs_getevent(event);
138     seq = evp->seq;
139     while (seq == evp->seq) {
140         AFS_ASSERT_GLOCK();
141         e_assert_wait(&evp->cond, 0);
142         AFS_GUNLOCK();
143         e_block_thread();
144         AFS_GLOCK();
145     }
146     relevent(evp);
147 }
148
149 int afs_osi_SleepSig(void *event)
150 {
151     afs_osi_Sleep(event);
152     return 0;
153 }
154
155 /* osi_TimedSleep
156  * 
157  * Arguments:
158  * event - event to sleep on
159  * ams --- max sleep time in milliseconds
160  * aintok - 1 if should sleep interruptibly
161  *
162  * Returns 0 if timeout and EINTR if signalled.
163  */
164 static int osi_TimedSleep(char *event, afs_int32 ams, int aintok)
165 {
166     int code = 0;
167     struct afs_event *evp;
168     struct timestruc_t ticks;
169     struct trb *trb;
170     int rc;
171
172     ticks.tv_sec = ams / 1000;
173     ticks.tv_nsec = (ams - (ticks.tv_sec * 1000) ) * 1000000;
174
175
176     evp = afs_getevent(event);
177
178     trb = talloc();
179     if (trb == NULL)
180         osi_Panic("talloc returned NULL");
181     trb->flags = 0;
182     trb->func  = AfsWaitHack;
183     trb->eventlist = EVENT_NULL;
184     trb->ipri = INTTIMER;
185     trb->func_data = thread_self();
186     trb->timeout.it_value = ticks;
187
188     e_assert_wait(&evp->cond, aintok);
189     AFS_GUNLOCK();
190     tstart(trb);
191     rc = e_block_thread();
192     while(tstop(trb));
193     if (rc == THREAD_INTERRUPTED)
194         code = EINTR;
195     tfree(trb);
196     AFS_GLOCK();
197     
198     relevent(evp);
199     return code;
200 }
201
202
203 void afs_osi_Wakeup(void *event)
204 {
205     struct afs_event *evp;
206
207     evp = afs_getevent(event);
208     if (evp->refcount > 1) {
209         evp->seq++;    
210         e_wakeup(&evp->cond);
211     }
212     relevent(evp);
213 }