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