Unix CM: Don't cast allocator returns
[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
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) curthreadp;
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 afs_event_t *afs_evhasht[AFS_EVHASHSIZE];       /* Hash table for events */
75 #if (_MIPS_SZPTR == 64)
76 #define afs_evhash(event)       (afs_uint32) ((((long)event)>>3) & (AFS_EVHASHSIZE-1));
77 #else
78 #define afs_evhash(event)       (afs_uint32) ((((long)event)>>2) & (AFS_EVHASHSIZE-1));
79 #endif
80 int afs_evhashcnt = 0;
81
82 /* Get and initialize event structure corresponding to lwp event (i.e. address)
83  * */
84 static afs_event_t *
85 afs_getevent(char *event)
86 {
87     afs_event_t *evp, *newp = 0;
88     int hashcode;
89
90     AFS_ASSERT_GLOCK();
91     hashcode = afs_evhash(event);
92     evp = afs_evhasht[hashcode];
93     while (evp) {
94         if (evp->event == event) {
95             evp->refcount++;
96             return evp;
97         }
98         if (evp->refcount == 0)
99             newp = evp;
100         evp = evp->next;
101     }
102     if (!newp) {
103         newp = osi_AllocSmallSpace(sizeof(afs_event_t));
104         afs_evhashcnt++;
105         newp->next = afs_evhasht[hashcode];
106         afs_evhasht[hashcode] = newp;
107         cv_init(&newp->cond, "event cond var", CV_DEFAULT, NULL);
108         newp->seq = 0;
109     }
110     newp->event = event;
111     newp->refcount = 1;
112     return newp;
113 }
114
115 /* Release the specified event */
116 #define relevent(evp) ((evp)->refcount--)
117
118
119 void
120 afs_osi_Sleep(void *event)
121 {
122     struct afs_event *evp;
123     int seq;
124
125     evp = afs_getevent(event);
126     seq = evp->seq;
127     while (seq == evp->seq) {
128         AFS_ASSERT_GLOCK();
129         cv_wait(&evp->cond, &afs_global_lock);
130     }
131     relevent(evp);
132 }
133
134 int
135 afs_osi_SleepSig(void *event)
136 {
137     afs_osi_Sleep(event);
138     return 0;
139 }
140
141 /* afs_osi_TimedSleep
142  * 
143  * Arguments:
144  * event - event to sleep on
145  * ams --- max sleep time in milliseconds
146  * aintok - 1 if should sleep interruptibly
147  *
148  * Returns 0 if timeout and EINTR if signalled.
149  */
150 int
151 afs_osi_TimedSleep(void *event, afs_int32 ams, int aintok)
152 {
153     int code = 0;
154     struct afs_event *evp;
155     struct timespec ticks;
156
157     ticks.tv_sec = ams / 1000;
158     ticks.tv_nsec = (ams - (ticks.tv_sec * 1000)) * 1000000;
159
160     evp = afs_getevent(event);
161
162     AFS_ASSERT_GLOCK();
163     if (aintok) {
164         if (sv_timedwait_sig
165             (&evp->cond, AFSD_PRI(), &afs_global_lock, 0, 0, &ticks,
166              (struct timespec *)0))
167             code = EINTR;
168         AFS_MUTEX_ENTER(&afs_global_lock);
169     } else {
170         cv_timedwait(&evp->cond, &afs_global_lock, ticks);
171     }
172
173     relevent(evp);
174     return code;
175 }
176
177
178 int
179 afs_osi_Wakeup(void *event)
180 {
181     int ret = 1;
182     struct afs_event *evp;
183
184     evp = afs_getevent(event);
185     if (evp->refcount > 1) {
186         evp->seq++;
187         cv_broadcast(&evp->cond);
188         ret = 0;
189     }
190     relevent(evp);
191     return ret;
192 }