home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / file / managers / git-4.3 / git-4 / git-4.3.7 / src / xtimer.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-08  |  3.1 KB  |  117 lines

  1. /* xtimer.c -- very simple application timers. */
  2.  
  3. /* Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
  4.  
  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 2, or (at your option)
  8.    any later version.
  9.  
  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU General Public License
  16.    along with this program; if not, write to the Free Software
  17.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. /* Written by Tudor Hulubei and Andrei Pitis.  */
  20.  
  21.  
  22. #ifdef HAVE_CONFIG_H
  23. #include <config.h>
  24. #endif
  25.  
  26. #include <sys/types.h>
  27. #include <signal.h>
  28.  
  29. #ifdef HAVE_UNISTD_H
  30. #include <unistd.h>
  31. #endif /* HAVE_UNISTD_H */
  32.  
  33. #include "xmalloc.h"
  34. #include "xstring.h"
  35. #include "xtimer.h"
  36. #include "misc.h"
  37.  
  38.  
  39. static int xtimers = 0;
  40. static int xtimer_period = 60;
  41. static xtimer_t **xtimer_data = NULL;
  42.  
  43.  
  44. /* Register a new timer.  */
  45.  
  46. int
  47. xtimer_register(xtimer_hook)
  48.     xtimer_function xtimer_hook;
  49. {
  50.     xtimers++;
  51.  
  52.     xtimer_data = (xtimer_t **)xrealloc(xtimer_data,
  53.                                         xtimers * sizeof(xtimer_t*));
  54.     xtimer_data[xtimers - 1] = (xtimer_t *)xmalloc(sizeof(xtimer_t));
  55.     xtimer_data[xtimers] = NULL;
  56.     memset(xtimer_data[xtimers - 1], 0, sizeof(xtimer_t));
  57.     xtimer_data[xtimers - 1]->hook = xtimer_hook;
  58.     return xtimers - 1;
  59. }
  60.  
  61.  
  62. /* Set xtimer flags.  Actually used only to specify the safe-to-call
  63.    status.  */
  64.  
  65. void
  66. xtimer_set_flags(xtimer, flags)
  67.     int xtimer;
  68.     int flags;
  69. {
  70.     xtimer_data[xtimer]->flags = flags;
  71. }
  72.  
  73.  
  74. /* This is the xtimer signal handler.  This function gets called at every
  75.    SIGALRM signal and calls those xtimer hooks that are available at the
  76.    time. The other hooks are ignored, only their statistics being updated.  */
  77.  
  78. RETSIGTYPE
  79. xtimer_signal_handler(signum)
  80.     int signum;
  81. {
  82.     int xtimer;
  83.  
  84.     signal(signum, xtimer_signal_handler);
  85.  
  86.     /* Tell the system to send the alarm signal again after about
  87.        xtimer_period seconds.  I don't want to call time() & localtime()
  88.        here to get the exact amount of time after which the next alarm
  89.        should be scheduled in order to prevent races: localtime() is
  90.        called from some other places too and I don't know if it is
  91.        reentrant.  I have tooked a simpler approach and scheduled the
  92.        next alarm signal after xtimer_period seconds.  */
  93.     alarm(xtimer_period);
  94.  
  95.     for (xtimer = 0; xtimer < xtimers; xtimer++)
  96.     {
  97.         xtimer_data[xtimer]->interrupts++;
  98.  
  99.         if (xtimer_data[xtimer]->flags & XT_SAFE_TO_CALL)
  100.         {
  101.             xtimer_data[xtimer]->hits++;
  102.             (*xtimer_data[xtimer]->hook)(xtimer_data[xtimer], signum);
  103.         }
  104.     }
  105. }
  106.  
  107.  
  108. /* Initialize the xtimer signal handling.  */
  109.  
  110. void
  111. xtimer(status)
  112.     int status;
  113. {
  114.     signal(SIGALRM, status ? xtimer_signal_handler : SIG_IGN);
  115.     alarm(xtimer_period - get_local_time()->tm_sec);
  116. }
  117.