]> code.delx.au - gnu-emacs/blobdiff - src/atimer.c
Fix a comment and delete trailing whitespace.
[gnu-emacs] / src / atimer.c
index 492aa4ec04e9a0eb68ca79269c69e485b2a1324c..308d4688d7b29bff8b3fec81263523ae97a97634 100644 (file)
@@ -1,12 +1,13 @@
 /* Asynchronous timers.
-   Copyright (C) 2000 Free Software Foundation, Inc.
+   Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005,
+                 2006, 2007, 2008, 2009  Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
-GNU Emacs is free software; you can redistribute it and/or modify
+GNU Emacs is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2, or (at your option)
-any later version.
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
 
 GNU Emacs is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -14,18 +15,17 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
-along with GNU Emacs; see the file COPYING.  If not, write to
-the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-Boston, MA 02111-1307, USA.  */
+along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include <config.h>
-#include <lisp.h>
 #include <signal.h>
+#include <stdio.h>
+#include <setjmp.h>
+#include <lisp.h>
 #include <syssignal.h>
 #include <systime.h>
 #include <blockinput.h>
 #include <atimer.h>
-#include <stdio.h>
 
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
@@ -132,12 +132,12 @@ start_atimer (type, time, fn, client_data)
     case ATIMER_ABSOLUTE:
       t->expiration = time;
       break;
-      
+
     case ATIMER_RELATIVE:
       EMACS_GET_TIME (t->expiration);
       EMACS_ADD_TIME (t->expiration, t->expiration, time);
       break;
-      
+
     case ATIMER_CONTINUOUS:
       EMACS_GET_TIME (t->expiration);
       EMACS_ADD_TIME (t->expiration, t->expiration, time);
@@ -151,7 +151,7 @@ start_atimer (type, time, fn, client_data)
 
   /* Arrange for a SIGALRM at the time the next atimer is ripe.  */
   set_alarm ();
-  
+
   return t;
 }
 
@@ -163,14 +163,14 @@ cancel_atimer (timer)
      struct atimer *timer;
 {
   int i;
-  
+
   BLOCK_ATIMERS;
 
   for (i = 0; i < 2; ++i)
     {
       struct atimer *t, *prev;
       struct atimer **list = i ? &stopped_atimers : &atimers;
-      
+
       /* See if TIMER is active or stopped.  */
       for (t = *list, prev = NULL; t && t != timer; prev = t, t = t->next)
        ;
@@ -184,7 +184,7 @@ cancel_atimer (timer)
            prev->next = t->next;
          else
            *list = t->next;
-         
+
          t->next = free_atimers;
          free_atimers = t;
          break;
@@ -209,7 +209,7 @@ append_atimer_lists (list1, list2)
   else
     {
       struct atimer *p;
-      
+
       for (p = list1; p->next; p = p->next)
        ;
       p->next = list2;
@@ -225,13 +225,13 @@ stop_other_atimers (t)
      struct atimer *t;
 {
   BLOCK_ATIMERS;
-  
+
   if (t)
     {
       struct atimer *p, *prev;
-      
+
       /* See if T is active.  */
-      for (p = atimers, prev = 0; p && p != t; p = p->next)
+      for (p = atimers, prev = NULL; p && p != t; prev = p, p = p->next)
        ;
 
       if (p == t)
@@ -246,7 +246,7 @@ stop_other_atimers (t)
        /* T is not active.  Let's handle this like T == 0.  */
        t = NULL;
     }
-  
+
   stopped_atimers = append_atimer_lists (atimers, stopped_atimers);
   atimers = t;
   UNBLOCK_ATIMERS;
@@ -263,18 +263,18 @@ run_all_atimers ()
     {
       struct atimer *t = atimers;
       struct atimer *next;
-      
+
       BLOCK_ATIMERS;
       atimers = stopped_atimers;
       stopped_atimers = NULL;
-      
+
       while (t)
        {
          next = t->next;
          schedule_atimer (t);
          t = next;
        }
-      
+
       UNBLOCK_ATIMERS;
     }
 }
@@ -301,7 +301,7 @@ set_alarm ()
      must reestablish each time.  */
   signal (SIGALRM, alarm_signal_handler);
 #endif /* USG */
-  
+
   if (atimers)
     {
       EMACS_TIME now, time;
@@ -320,7 +320,7 @@ set_alarm ()
          EMACS_SET_SECS (time, 0);
          EMACS_SET_USECS (time, 1000);
        }
-      
+
       bzero (&it, sizeof it);
       it.it_value = time;
       setitimer (ITIMER_REAL, &it, 0);
@@ -350,33 +350,27 @@ schedule_atimer (t)
     prev->next = t;
   else
     atimers = t;
-  
+
   t->next = a;
 }
 
-
-/* Signal handler for SIGALRM.  SIGNO is the signal number, i.e.
-   SIGALRM.  */
-
-SIGTYPE
-alarm_signal_handler (signo)
-     int signo;
+static void
+run_timers ()
 {
   EMACS_TIME now;
-  
+
   EMACS_GET_TIME (now);
-  pending_atimers = 0;
-  
+
   while (atimers
         && (pending_atimers = interrupt_input_blocked) == 0
         && EMACS_TIME_LE (atimers->expiration, now))
     {
       struct atimer *t;
-      
+
       t = atimers;
       atimers = atimers->next;
       t->fn (t);
-      
+
       if (t->type == ATIMER_CONTINUOUS)
        {
          EMACS_ADD_TIME (t->expiration, now, t->interval);
@@ -387,11 +381,41 @@ alarm_signal_handler (signo)
          t->next = free_atimers;
          free_atimers = t;
        }
-      
+
       EMACS_GET_TIME (now);
     }
-  
-  set_alarm ();
+
+  if (! atimers)
+    pending_atimers = 0;
+
+#ifdef SYNC_INPUT
+  if (pending_atimers)
+    pending_signals = 1;
+  else
+    {
+      pending_signals = interrupt_input_pending;
+      set_alarm ();
+    }
+#else
+  if (! pending_atimers)
+    set_alarm ();
+#endif
+}
+
+
+/* Signal handler for SIGALRM.  SIGNO is the signal number, i.e.
+   SIGALRM.  */
+
+SIGTYPE
+alarm_signal_handler (signo)
+     int signo;
+{
+  pending_atimers = 1;
+#ifdef SYNC_INPUT
+  pending_signals = 1;
+#else
+  run_timers ();
+#endif
 }
 
 
@@ -403,7 +427,7 @@ do_pending_atimers ()
   if (pending_atimers)
     {
       BLOCK_ATIMERS;
-      alarm_signal_handler (SIGALRM);
+      run_timers ();
       UNBLOCK_ATIMERS;
     }
 }
@@ -429,7 +453,11 @@ turn_on_atimers (on)
 void
 init_atimer ()
 {
-  free_atimers = atimers = NULL;
+  free_atimers = stopped_atimers = atimers = NULL;
   pending_atimers = 0;
+  /* pending_signals is initialized in init_keyboard.*/
   signal (SIGALRM, alarm_signal_handler);
 }
+
+/* arch-tag: e6308261-eec6-404b-89fb-6e5909518d70
+   (do not change this comment) */