kill-rxglock-20050413
[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;
126     if (rxevent_initialized) {
127         UNLOCK_EV_INIT;
128         return;
129     }
130     MUTEX_INIT(&rxevent_lock, "rxevent_lock", MUTEX_DEFAULT, 0);
131     clock_Init();
132     if (nEvents)
133         rxevent_allocUnit = nEvents;
134     queue_Init(&rxevent_free);
135     queue_Init(&rxepoch_free);
136     queue_Init(&rxepoch_queue);
137     rxevent_nFree = rxevent_nPosted = 0;
138     rxepoch_nFree = 0;
139     rxevent_ScheduledEarlierEvent = scheduler;
140     rxevent_initialized = 1;
141     clock_Zero(&rxevent_nextRaiseEvents);
142     rxevent_raiseScheduled = 0;
143     UNLOCK_EV_INIT;
144 }
145
146 /* Create and initialize new epoch structure */
147 struct rxepoch *
148 rxepoch_Allocate(struct clock *when)
149 {
150     struct rxepoch *ep;
151     int i;
152
153     /* If we are short on free epoch entries, create a block of new oned
154      * and add them to the free queue */
155     if (queue_IsEmpty(&rxepoch_free)) {
156 #if    defined(AFS_AIX32_ENV) && defined(KERNEL)
157         ep = (struct rxepoch *)rxi_Alloc(sizeof(struct rxepoch));
158         queue_Append(&rxepoch_free, &ep[0]), rxepoch_nFree++;
159 #else
160         ep = (struct rxepoch *)
161             osi_Alloc(sizeof(struct rxepoch) * rxepoch_allocUnit);
162         xsp = xfreemallocs;
163         xfreemallocs =
164             (struct xfreelist *)osi_Alloc(sizeof(struct xfreelist));
165         xfreemallocs->mem = (void *)ep;
166         xfreemallocs->size = sizeof(struct rxepoch) * rxepoch_allocUnit;
167         xfreemallocs->next = xsp;
168         for (i = 0; i < rxepoch_allocUnit; i++)
169             queue_Append(&rxepoch_free, &ep[i]), rxepoch_nFree++;
170 #endif
171     }
172     ep = queue_First(&rxepoch_free, rxepoch);
173     queue_Remove(ep);
174     rxepoch_nFree--;
175     ep->epochSec = when->sec;
176     queue_Init(&ep->events);
177     return ep;
178 }
179
180 /* Add the indicated event (function, arg) at the specified clock time.  The
181  * "when" argument specifies when "func" should be called, in clock (clock.h)
182  * units. */
183
184 #if 0
185 struct rxevent *
186 rxevent_Post(struct clock *when,
187              void (*func) (struct rxevent * event,
188                            struct rx_connection * conn,
189                            struct rx_call * acall), void *arg, void *arg1)
190 #else
191 static struct rxevent *
192 _rxevent_Post(struct clock *when, void (*func) (), void *arg, void *arg1,
193               int arg2, int newargs)
194 #endif
195 {
196     register struct rxevent *ev, *evqe, *evqpr;
197     register struct rxepoch *ep, *epqe, *epqpr;
198     int isEarliest = 0;
199
200     MUTEX_ENTER(&rxevent_lock);
201 #ifdef RXDEBUG
202     if (rx_Log_event) {
203         struct clock now;
204         clock_GetTime(&now);
205         fprintf(rx_Log_event, "%d.%d: rxevent_Post(%d.%d, %lx, %lx, %lx, %d)\n",
206                 (int)now.sec, (int)now.usec, (int)when->sec, (int)when->usec,
207                 (unsigned long)func, (unsigned long)arg,
208                 (unsigned long)arg1, arg2);
209     }
210 #endif
211
212     /* Get a pointer to the epoch for this event, if none is found then
213      * create a new epoch and insert it into the sorted list */
214     for (ep = NULL, queue_ScanBackwards(&rxepoch_queue, epqe, epqpr, rxepoch)) {
215         if (when->sec == epqe->epochSec) {
216             /* already have an structure for this epoch */
217             ep = epqe;
218             if (ep == queue_First(&rxepoch_queue, rxepoch))
219                 isEarliest = 1;
220             break;
221         } else if (when->sec > epqe->epochSec) {
222             /* Create a new epoch and insert after qe */
223             ep = rxepoch_Allocate(when);
224             queue_InsertAfter(epqe, ep);
225             break;
226         }
227     }
228     if (ep == NULL) {
229         /* Create a new epoch and place it at the head of the list */
230         ep = rxepoch_Allocate(when);
231         queue_Prepend(&rxepoch_queue, ep);
232         isEarliest = 1;
233     }
234
235     /* If we're short on free event entries, create a block of new ones and add
236      * them to the free queue */
237     if (queue_IsEmpty(&rxevent_free)) {
238         register int i;
239 #if     defined(AFS_AIX32_ENV) && defined(KERNEL)
240         ev = (struct rxevent *)rxi_Alloc(sizeof(struct rxevent));
241         queue_Append(&rxevent_free, &ev[0]), rxevent_nFree++;
242 #else
243         ev = (struct rxevent *)osi_Alloc(sizeof(struct rxevent) *
244                                          rxevent_allocUnit);
245         xsp = xfreemallocs;
246         xfreemallocs =
247             (struct xfreelist *)osi_Alloc(sizeof(struct xfreelist));
248         xfreemallocs->mem = (void *)ev;
249         xfreemallocs->size = sizeof(struct rxevent) * rxevent_allocUnit;
250         xfreemallocs->next = xsp;
251         for (i = 0; i < rxevent_allocUnit; i++)
252             queue_Append(&rxevent_free, &ev[i]), rxevent_nFree++;
253 #endif
254     }
255
256     /* Grab and initialize a new rxevent structure */
257     ev = queue_First(&rxevent_free, rxevent);
258     queue_Remove(ev);
259     rxevent_nFree--;
260
261     /* Record user defined event state */
262     ev->eventTime = *when;
263     ev->func = func;
264     ev->arg = arg;
265     ev->arg1 = arg1;
266     ev->arg2 = arg2;
267     ev->newargs = newargs;
268     rxevent_nPosted += 1;       /* Rather than ++, to shut high-C up
269                                  *  regarding never-set variables
270                                  */
271
272     /* Insert the event into the sorted list of events for this epoch */
273     for (queue_ScanBackwards(&ep->events, evqe, evqpr, rxevent)) {
274         if (when->usec >= evqe->eventTime.usec) {
275             /* Insert event after evqe */
276             queue_InsertAfter(evqe, ev);
277             MUTEX_EXIT(&rxevent_lock);
278             return ev;
279         }
280     }
281     /* Insert event at head of current epoch */
282     queue_Prepend(&ep->events, ev);
283     if (isEarliest && rxevent_ScheduledEarlierEvent
284         && (!rxevent_raiseScheduled
285             || clock_Lt(&ev->eventTime, &rxevent_nextRaiseEvents))) {
286         rxevent_raiseScheduled = 1;
287         clock_Zero(&rxevent_nextRaiseEvents);
288         MUTEX_EXIT(&rxevent_lock);
289         /* Notify our external scheduler */
290         (*rxevent_ScheduledEarlierEvent) ();
291         MUTEX_ENTER(&rxevent_lock);
292     }
293     MUTEX_EXIT(&rxevent_lock);
294     return ev;
295 }
296
297 struct rxevent *
298 rxevent_Post(struct clock *when, void (*func) (), void *arg, void *arg1)
299 {
300     return _rxevent_Post(when, func, arg, arg1, 0, 0);
301 }
302
303 struct rxevent *
304 rxevent_Post2(struct clock *when, void (*func) (), void *arg, void *arg1,
305               int arg2)
306 {
307     return _rxevent_Post(when, func, arg, arg1, arg2, 1);
308 }
309
310 /* Cancel an event by moving it from the event queue to the free list.
311  * Warning, the event must be on the event queue!  If not, this should core
312  * dump (reference through 0).  This routine should be called using the macro
313  * event_Cancel, which checks for a null event and also nulls the caller's
314  * event pointer after cancelling the event.
315  */
316 #ifdef RX_ENABLE_LOCKS
317 #ifdef RX_REFCOUNT_CHECK
318 int rxevent_Cancel_type = 0;
319 #endif
320 #endif
321
322 void
323 rxevent_Cancel_1(register struct rxevent *ev, register struct rx_call *call,
324                  register int type)
325 {
326 #ifdef RXDEBUG
327     if (rx_Log_event) {
328         struct clock now;
329         clock_GetTime(&now);
330         fprintf(rx_Log_event, "%d.%d: rxevent_Cancel_1(%d.%d, %lx, %lx)\n",
331                 (int)now.sec, (int)now.usec, (int)ev->eventTime.sec,
332                 (int)ev->eventTime.usec, (unsigned long)ev->func,
333                 (unsigned long)ev->arg);
334     }
335 #endif
336     /* Append it to the free list (rather than prepending) to keep the free
337      * list hot so nothing pages out
338      */
339     MUTEX_ENTER(&rxevent_lock);
340     if (!ev) {
341         MUTEX_EXIT(&rxevent_lock);
342         return;
343     }
344 #ifdef RX_ENABLE_LOCKS
345     /* It's possible we're currently processing this event. */
346     if (queue_IsOnQueue(ev)) {
347         queue_MoveAppend(&rxevent_free, ev);
348         rxevent_nPosted--;
349         rxevent_nFree++;
350         if (call) {
351             call->refCount--;
352 #ifdef RX_REFCOUNT_CHECK
353             call->refCDebug[type]--;
354             if (call->refCDebug[type] < 0) {
355                 rxevent_Cancel_type = type;
356                 osi_Panic("rxevent_Cancel: call refCount < 0");
357             }
358 #endif /* RX_REFCOUNT_CHECK */
359         }
360     }
361 #else /* RX_ENABLE_LOCKS */
362     queue_MoveAppend(&rxevent_free, ev);
363     rxevent_nPosted--;
364     rxevent_nFree++;
365 #endif /* RX_ENABLE_LOCKS */
366     MUTEX_EXIT(&rxevent_lock);
367 }
368
369 /* Process all epochs that have expired relative to the current clock time
370  * (which is not re-evaluated unless clock_NewTime has been called).  The
371  * relative time to the next epoch is returned in the output parameter next
372  * and the function returns 1.  If there are is no next epoch, the function
373  * returns 0.
374  */
375 int
376 rxevent_RaiseEvents(struct clock *next)
377 {
378     register struct rxepoch *ep;
379     register struct rxevent *ev;
380     volatile struct clock now;
381
382     MUTEX_ENTER(&rxevent_lock);
383
384     /* Events are sorted by time, so only scan until an event is found that has
385      * not yet timed out */
386
387     clock_Zero(&now);
388     while (queue_IsNotEmpty(&rxepoch_queue)) {
389         ep = queue_First(&rxepoch_queue, rxepoch);
390         if (queue_IsEmpty(&ep->events)) {
391             queue_Remove(ep);
392             queue_Append(&rxepoch_free, ep);
393             rxepoch_nFree++;
394             continue;
395         }
396         do {
397             ev = queue_First(&ep->events, rxevent);
398             if (clock_Lt(&now, &ev->eventTime)) {
399                 clock_GetTime(&now);
400                 if (clock_Lt(&now, &ev->eventTime)) {
401                     *next = rxevent_nextRaiseEvents = ev->eventTime;
402                     rxevent_raiseScheduled = 1;
403                     clock_Sub(next, &now);
404                     MUTEX_EXIT(&rxevent_lock);
405                     return 1;
406                 }
407             }
408             queue_Remove(ev);
409             rxevent_nPosted--;
410             MUTEX_EXIT(&rxevent_lock);
411             if (ev->newargs) {
412                 ev->func(ev, ev->arg, ev->arg1, ev->arg2);
413             } else {
414                 ev->func(ev, ev->arg, ev->arg1);
415             }
416             MUTEX_ENTER(&rxevent_lock);
417             queue_Append(&rxevent_free, ev);
418             rxevent_nFree++;
419         } while (queue_IsNotEmpty(&ep->events));
420     }
421 #ifdef RXDEBUG
422     if (rx_Log_event)
423         fprintf(rx_Log_event, "rxevent_RaiseEvents(%d.%d)\n", (int)now.sec,
424                 (int)now.usec);
425 #endif
426     rxevent_raiseScheduled = 0;
427     MUTEX_EXIT(&rxevent_lock);
428     return 0;
429 }
430
431 void
432 shutdown_rxevent(void)
433 {
434     struct xfreelist *xp, *nxp;
435
436     LOCK_EV_INIT;
437     if (!rxevent_initialized) {
438         UNLOCK_EV_INIT;
439         return;
440     }
441     rxevent_initialized = 0;
442     UNLOCK_EV_INIT;
443     MUTEX_DESTROY(&rxevent_lock);
444 #if     defined(AFS_AIX32_ENV) && defined(KERNEL)
445     /* Everything is freed in afs_osinet.c */
446 #else
447     xp = xfreemallocs;
448     while (xp) {
449         nxp = xp->next;
450         osi_Free((char *)xp->mem, xp->size);
451         osi_Free((char *)xp, sizeof(struct xfreelist));
452         xp = nxp;
453     }
454     xfreemallocs = NULL;
455 #endif
456
457 }