Standardize License information
[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 "../afs/param.h"       /* Should be always first */
11 #include "../afs/sysincludes.h" /* Standard vendor system headers */
12 #include "../afs/afsincludes.h" /* Afs-based standard headers */
13 #include "../afs/afs_stats.h"   /* afs statistics */
14
15
16
17 static int osi_TimedSleep(char *event, afs_int32 ams, int aintok);
18 void afs_osi_Wakeup(char *event);
19 void afs_osi_Sleep(char *event);
20
21 static char waitV;
22
23
24 void afs_osi_InitWaitHandle(struct afs_osi_WaitHandle *achandle)
25 {
26     AFS_STATCNT(osi_InitWaitHandle);
27     achandle->proc = (caddr_t) 0;
28 }
29
30 /* cancel osi_Wait */
31 void afs_osi_CancelWait(struct afs_osi_WaitHandle *achandle)
32 {
33     caddr_t proc;
34
35     AFS_STATCNT(osi_CancelWait);
36     proc = achandle->proc;
37     if (proc == 0) return;
38     achandle->proc = (caddr_t) 0;   /* so dude can figure out he was signalled */
39     afs_osi_Wakeup(&waitV);
40 }
41
42 /* afs_osi_Wait
43  * Waits for data on ahandle, or ams ms later.  ahandle may be null.
44  * Returns 0 if timeout and EINTR if signalled.
45  */
46 int 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 = osi_TimedSleep(&waitV, ams, aintok);
59
60         if (code) break;        /* if something happened, quit now */
61         /* if we we're cancelled, quit now */
62         if (ahandle && (ahandle->proc == (caddr_t) 0)) {
63             /* we've been signalled */
64             break;
65         }
66     } while (osi_Time() < endTime);
67     return code;
68 }
69
70
71
72
73 typedef struct afs_event {
74     struct afs_event *next;     /* next in hash chain */
75     char *event;                /* lwp event: an address */
76     int refcount;               /* Is it in use? */
77     int seq;                    /* Sequence number: this is incremented
78                                    by wakeup calls; wait will not return until
79                                    it changes */
80     kcondvar_t cond;            /* Currently associated condition variable */
81 } afs_event_t;
82
83 #define HASHSIZE 128
84 afs_event_t *afs_evhasht[HASHSIZE];/* Hash table for events */
85 #if (_MIPS_SZPTR == 64)
86 #define afs_evhash(event)       (afs_uint32) ((((long)event)>>3) & (HASHSIZE-1));
87 #else
88 #define afs_evhash(event)       (afs_uint32) ((((long)event)>>2) & (HASHSIZE-1));
89 #endif
90 int afs_evhashcnt = 0;
91
92 /* Get and initialize event structure corresponding to lwp event (i.e. address)
93  * */
94 static afs_event_t *afs_getevent(char *event)
95 {
96     afs_event_t *evp, *newp = 0;
97     int hashcode;
98
99     AFS_ASSERT_GLOCK();
100     hashcode = afs_evhash(event);
101     evp = afs_evhasht[hashcode];
102     while (evp) {
103         if (evp->event == event) {
104             evp->refcount++;
105             return evp;
106         }
107         if (evp->refcount == 0)
108             newp = evp;
109         evp = evp->next;
110     }
111     if (!newp) {
112         newp = (afs_event_t *) osi_AllocSmallSpace(sizeof (afs_event_t));
113         afs_evhashcnt++;
114         newp->next = afs_evhasht[hashcode];
115         afs_evhasht[hashcode] = newp;
116         cv_init(&newp->cond, "event cond var", CV_DEFAULT, NULL);
117         newp->seq = 0;
118     }
119     newp->event = event;
120     newp->refcount = 1;
121     return newp;
122 }
123
124 /* Release the specified event */
125 #define relevent(evp) ((evp)->refcount--)
126
127
128 void afs_osi_Sleep(char *event)
129 {
130     struct afs_event *evp;
131     int seq;
132
133     evp = afs_getevent(event);
134     seq = evp->seq;
135     while (seq == evp->seq) {
136         AFS_ASSERT_GLOCK();
137         cv_wait(&evp->cond, &afs_global_lock);
138     }
139     relevent(evp);
140 }
141
142 /* osi_TimedSleep
143  * 
144  * Arguments:
145  * event - event to sleep on
146  * ams --- max sleep time in milliseconds
147  * aintok - 1 if should sleep interruptibly
148  *
149  * Returns 0 if timeout and EINTR if signalled.
150  */
151 static int osi_TimedSleep(char *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
161     evp = afs_getevent(event);
162
163     AFS_ASSERT_GLOCK();
164     if (aintok) {
165         if (sv_timedwait_sig(&evp->cond, AFSD_PRI(), &afs_global_lock,
166                              0, 0, &ticks, (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 void afs_osi_Wakeup(char *event)
179 {
180     struct afs_event *evp;
181
182     evp = afs_getevent(event);
183     if (evp->refcount > 1) {
184         evp->seq++;    
185         cv_broadcast(&evp->cond);
186     }
187     relevent(evp);
188 }