import insight-2000-02-04 snapshot (2nd try)
[external/binutils.git] / sim / ppc / events.c
index 4b26a6a..8384622 100644 (file)
@@ -1,6 +1,6 @@
 /*  This file is part of the program psim.
 
-    Copyright (C) 1994-1995, Andrew Cagney <cagney@highland.com.au>
+    Copyright (C) 1994-1998, Andrew Cagney <cagney@highland.com.au>
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
 #ifndef _EVENTS_C_
 #define _EVENTS_C_
 
-#ifndef STATIC_INLINE_EVENTS
-#define STATIC_INLINE_EVENTS STATIC_INLINE
-#endif
-
-
 #include "basics.h"
 #include "events.h"
 
+#include <signal.h>
+
+#if !defined (SIM_EVENTS_POLL_RATE)
+#define SIM_EVENTS_POLL_RATE 0x1000
+#endif
+
+
 
 /* The event queue maintains a single absolute time using two
    variables.
    
    TIME_OF_EVENT: this holds the time at which the next event is ment
    to occure.  If no next event it will hold the time of the last
-   event.  The first event occures at time 0 - system start.
+   event.
 
    TIME_FROM_EVENT: The current distance from TIME_OF_EVENT.  If an
    event is pending, this will be positive.  If no future event is
    pending this will be negative.  This variable is decremented once
    for each iteration of a clock cycle.
 
+   Initially, the clock is started at time one (1) with TIME_OF_EVENT
+   == 0 and TIME_FROM_EVENT == -1.
+
    Clearly there is a bug in that this code assumes that the absolute
    time counter will never become greater than 2^62. */
 
@@ -55,6 +60,7 @@ struct _event_entry {
 };
 
 struct _event_queue {
+  int processing;
   event_entry *queue;
   event_entry *volatile held;
   event_entry *volatile *volatile held_end;
@@ -63,58 +69,140 @@ struct _event_queue {
 };
 
 
-INLINE_EVENTS event_queue *
+STATIC_INLINE_EVENTS\
+(void)
+sim_events_poll (void *data)
+{
+  event_queue *queue = data;
+  /* just re-schedule in 1000 million ticks time */
+  event_queue_schedule (queue, SIM_EVENTS_POLL_RATE, sim_events_poll, queue);
+  sim_io_poll_quit ();
+}
+
+
+INLINE_EVENTS\
+(event_queue *)
 event_queue_create(void)
 {
   event_queue *new_event_queue = ZALLOC(event_queue);
 
+  new_event_queue->processing = 0;
   new_event_queue->queue = NULL;
   new_event_queue->held = NULL;
   new_event_queue->held_end = &new_event_queue->held;
+
   /* both times are already zero */
   return new_event_queue;
 }
 
 
+INLINE_EVENTS\
+(void)
+event_queue_init(event_queue *queue)
+{
+  event_entry *event;
+
+  /* drain the interrupt queue */
+  {
+#if defined(HAVE_SIGPROCMASK) && defined(SIG_SETMASK)
+    sigset_t old_mask;
+    sigset_t new_mask;
+    sigfillset(&new_mask);
+    /*-LOCK-*/ sigprocmask(SIG_SETMASK, &new_mask, &old_mask);
+#endif
+    event = queue->held;
+    while (event != NULL) {
+      event_entry *dead = event;
+      event = event->next;
+      zfree(dead);
+    }
+    queue->held = NULL;
+    queue->held_end = &queue->held;
+#if defined(HAVE_SIGPROCMASK) && defined(SIG_SETMASK)
+    /*-UNLOCK-*/ sigprocmask(SIG_SETMASK, &old_mask, NULL);
+#endif
+  }
+
+  /* drain the normal queue */
+  event = queue->queue;
+  while (event != NULL) {
+    event_entry *dead = event;
+    event = event->next;
+    zfree(dead);
+  }
+  queue->queue = NULL;
+    
+  /* wind time back to one */
+  queue->processing = 0;
+  queue->time_of_event = 0;
+  queue->time_from_event = -1;
+
+  /* schedule our initial counter event */
+  event_queue_schedule (queue, 0, sim_events_poll, queue);
+}
+
+INLINE_EVENTS\
+(signed64)
+event_queue_time(event_queue *queue)
+{
+  return queue->time_of_event - queue->time_from_event;
+}
+
+STATIC_INLINE_EVENTS\
+(void)
+update_time_from_event(event_queue *events)
+{
+  signed64 current_time = event_queue_time(events);
+  if (events->queue != NULL) {
+    events->time_from_event = (events->queue->time_of_event - current_time);
+    events->time_of_event = events->queue->time_of_event;
+  }
+  else {
+    events->time_of_event = current_time - 1;
+    events->time_from_event = -1;
+  }
+  ASSERT(current_time == event_queue_time(events));
+  ASSERT((events->time_from_event >= 0) == (events->queue != NULL));
+}
 
-STATIC_INLINE_EVENTS void
+STATIC_INLINE_EVENTS\
+(void)
 insert_event_entry(event_queue *events,
                   event_entry *new_event,
                   signed64 delta)
 {
   event_entry *curr;
-  event_entry **last;
+  event_entry **prev;
   signed64 time_of_event;
 
-  if (delta <= 0)
-    error("can not schedule event for current time\n");
+  if (delta < 0)
+    error("what is past is past!\n");
 
   /* compute when the event should occure */
-  time_of_event = (events->time_of_event
-                  - events->time_from_event
-                  + delta);
+  time_of_event = event_queue_time(events) + delta;
 
   /* find the queue insertion point - things are time ordered */
-  last = &events->queue;
+  prev = &events->queue;
   curr = events->queue;
   while (curr != NULL && time_of_event >= curr->time_of_event) {
-    last = &curr->next;
+    ASSERT(curr->next == NULL
+          || curr->time_of_event <= curr->next->time_of_event);
+    prev = &curr->next;
     curr = curr->next;
   }
+  ASSERT(curr == NULL || time_of_event < curr->time_of_event);
 
   /* insert it */
   new_event->next = curr;
-  *last = new_event;
+  *prev = new_event;
   new_event->time_of_event = time_of_event;
 
   /* adjust the time until the first event */
-  events->time_from_event = (events->queue->time_of_event
-                            - (events->time_of_event
-                               - events->time_from_event));
-  events->time_of_event = events->queue->time_of_event;
+  update_time_from_event(events);
 }
 
-INLINE_EVENTS event_entry_tag
+INLINE_EVENTS\
+(event_entry_tag)
 event_queue_schedule(event_queue *events,
                     signed64 delta_time,
                     event_handler *handler,
@@ -124,11 +212,18 @@ event_queue_schedule(event_queue *events,
   new_event->data = data;
   new_event->handler = handler;
   insert_event_entry(events, new_event, delta_time);
-  return new_event;
+  TRACE(trace_events, ("event scheduled at %ld - tag 0x%lx - time %ld, handler 0x%lx, data 0x%lx\n",
+                      (long)event_queue_time(events),
+                      (long)new_event,
+                      (long)new_event->time_of_event,
+                      (long)new_event->handler,
+                      (long)new_event->data));
+  return (event_entry_tag)new_event;
 }
 
 
-INLINE_EVENTS event_entry_tag
+INLINE_EVENTS\
+(event_entry_tag)
 event_queue_schedule_after_signal(event_queue *events,
                                  signed64 delta_time,
                                  event_handler *handler,
@@ -141,54 +236,104 @@ event_queue_schedule_after_signal(event_queue *events,
   new_event->time_of_event = delta_time; /* work it out later */
   new_event->next = NULL;
 
-  /*-LOCK-*/
-  if (events->held == NULL) {
-    events->held = new_event;
-  }
-  else {
-    *events->held_end = new_event;
+  {
+#if defined(HAVE_SIGPROCMASK) && defined(SIG_SETMASK)
+    sigset_t old_mask;
+    sigset_t new_mask;
+    sigfillset(&new_mask);
+    /*-LOCK-*/ sigprocmask(SIG_SETMASK, &new_mask, &old_mask);
+#endif
+    if (events->held == NULL) {
+      events->held = new_event;
+    }
+    else {
+      *events->held_end = new_event;
+    }
+    events->held_end = &new_event->next;
+#if defined(HAVE_SIGPROCMASK) && defined(SIG_SETMASK)
+    /*-UNLOCK-*/ sigprocmask(SIG_SETMASK, &old_mask, NULL);
+#endif
   }
-  events->held_end = &new_event->next;
-  /*-UNLOCK-*/
 
-  return new_event;
+  TRACE(trace_events, ("event scheduled at %ld - tag 0x%lx - time %ld, handler 0x%lx, data 0x%lx\n",
+                      (long)event_queue_time(events),
+                      (long)new_event,
+                      (long)new_event->time_of_event,
+                      (long)new_event->handler,
+                      (long)new_event->data));
+
+  return (event_entry_tag)new_event;
 }
 
 
-INLINE_EVENTS void
+INLINE_EVENTS\
+(void)
 event_queue_deschedule(event_queue *events,
                       event_entry_tag event_to_remove)
 {
+  event_entry *to_remove = (event_entry*)event_to_remove;
+  ASSERT((events->time_from_event >= 0) == (events->queue != NULL));
   if (event_to_remove != NULL) {
     event_entry *current;
     event_entry **ptr_to_current;
     for (ptr_to_current = &events->queue, current = *ptr_to_current;
-        current != NULL && current != event_to_remove;
+        current != NULL && current != to_remove;
         ptr_to_current = &current->next, current = *ptr_to_current);
-    if (current == event_to_remove) {
+    if (current == to_remove) {
       *ptr_to_current = current->next;
+      TRACE(trace_events, ("event descheduled at %ld - tag 0x%lx - time %ld, handler 0x%lx, data 0x%lx\n",
+                          (long)event_queue_time(events),
+                          (long)event_to_remove,
+                          (long)current->time_of_event,
+                          (long)current->handler,
+                          (long)current->data));
       zfree(current);
-      /* Just forget to recompute the delay to the next event */
+      update_time_from_event(events);
+    }
+    else {
+      TRACE(trace_events, ("event descheduled at %ld - tag 0x%lx - not found\n",
+                          (long)event_queue_time(events),
+                          (long)event_to_remove));
     }
   }
+  ASSERT((events->time_from_event >= 0) == (events->queue != NULL));
 }
 
 
 
 
-INLINE_EVENTS int
+INLINE_EVENTS\
+(int)
 event_queue_tick(event_queue *events)
 {
-  /* remove things from the asynchronous event queue onto the real one */
+  signed64 time_from_event;
+
+  /* we should only be here when the previous tick has been fully processed */
+  ASSERT(!events->processing);
+
+  /* move any events that were queued by any signal handlers onto the
+     real event queue.  BTW: When inlining, having this code here,
+     instead of in event_queue_process() causes GCC to put greater
+     weight on keeping the pointer EVENTS in a register.  This, in
+     turn results in better code being output. */
   if (events->held != NULL) {
     event_entry *held_events;
     event_entry *curr_event;
 
-    /*-LOCK-*/
-    held_events = events->held;
-    events->held = NULL;
-    events->held_end = &events->held;
-    /*-UNLOCK-*/
+    {
+#if defined(HAVE_SIGPROCMASK) && defined(SIG_SETMASK)
+      sigset_t old_mask;
+      sigset_t new_mask;
+      sigfillset(&new_mask);
+      /*-LOCK-*/ sigprocmask(SIG_SETMASK, &new_mask, &old_mask);
+#endif
+      held_events = events->held;
+      events->held = NULL;
+      events->held_end = &events->held;
+#if defined(HAVE_SIGPROCMASK) && defined(SIG_SETMASK)
+      /*-UNLOCK-*/ sigprocmask(SIG_SETMASK, &old_mask, NULL);
+#endif
+    }
 
     do {
       curr_event = held_events;
@@ -199,39 +344,43 @@ event_queue_tick(event_queue *events)
 
   /* advance time, checking to see if we've reached time zero which
      would indicate the time for the next event has arrived */
-  events->time_from_event -= 1;
-  return events->time_from_event == 0;
+  time_from_event = events->time_from_event;
+  events->time_from_event = time_from_event - 1;
+  return time_from_event == 0;
 }
 
-INLINE_EVENTS void
+
+
+INLINE_EVENTS\
+(void)
 event_queue_process(event_queue *events)
 {
-  if (events->time_from_event == 0) {
-    /* consume all events for this or earlier times */
-    do {
-      event_entry *to_do = events->queue;
-      events->queue = to_do->next;
-      to_do->handler(events,
-                    to_do->data);
-      zfree(to_do);
-    } while (events->queue != NULL
-            && events->queue->time_of_event <= events->time_of_event);
-    /* re-caculate time for new events */
-    if (events->queue != NULL) {
-      events->time_from_event = (events->queue->time_of_event
-                                - events->time_of_event);
-      events->time_of_event = events->queue->time_of_event;
-    }
-    else {
-      /* nothing to do, time_from_event will go negative */
-    }
+  signed64 event_time = event_queue_time(events);
+
+  ASSERT((events->time_from_event == -1 && events->queue != NULL)
+        || events->processing); /* something to do */
+
+  /* consume all events for this or earlier times.  Be careful to
+     allow a new event to appear under our feet */
+  events->processing = 1;
+  while (events->queue != NULL
+        && events->queue->time_of_event <= event_time) {
+    event_entry *to_do = events->queue;
+    event_handler *handler = to_do->handler;
+    void *data = to_do->data;
+    events->queue = to_do->next;
+    TRACE(trace_events, ("event issued at %ld - tag 0x%lx - handler 0x%lx, data 0x%lx\n",
+                        (long)event_time,
+                        (long)to_do,
+                        (long)handler,
+                        (long)data));
+    zfree(to_do);
+    handler(data);
   }
-}
+  events->processing = 0;
 
-INLINE_EVENTS signed64
-event_queue_time(event_queue *queue)
-{
-  return queue->time_of_event - queue->time_from_event;
+  /* re-caculate time for new events */
+  update_time_from_event(events);
 }