First pass at better signal handling:
[openafs.git] / src / afs / IRIX / 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
28 void afs_osi_InitWaitHandle(struct afs_osi_WaitHandle *achandle)
29 {
30     AFS_STATCNT(osi_InitWaitHandle);
31     achandle->proc = (caddr_t) 0;
32 }
33
34 /* cancel osi_Wait */
35 void afs_osi_CancelWait(struct afs_osi_WaitHandle *achandle)
36 {
37     caddr_t proc;
38
39     AFS_STATCNT(osi_CancelWait);
40     proc = achandle->proc;
41     if (proc == 0) return;
42     achandle->proc = (caddr_t) 0;   /* so dude can figure out he was signalled */
43     afs_osi_Wakeup(&waitV);
44 }
45
46 /* afs_osi_Wait
47  * Waits for data on ahandle, or ams ms later.  ahandle may be null.
48  * Returns 0 if timeout and EINTR if signalled.
49  */
50 int 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) curthreadp;
59     do {
60         AFS_ASSERT_GLOCK();
61         code = 0;
62         code = osi_TimedSleep(&waitV, ams, aintok);
63
64         if (code) break;        /* if something happened, quit now */
65         /* if we we're cancelled, quit now */
66         if (ahandle && (ahandle->proc == (caddr_t) 0)) {
67             /* we've been signalled */
68             break;
69         }
70     } while (osi_Time() < endTime);
71     return code;
72 }
73
74
75
76
77 typedef struct afs_event {
78     struct afs_event *next;     /* next in hash chain */
79     char *event;                /* lwp event: an address */
80     int refcount;               /* Is it in use? */
81     int seq;                    /* Sequence number: this is incremented
82                                    by wakeup calls; wait will not return until
83                                    it changes */
84     kcondvar_t cond;            /* Currently associated condition variable */
85 } afs_event_t;
86
87 #define HASHSIZE 128
88 afs_event_t *afs_evhasht[HASHSIZE];/* Hash table for events */
89 #if (_MIPS_SZPTR == 64)
90 #define afs_evhash(event)       (afs_uint32) ((((long)event)>>3) & (HASHSIZE-1));
91 #else
92 #define afs_evhash(event)       (afs_uint32) ((((long)event)>>2) & (HASHSIZE-1));
93 #endif
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         cv_init(&newp->cond, "event cond var", CV_DEFAULT, 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(char *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         cv_wait(&evp->cond, &afs_global_lock);
142     }
143     relevent(evp);
144 }
145
146 int afs_osi_SleepSig(char *event)
147 {
148     afs_osi_Sleep(event);
149     return 0;
150 }
151
152 /* osi_TimedSleep
153  * 
154  * Arguments:
155  * event - event to sleep on
156  * ams --- max sleep time in milliseconds
157  * aintok - 1 if should sleep interruptibly
158  *
159  * Returns 0 if timeout and EINTR if signalled.
160  */
161 static int osi_TimedSleep(char *event, afs_int32 ams, int aintok)
162 {
163     int code = 0;
164     struct afs_event *evp;
165     struct timespec ticks;
166
167     ticks.tv_sec = ams / 1000;
168     ticks.tv_nsec = (ams - (ticks.tv_sec * 1000) ) * 1000000;
169
170
171     evp = afs_getevent(event);
172
173     AFS_ASSERT_GLOCK();
174     if (aintok) {
175         if (sv_timedwait_sig(&evp->cond, AFSD_PRI(), &afs_global_lock,
176                              0, 0, &ticks, (struct timespec*)0))
177             code = EINTR;
178         AFS_MUTEX_ENTER(&afs_global_lock); 
179     } else {
180         cv_timedwait(&evp->cond, &afs_global_lock, ticks);
181     }
182     
183     relevent(evp);
184     return code;
185 }
186
187
188 void afs_osi_Wakeup(char *event)
189 {
190     struct afs_event *evp;
191
192     evp = afs_getevent(event);
193     if (evp->refcount > 1) {
194         evp->seq++;    
195         cv_broadcast(&evp->cond);
196     }
197     relevent(evp);
198 }