dux-rxk-init-is-void-20040808
[openafs.git] / src / rx / rx_event.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 #ifdef  KERNEL
12 #include "afs/param.h"
13 #else
14 #include <afs/param.h>
15 #endif
16
17 #ifdef AFS_SUN59_ENV
18 #include <sys/time_impl.h>
19 #endif
20
21 RCSID
22     ("$Header$");
23
24 #ifdef KERNEL
25 #ifndef UKERNEL
26 #include "afs/afs_osi.h"
27 #else /* !UKERNEL */
28 #include "afs/sysincludes.h"
29 #include "afsincludes.h"
30 #endif /* !UKERNEL */
31 #include "rx/rx_clock.h"
32 #include "rx/rx_queue.h"
33 #include "rx/rx_event.h"
34 #include "rx/rx_kernel.h"
35 #include "rx_kmutex.h"
36 #ifdef RX_ENABLE_LOCKS
37 #include "rx/rx.h"
38 #endif /* RX_ENABLE_LOCKS */
39 #include "rx/rx_globals.h"
40 #if defined(AFS_SGI_ENV)
41 #include "sys/debug.h"
42 /* These are necessary to get curproc (used by GLOCK asserts) to work. */
43 #include "h/proc.h"
44 #if !defined(AFS_SGI64_ENV) && !defined(UKERNEL)
45 #include "h/user.h"
46 #endif
47 extern void *osi_Alloc();
48 #endif
49 #if defined(AFS_OBSD_ENV)
50 #include "h/proc.h"
51 #endif
52 #else /* KERNEL */
53 #include <stdio.h>
54 #include "rx_clock.h"
55 #include "rx_queue.h"
56 #include "rx_event.h"
57 #include "rx_user.h"
58 #ifdef AFS_PTHREAD_ENV
59 #include <rx/rx_pthread.h>
60 #else
61 #include "rx_lwp.h"
62 #endif
63 #ifdef RX_ENABLE_LOCKS
64 #include "rx.h"
65 #endif /* RX_ENABLE_LOCKS */
66 #include "rx_globals.h"
67 #ifdef AFS_NT40_ENV
68 #include <malloc.h>
69 #endif
70 #endif /* KERNEL */
71
72
73 /* All event processing is relative to the apparent current time given by clock_GetTime */
74
75 /* This should be static, but event_test wants to look at the free list... */
76 struct rx_queue rxevent_free;   /* It's somewhat bogus to use a doubly-linked queue for the free list */
77 struct rx_queue rxepoch_free;   /* It's somewhat bogus to use a doubly-linked queue for the free list */
78 static struct rx_queue rxepoch_queue;   /* list of waiting epochs */
79 static int rxevent_allocUnit = 10;      /* Allocation unit (number of event records to allocate at one time) */
80 static int rxepoch_allocUnit = 10;      /* Allocation unit (number of epoch records to allocate at one time) */
81 int rxevent_nFree;              /* Number of free event records */
82 int rxevent_nPosted;            /* Current number of posted events */
83 int rxepoch_nFree;              /* Number of free epoch records */
84 static void (*rxevent_ScheduledEarlierEvent) (void);    /* Proc to call when an event is scheduled that is earlier than all other events */
85 struct xfreelist {
86     void *mem;
87     int size;
88     struct xfreelist *next;
89 };
90 static struct xfreelist *xfreemallocs = 0, *xsp = 0;
91
92 struct clock rxevent_nextRaiseEvents;   /* Time of next call to raise events */
93 int rxevent_raiseScheduled;     /* true if raise events is scheduled */
94
95 #ifdef RX_ENABLE_LOCKS
96 #ifdef RX_LOCKS_DB
97 /* rxdb_fileID is used to identify the lock location, along with line#. */
98 static int rxdb_fileID = RXDB_FILE_RX_EVENT;
99 #endif /* RX_LOCKS_DB */
100 #define RX_ENABLE_LOCKS  1
101 afs_kmutex_t rxevent_lock;
102 #endif /* RX_ENABLE_LOCKS */
103
104 #ifdef AFS_PTHREAD_ENV
105 /*
106  * This mutex protects the following global variables:
107  * rxevent_initialized
108  */
109
110 #include <assert.h>
111 pthread_mutex_t rx_event_mutex;
112 #define LOCK_EV_INIT assert(pthread_mutex_lock(&rx_event_mutex)==0);
113 #define UNLOCK_EV_INIT assert(pthread_mutex_unlock(&rx_event_mutex)==0);
114 #else
115 #define LOCK_EV_INIT
116 #define UNLOCK_EV_INIT
117 #endif /* AFS_PTHREAD_ENV */
118
119
120 /* Pass in the number of events to allocate at a time */
121 int rxevent_initialized = 0;
122 void
123 rxevent_Init(int nEvents, void (*scheduler) (void))
124 {
125     LOCK_EV_INIT if (rxevent_initialized) {
126         UNLOCK_EV_INIT return;
127     }
128     MUTEX_INIT(&rxevent_lock, "rxevent_lock", MUTEX_DEFAULT, 0);
129     clock_Init();
130     if (nEvents)
131         rxevent_allocUnit = nEvents;
132     queue_Init(&rxevent_free);
133     queue_Init(&rxepoch_free);
134     queue_Init(&rxepoch_queue);
135     rxevent_nFree = rxevent_nPosted = 0;
136     rxepoch_nFree = 0;
137     rxevent_ScheduledEarlierEvent = scheduler;
138     rxevent_initialized = 1;
139     clock_Zero(&rxevent_nextRaiseEvents);
140     rxevent_raiseScheduled = 0;
141 UNLOCK_EV_INIT}
142
143 /* Create and initialize new epoch structure */
144 struct rxepoch *
145 rxepoch_Allocate(struct clock *when)
146 {
147     struct rxepoch *ep;
148     int i;
149
150     /* If we are short on free epoch entries, create a block of new oned
151      * and add them to the free queue */
152     if (queue_IsEmpty(&rxepoch_free)) {
153 #if    defined(AFS_AIX32_ENV) && defined(KERNEL)
154         ep = (struct rxepoch *)rxi_Alloc(sizeof(struct rxepoch));
155         queue_Append(&rxepoch_free, &ep[0]), rxepoch_nFree++;
156 #else
157         ep = (struct rxepoch *)
158             osi_Alloc(sizeof(struct rxepoch) * rxepoch_allocUnit);
159         xsp = xfreemallocs;
160         xfreemallocs =
161             (struct xfreelist *)osi_Alloc(sizeof(struct xfreelist));
162         xfreemallocs->mem = (void *)ep;
163         xfreemallocs->size = sizeof(struct rxepoch) * rxepoch_allocUnit;
164         xfreemallocs->next = xsp;
165         for (i = 0; i < rxepoch_allocUnit; i++)
166             queue_Append(&rxepoch_free, &ep[i]), rxepoch_nFree++;
167 #endif
168     }
169     ep = queue_First(&rxepoch_free, rxepoch);
170     queue_Remove(ep);
171     rxepoch_nFree--;
172     ep->epochSec = when->sec;
173     queue_Init(&ep->events);
174     return ep;
175 }
176
177 /* Add the indicated event (function, arg) at the specified clock time.  The
178  * "when" argument specifies when "func" should be called, in clock (clock.h)
179  * units. */
180
181 #if 0
182 struct rxevent *
183 rxevent_Post(struct clock *when,
184              void (*func) (struct rxevent * event,
185                            struct rx_connection * conn,
186                            struct rx_call * acall), void *arg, void *arg1)
187 #else
188 struct rxevent *
189 rxevent_Post(struct clock *when, void (*func) (), void *arg, void *arg1)
190 #endif
191 {
192     register struct rxevent *ev, *evqe, *evqpr;
193     register struct rxepoch *ep, *epqe, *epqpr;
194     int isEarliest = 0;
195
196     MUTEX_ENTER(&rxevent_lock);
197     AFS_ASSERT_RXGLOCK();
198 #ifdef RXDEBUG
199     if (rx_Log_event) {
200         struct clock now;
201         clock_GetTime(&now);
202         fprintf(rx_Log_event, "%d.%d: rxevent_Post(%d.%d, %x, %x)\n",
203                 (int)now.sec, (int)now.usec, (int)when->sec, (int)when->usec,
204                 (unsigned int)func, (unsigned int)arg);
205     }
206 #endif
207
208     /* Get a pointer to the epoch for this event, if none is found then
209      * create a new epoch and insert it into the sorted list */
210     for (ep = NULL, queue_ScanBackwards(&rxepoch_queue, epqe, epqpr, rxepoch)) {
211         if (when->sec == epqe->epochSec) {
212             /* already have an structure for this epoch */
213             ep = epqe;
214             if (ep == queue_First(&rxepoch_queue, rxepoch))
215                 isEarliest = 1;
216             break;
217         } else if (when->sec > epqe->epochSec) {
218             /* Create a new epoch and insert after qe */
219             ep = rxepoch_Allocate(when);
220             queue_InsertAfter(epqe, ep);
221             break;
222         }
223     }
224     if (ep == NULL) {
225         /* Create a new epoch and place it at the head of the list */
226         ep = rxepoch_Allocate(when);
227         queue_Prepend(&rxepoch_queue, ep);
228         isEarliest = 1;
229     }
230
231     /* If we're short on free event entries, create a block of new ones and add
232      * them to the free queue */
233     if (queue_IsEmpty(&rxevent_free)) {
234         register int i;
235 #if     defined(AFS_AIX32_ENV) && defined(KERNEL)
236         ev = (struct rxevent *)rxi_Alloc(sizeof(struct rxevent));
237         queue_Append(&rxevent_free, &ev[0]), rxevent_nFree++;
238 #else
239         ev = (struct rxevent *)osi_Alloc(sizeof(struct rxevent) *
240                                          rxevent_allocUnit);
241         xsp = xfreemallocs;
242         xfreemallocs =
243             (struct xfreelist *)osi_Alloc(sizeof(struct xfreelist));
244         xfreemallocs->mem = (void *)ev;
245         xfreemallocs->size = sizeof(struct rxevent) * rxevent_allocUnit;
246         xfreemallocs->next = xsp;
247         for (i = 0; i < rxevent_allocUnit; i++)
248             queue_Append(&rxevent_free, &ev[i]), rxevent_nFree++;
249 #endif
250     }
251
252     /* Grab and initialize a new rxevent structure */
253     ev = queue_First(&rxevent_free, rxevent);
254     queue_Remove(ev);
255     rxevent_nFree--;
256
257     /* Record user defined event state */
258     ev->eventTime = *when;
259     ev->func = func;
260     ev->arg = arg;
261     ev->arg1 = arg1;
262     rxevent_nPosted += 1;       /* Rather than ++, to shut high-C up
263                                  *  regarding never-set variables
264                                  */
265
266     /* Insert the event into the sorted list of events for this epoch */
267     for (queue_ScanBackwards(&ep->events, evqe, evqpr, rxevent)) {
268         if (when->usec >= evqe->eventTime.usec) {
269             /* Insert event after evqe */
270             queue_InsertAfter(evqe, ev);
271             MUTEX_EXIT(&rxevent_lock);
272             return ev;
273         }
274     }
275     /* Insert event at head of current epoch */
276     queue_Prepend(&ep->events, ev);
277     if (isEarliest && rxevent_ScheduledEarlierEvent
278         && (!rxevent_raiseScheduled
279             || clock_Lt(&ev->eventTime, &rxevent_nextRaiseEvents))) {
280         rxevent_raiseScheduled = 1;
281         clock_Zero(&rxevent_nextRaiseEvents);
282         MUTEX_EXIT(&rxevent_lock);
283         /* Notify our external scheduler */
284         (*rxevent_ScheduledEarlierEvent) ();
285         MUTEX_ENTER(&rxevent_lock);
286     }
287     MUTEX_EXIT(&rxevent_lock);
288     return ev;
289 }
290
291 /* Cancel an event by moving it from the event queue to the free list.
292  * Warning, the event must be on the event queue!  If not, this should core
293  * dump (reference through 0).  This routine should be called using the macro
294  * event_Cancel, which checks for a null event and also nulls the caller's
295  * event pointer after cancelling the event.
296  */
297 #ifdef RX_ENABLE_LOCKS
298 #ifdef RX_REFCOUNT_CHECK
299 int rxevent_Cancel_type = 0;
300 #endif
301 #endif
302
303 void
304 rxevent_Cancel_1(register struct rxevent *ev, register struct rx_call *call,
305                  register int type)
306 {
307 #ifdef RXDEBUG
308     if (rx_Log_event) {
309         struct clock now;
310         clock_GetTime(&now);
311         fprintf(rx_Log_event, "%d.%d: rxevent_Cancel_1(%d.%d, %x, %x)\n",
312                 (int)now.sec, (int)now.usec, (int)ev->eventTime.sec,
313                 (int)ev->eventTime.usec, (unsigned int)ev->func,
314                 (unsigned int)ev->arg);
315     }
316 #endif
317     /* Append it to the free list (rather than prepending) to keep the free
318      * list hot so nothing pages out
319      */
320     AFS_ASSERT_RXGLOCK();
321     MUTEX_ENTER(&rxevent_lock);
322     if (!ev) {
323         MUTEX_EXIT(&rxevent_lock);
324         return;
325     }
326 #ifdef RX_ENABLE_LOCKS
327     /* It's possible we're currently processing this event. */
328     if (queue_IsOnQueue(ev)) {
329         queue_MoveAppend(&rxevent_free, ev);
330         rxevent_nPosted--;
331         rxevent_nFree++;
332         if (call) {
333             call->refCount--;
334 #ifdef RX_REFCOUNT_CHECK
335             call->refCDebug[type]--;
336             if (call->refCDebug[type] < 0) {
337                 rxevent_Cancel_type = type;
338                 osi_Panic("rxevent_Cancel: call refCount < 0");
339             }
340 #endif /* RX_REFCOUNT_CHECK */
341         }
342     }
343 #else /* RX_ENABLE_LOCKS */
344     queue_MoveAppend(&rxevent_free, ev);
345     rxevent_nPosted--;
346     rxevent_nFree++;
347 #endif /* RX_ENABLE_LOCKS */
348     MUTEX_EXIT(&rxevent_lock);
349 }
350
351 /* Process all epochs that have expired relative to the current clock time
352  * (which is not re-evaluated unless clock_NewTime has been called).  The
353  * relative time to the next epoch is returned in the output parameter next
354  * and the function returns 1.  If there are is no next epoch, the function
355  * returns 0.
356  */
357 int
358 rxevent_RaiseEvents(struct clock *next)
359 {
360     register struct rxepoch *ep;
361     register struct rxevent *ev;
362     volatile struct clock now;
363
364     MUTEX_ENTER(&rxevent_lock);
365
366     AFS_ASSERT_RXGLOCK();
367
368     /* Events are sorted by time, so only scan until an event is found that has
369      * not yet timed out */
370
371     clock_Zero(&now);
372     while (queue_IsNotEmpty(&rxepoch_queue)) {
373         ep = queue_First(&rxepoch_queue, rxepoch);
374         if (queue_IsEmpty(&ep->events)) {
375             queue_Remove(ep);
376             queue_Append(&rxepoch_free, ep);
377             rxepoch_nFree++;
378             continue;
379         }
380         do {
381             ev = queue_First(&ep->events, rxevent);
382             if (clock_Lt(&now, &ev->eventTime)) {
383                 clock_GetTime(&now);
384                 if (clock_Lt(&now, &ev->eventTime)) {
385                     *next = rxevent_nextRaiseEvents = ev->eventTime;
386                     rxevent_raiseScheduled = 1;
387                     clock_Sub(next, &now);
388                     MUTEX_EXIT(&rxevent_lock);
389                     return 1;
390                 }
391             }
392             queue_Remove(ev);
393             rxevent_nPosted--;
394             MUTEX_EXIT(&rxevent_lock);
395             ev->func(ev, ev->arg, ev->arg1);
396             MUTEX_ENTER(&rxevent_lock);
397             queue_Append(&rxevent_free, ev);
398             rxevent_nFree++;
399         } while (queue_IsNotEmpty(&ep->events));
400     }
401 #ifdef RXDEBUG
402     if (rx_Log_event)
403         fprintf(rx_Log_event, "rxevent_RaiseEvents(%d.%d)\n", (int)now.sec,
404                 (int)now.usec);
405 #endif
406     rxevent_raiseScheduled = 0;
407     MUTEX_EXIT(&rxevent_lock);
408     return 0;
409 }
410
411 void
412 shutdown_rxevent(void)
413 {
414     struct xfreelist *xp, *nxp;
415
416     LOCK_EV_INIT if (!rxevent_initialized) {
417         UNLOCK_EV_INIT return;
418     }
419     rxevent_initialized = 0;
420     UNLOCK_EV_INIT MUTEX_DESTROY(&rxevent_lock);
421 #if     defined(AFS_AIX32_ENV) && defined(KERNEL)
422     /* Everything is freed in afs_osinet.c */
423 #else
424     xp = xfreemallocs;
425     while (xp) {
426         nxp = xp->next;
427         osi_Free((char *)xp->mem, xp->size);
428         osi_Free((char *)xp, sizeof(struct xfreelist));
429         xp = nxp;
430     }
431     xfreemallocs = NULL;
432 #endif
433
434 }