2 * Copyright 2000, International Business Machines Corporation and others.
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
10 #include <afsconfig.h>
12 #include "afs/param.h"
14 #include <afs/param.h>
18 #include <sys/time_impl.h>
24 #include "afs/afs_osi.h"
26 #include "afs/sysincludes.h"
27 #include "afsincludes.h"
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
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. */
42 #if !defined(AFS_SGI64_ENV) && !defined(UKERNEL)
45 extern void *osi_Alloc();
47 #if defined(AFS_OBSD_ENV)
56 #ifdef AFS_PTHREAD_ENV
57 #include <rx/rx_pthread.h>
61 #ifdef RX_ENABLE_LOCKS
63 #endif /* RX_ENABLE_LOCKS */
64 #include "rx_globals.h"
71 /* All event processing is relative to the apparent current time given by clock_GetTime */
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 */
86 struct xfreelist *next;
88 static struct xfreelist *xfreemallocs = 0, *xsp = 0;
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 */
94 #ifdef RX_ENABLE_LOCKS
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 */
103 #ifdef AFS_PTHREAD_ENV
105 * This mutex protects the following global variables:
106 * rxevent_initialized
110 afs_kmutex_t rx_event_mutex;
111 #define LOCK_EV_INIT MUTEX_ENTER(&rx_event_mutex)
112 #define UNLOCK_EV_INIT MUTEX_EXIT(&rx_event_mutex)
115 #define UNLOCK_EV_INIT
116 #endif /* AFS_PTHREAD_ENV */
120 rxevent_adjTimes(struct clock *adjTime)
122 /* backwards clock correction */
124 struct rxepoch *qep, *nqep;
125 struct rxevent *qev, *nqev;
127 for (queue_Scan(&rxepoch_queue, qep, nqep, rxepoch)) {
128 for (queue_Scan(&qep->events, qev, nqev, rxevent)) {
129 if (clock_Gt(&qev->eventTime, adjTime)) {
130 clock_Sub(&qev->eventTime, adjTime);
134 if (qep->epochSec > adjTime->sec) {
135 qep->epochSec -= adjTime->sec;
141 /* Pass in the number of events to allocate at a time */
142 int rxevent_initialized = 0;
144 rxevent_Init(int nEvents, void (*scheduler) (void))
147 if (rxevent_initialized) {
151 MUTEX_INIT(&rxevent_lock, "rxevent_lock", MUTEX_DEFAULT, 0);
154 rxevent_allocUnit = nEvents;
155 queue_Init(&rxevent_free);
156 queue_Init(&rxepoch_free);
157 queue_Init(&rxepoch_queue);
158 rxevent_nFree = rxevent_nPosted = 0;
160 rxevent_ScheduledEarlierEvent = scheduler;
161 rxevent_initialized = 1;
162 clock_Zero(&rxevent_nextRaiseEvents);
163 clock_Zero(&rxevent_lastEvent);
164 rxevent_raiseScheduled = 0;
168 /* Create and initialize new epoch structure */
170 rxepoch_Allocate(struct clock *when)
175 /* If we are short on free epoch entries, create a block of new oned
176 * and add them to the free queue */
177 if (queue_IsEmpty(&rxepoch_free)) {
178 #if defined(AFS_AIX32_ENV) && defined(KERNEL)
179 ep = (struct rxepoch *)rxi_Alloc(sizeof(struct rxepoch));
180 queue_Append(&rxepoch_free, &ep[0]), rxepoch_nFree++;
182 #if defined(KERNEL) && !defined(UKERNEL) && defined(AFS_FBSD80_ENV)
183 ep = (struct rxepoch *)
184 afs_osi_Alloc_NoSleep(sizeof(struct rxepoch) * rxepoch_allocUnit);
187 (struct xfreelist *)afs_osi_Alloc_NoSleep(sizeof(struct xfreelist));
189 ep = (struct rxepoch *)
190 osi_Alloc(sizeof(struct rxepoch) * rxepoch_allocUnit);
193 (struct xfreelist *)osi_Alloc(sizeof(struct xfreelist));
195 xfreemallocs->mem = (void *)ep;
196 xfreemallocs->size = sizeof(struct rxepoch) * rxepoch_allocUnit;
197 xfreemallocs->next = xsp;
198 for (i = 0; i < rxepoch_allocUnit; i++)
199 queue_Append(&rxepoch_free, &ep[i]), rxepoch_nFree++;
202 ep = queue_First(&rxepoch_free, rxepoch);
205 ep->epochSec = when->sec;
206 queue_Init(&ep->events);
210 /* Add the indicated event (function, arg) at the specified clock time. The
211 * "when" argument specifies when "func" should be called, in clock (clock.h)
214 static struct rxevent *
215 _rxevent_Post(struct clock *when, struct clock *now,
216 void (*func) (struct rxevent *, void *, void *, int),
217 void *arg, void *arg1, int arg2, int newargs)
219 struct rxevent *ev, *evqe, *evqpr;
220 struct rxepoch *ep, *epqe, *epqpr;
223 MUTEX_ENTER(&rxevent_lock);
227 clock_GetTime(&now1);
228 fprintf(rx_Log_event, "%ld.%ld: rxevent_Post(%ld.%ld, %lp, %lp, %lp, %d)\n",
229 afs_printable_int32_ld(now1.sec),
230 afs_printable_int32_ld(now1.usec),
231 afs_printable_int32_ld(when->sec),
232 afs_printable_int32_ld(when->usec),
237 /* If a time was provided, check for consistency */
239 if (clock_Gt(&rxevent_lastEvent, now)) {
240 struct clock adjTime = rxevent_lastEvent;
241 clock_Sub(&adjTime, now);
242 rxevent_adjTimes(&adjTime);
244 rxevent_lastEvent = *now;
246 /* Get a pointer to the epoch for this event, if none is found then
247 * create a new epoch and insert it into the sorted list */
248 for (ep = NULL, queue_ScanBackwards(&rxepoch_queue, epqe, epqpr, rxepoch)) {
249 if (when->sec == epqe->epochSec) {
250 /* already have an structure for this epoch */
252 if (ep == queue_First(&rxepoch_queue, rxepoch))
255 } else if (when->sec > epqe->epochSec) {
256 /* Create a new epoch and insert after qe */
257 ep = rxepoch_Allocate(when);
258 queue_InsertAfter(epqe, ep);
263 /* Create a new epoch and place it at the head of the list */
264 ep = rxepoch_Allocate(when);
265 queue_Prepend(&rxepoch_queue, ep);
269 /* If we're short on free event entries, create a block of new ones and add
270 * them to the free queue */
271 if (queue_IsEmpty(&rxevent_free)) {
273 #if defined(AFS_AIX32_ENV) && defined(KERNEL)
274 ev = (struct rxevent *)rxi_Alloc(sizeof(struct rxevent));
275 queue_Append(&rxevent_free, &ev[0]), rxevent_nFree++;
278 #if defined(KERNEL) && !defined(UKERNEL) && defined(AFS_FBSD80_ENV)
279 ev = (struct rxevent *)afs_osi_Alloc_NoSleep(sizeof(struct rxevent) *
283 (struct xfreelist *)afs_osi_Alloc_NoSleep(sizeof(struct xfreelist));
285 ev = (struct rxevent *)osi_Alloc(sizeof(struct rxevent) *
289 (struct xfreelist *)osi_Alloc(sizeof(struct xfreelist));
291 xfreemallocs->mem = (void *)ev;
292 xfreemallocs->size = sizeof(struct rxevent) * rxevent_allocUnit;
293 xfreemallocs->next = xsp;
294 for (i = 0; i < rxevent_allocUnit; i++)
295 queue_Append(&rxevent_free, &ev[i]), rxevent_nFree++;
299 /* Grab and initialize a new rxevent structure */
300 ev = queue_First(&rxevent_free, rxevent);
304 /* Record user defined event state */
305 ev->eventTime = *when;
307 ev->func.newfunc = func;
309 ev->func.oldfunc = (void (*)(struct rxevent *, void *, void*))func;
314 ev->newargs = newargs;
315 rxevent_nPosted += 1; /* Rather than ++, to shut high-C up
316 * regarding never-set variables
319 /* Insert the event into the sorted list of events for this epoch */
320 for (queue_ScanBackwards(&ep->events, evqe, evqpr, rxevent)) {
321 if (when->usec >= evqe->eventTime.usec) {
322 /* Insert event after evqe */
323 queue_InsertAfter(evqe, ev);
324 MUTEX_EXIT(&rxevent_lock);
328 /* Insert event at head of current epoch */
329 queue_Prepend(&ep->events, ev);
330 if (isEarliest && rxevent_ScheduledEarlierEvent
331 && (!rxevent_raiseScheduled
332 || clock_Lt(&ev->eventTime, &rxevent_nextRaiseEvents))) {
333 rxevent_raiseScheduled = 1;
334 clock_Zero(&rxevent_nextRaiseEvents);
335 MUTEX_EXIT(&rxevent_lock);
336 /* Notify our external scheduler */
337 (*rxevent_ScheduledEarlierEvent) ();
338 MUTEX_ENTER(&rxevent_lock);
340 MUTEX_EXIT(&rxevent_lock);
345 rxevent_Post(struct clock *when,
346 void (*func) (struct rxevent *, void *, void *),
347 void *arg, void *arg1)
351 return _rxevent_Post(when, &now,
352 (void (*)(struct rxevent *, void *, void *, int))func,
357 rxevent_Post2(struct clock *when,
358 void (*func) (struct rxevent *, void *, void *, int),
359 void *arg, void *arg1, int arg2)
363 return _rxevent_Post(when, &now, func, arg, arg1, arg2, 1);
367 rxevent_PostNow(struct clock *when, struct clock *now,
368 void (*func) (struct rxevent *, void *, void *),
369 void *arg, void *arg1)
371 return _rxevent_Post(when, now,
372 (void (*)(struct rxevent *, void *, void *, int))func,
377 rxevent_PostNow2(struct clock *when, struct clock *now,
378 void (*func) (struct rxevent *, void *, void *, int),
379 void *arg, void *arg1, int arg2)
381 return _rxevent_Post(when, now, func, arg, arg1, arg2, 1);
384 /* Cancel an event by moving it from the event queue to the free list.
385 * Warning, the event must be on the event queue! If not, this should core
386 * dump (reference through 0). This routine should be called using the macro
387 * event_Cancel, which checks for a null event and also nulls the caller's
388 * event pointer after cancelling the event.
390 #ifdef RX_ENABLE_LOCKS
391 #ifdef RX_REFCOUNT_CHECK
392 int rxevent_Cancel_type = 0;
397 rxevent_Cancel_1(struct rxevent *ev, struct rx_call *call,
404 fprintf(rx_Log_event, "%d.%d: rxevent_Cancel_1(%d.%d, %lp, %lp)\n",
405 (int)now.sec, (int)now.usec, (int)ev->eventTime.sec,
406 (int)ev->eventTime.usec, ev->func.newfunc,
410 /* Append it to the free list (rather than prepending) to keep the free
411 * list hot so nothing pages out
413 MUTEX_ENTER(&rxevent_lock);
415 MUTEX_EXIT(&rxevent_lock);
418 #ifdef RX_ENABLE_LOCKS
419 /* It's possible we're currently processing this event. */
420 if (queue_IsOnQueue(ev)) {
421 queue_MoveAppend(&rxevent_free, ev);
426 #ifdef RX_REFCOUNT_CHECK
427 call->refCDebug[type]--;
428 if (call->refCDebug[type] < 0) {
429 rxevent_Cancel_type = type;
430 osi_Panic("rxevent_Cancel: call refCount < 0");
432 #endif /* RX_REFCOUNT_CHECK */
435 #else /* RX_ENABLE_LOCKS */
436 queue_MoveAppend(&rxevent_free, ev);
439 #endif /* RX_ENABLE_LOCKS */
440 MUTEX_EXIT(&rxevent_lock);
443 /* Process all epochs that have expired relative to the current clock time
444 * (which is not re-evaluated unless clock_NewTime has been called). The
445 * relative time to the next epoch is returned in the output parameter next
446 * and the function returns 1. If there are is no next epoch, the function
450 rxevent_RaiseEvents(struct clock *next)
454 volatile struct clock now;
455 MUTEX_ENTER(&rxevent_lock);
457 /* Events are sorted by time, so only scan until an event is found that has
458 * not yet timed out */
461 while (queue_IsNotEmpty(&rxepoch_queue)) {
462 ep = queue_First(&rxepoch_queue, rxepoch);
463 if (queue_IsEmpty(&ep->events)) {
465 queue_Append(&rxepoch_free, ep);
471 ev = queue_First(&ep->events, rxevent);
472 if (clock_Lt(&now, &ev->eventTime)) {
474 if (clock_Gt(&rxevent_lastEvent, &now)) {
475 struct clock adjTime = rxevent_lastEvent;
477 clock_Sub(&adjTime, &now);
478 adjusted = rxevent_adjTimes(&adjTime);
479 rxevent_lastEvent = now;
483 if (clock_Lt(&now, &ev->eventTime)) {
484 *next = rxevent_nextRaiseEvents = ev->eventTime;
485 rxevent_raiseScheduled = 1;
486 clock_Sub(next, &now);
487 MUTEX_EXIT(&rxevent_lock);
493 MUTEX_EXIT(&rxevent_lock);
495 ev->func.newfunc(ev, ev->arg, ev->arg1, ev->arg2);
497 ev->func.oldfunc(ev, ev->arg, ev->arg1);
499 MUTEX_ENTER(&rxevent_lock);
500 queue_Append(&rxevent_free, ev);
502 } while (queue_IsNotEmpty(&ep->events));
506 fprintf(rx_Log_event, "rxevent_RaiseEvents(%d.%d)\n", (int)now.sec,
509 rxevent_raiseScheduled = 0;
510 MUTEX_EXIT(&rxevent_lock);
515 shutdown_rxevent(void)
517 struct xfreelist *xp, *nxp;
520 if (!rxevent_initialized) {
524 rxevent_initialized = 0;
526 MUTEX_DESTROY(&rxevent_lock);
527 #if defined(AFS_AIX32_ENV) && defined(KERNEL)
528 /* Everything is freed in afs_osinet.c */
533 osi_Free((char *)xp->mem, xp->size);
534 osi_Free((char *)xp, sizeof(struct xfreelist));