Initial IBM OpenAFS 1.0 tree
[openafs.git] / src / afs / DUX / osi_sleep.c
1 /* Copyright (C) 1995 Transarc Corporation - All rights reserved. */
2 /*
3  * (C) COPYRIGHT IBM CORPORATION 1987, 1988
4  * LICENSED MATERIALS - PROPERTY OF IBM
5  */
6
7 #include "../afs/param.h"       /* Should be always first */
8 #include "../afs/sysincludes.h" /* Standard vendor system headers */
9 #include "../afs/afsincludes.h" /* Afs-based standard headers */
10 #include "../afs/afs_stats.h"   /* afs statistics */
11
12
13
14 static int osi_TimedSleep(char *event, afs_int32 ams, int aintok);
15 void afs_osi_Wakeup(char *event);
16 void afs_osi_Sleep(char *event);
17
18 static char waitV;
19
20
21 void 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 afs_osi_CancelWait(struct afs_osi_WaitHandle *achandle)
29 {
30     caddr_t proc;
31
32     AFS_STATCNT(osi_CancelWait);
33     proc = achandle->proc;
34     if (proc == 0) return;
35     achandle->proc = (caddr_t) 0;   /* so dude can figure out he was signalled */
36     afs_osi_Wakeup(&waitV);
37 }
38
39 /* afs_osi_Wait
40  * Waits for data on ahandle, or ams ms later.  ahandle may be null.
41  * Returns 0 if timeout and EINTR if signalled.
42  */
43 int afs_osi_Wait(afs_int32 ams, struct afs_osi_WaitHandle *ahandle, int aintok)
44 {
45     int code;
46     afs_int32 endTime, tid;
47
48     AFS_STATCNT(osi_Wait);
49     endTime = osi_Time() + (ams/1000);
50     if (ahandle)
51         ahandle->proc = (caddr_t) u.u_procp;
52     do {
53         AFS_ASSERT_GLOCK();
54         code = 0;
55         code = osi_TimedSleep(&waitV, ams, aintok);
56
57         if (code) break;        /* if something happened, quit now */
58         /* if we we're cancelled, quit now */
59         if (ahandle && (ahandle->proc == (caddr_t) 0)) {
60             /* we've been signalled */
61             break;
62         }
63     } while (osi_Time() < endTime);
64     return code;
65 }
66
67
68
69
70 typedef struct afs_event {
71     struct afs_event *next;     /* next in hash chain */
72     char *event;                /* lwp event: an address */
73     int refcount;               /* Is it in use? */
74     int seq;                    /* Sequence number: this is incremented
75                                    by wakeup calls; wait will not return until
76                                    it changes */
77     int cond;
78 } afs_event_t;
79
80 #define HASHSIZE 128
81 afs_event_t *afs_evhasht[HASHSIZE];/* Hash table for events */
82 #define afs_evhash(event)       (afs_uint32) ((((long)event)>>2) & (HASHSIZE-1));
83 int afs_evhashcnt = 0;
84
85 /* Get and initialize event structure corresponding to lwp event (i.e. address)
86  * */
87 static afs_event_t *afs_getevent(char *event)
88 {
89     afs_event_t *evp, *newp = 0;
90     int hashcode;
91
92     AFS_ASSERT_GLOCK();
93     hashcode = afs_evhash(event);
94     evp = afs_evhasht[hashcode];
95     while (evp) {
96         if (evp->event == event) {
97             evp->refcount++;
98             return evp;
99         }
100         if (evp->refcount == 0)
101             newp = evp;
102         evp = evp->next;
103     }
104     if (!newp) {
105         newp = (afs_event_t *) osi_AllocSmallSpace(sizeof (afs_event_t));
106         afs_evhashcnt++;
107         newp->next = afs_evhasht[hashcode];
108         afs_evhasht[hashcode] = newp;
109         newp->seq = 0;
110     }
111     newp->event = event;
112     newp->refcount = 1;
113     return newp;
114 }
115
116 /* Release the specified event */
117 #define relevent(evp) ((evp)->refcount--)
118
119
120 void afs_osi_Sleep(char *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         assert_wait((vm_offset_t)(&evp->cond), 0);
130         AFS_GUNLOCK();
131         thread_block();
132         AFS_GLOCK();
133     }
134     relevent(evp);
135 }
136
137 /* osi_TimedSleep
138  * 
139  * Arguments:
140  * event - event to sleep on
141  * ams --- max sleep time in milliseconds
142  * aintok - 1 if should sleep interruptibly
143  *
144  * Returns 0 if timeout and EINTR if signalled.
145  */
146 static int osi_TimedSleep(char *event, afs_int32 ams, int aintok)
147 {
148     int code = 0;
149     struct afs_event *evp;
150     int ticks;
151
152     ticks = ( ams * afs_hz )/1000;
153
154
155     evp = afs_getevent(event);
156
157     assert_wait((vm_offset_t)(&evp->cond), aintok);
158     AFS_GUNLOCK();
159     thread_set_timeout(ticks);
160     thread_block();
161     AFS_GLOCK();
162     if (current_thread()->wait_result != THREAD_AWAKENED)
163         code = EINTR;
164     
165     relevent(evp);
166     return code;
167 }
168
169
170 void afs_osi_Wakeup(char *event)
171 {
172     struct afs_event *evp;
173
174     evp = afs_getevent(event);
175     if (evp->refcount > 1) {
176         evp->seq++;    
177         thread_wakeup((vm_offset_t)(&evp->cond));
178     }
179     relevent(evp);
180 }