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