source: rtems/cpukit/posix/src/ualarm.c @ 8fd5081a

4.115
Last change on this file since 8fd5081a was 8fd5081a, checked in by Ralf Corsepius <ralf.corsepius@…>, on Oct 17, 2011 at 12:25:35 PM

2011-10-17 Ralf Corsépius <ralf.corsepius@…>

  • posix/src/ualarm.c: Make _POSIX_signals_Ualarm_TSR static.
  • Property mode set to 100644
File size: 2.5 KB
Line 
1/*
2 *  3.4.1 Schedule Alarm, P1003.1b-1993, p. 79
3 *
4 *  COPYRIGHT (c) 1989-2008.
5 *  On-Line Applications Research Corporation (OAR).
6 *
7 *  The license and distribution terms for this file may be
8 *  found in the file LICENSE in this distribution or at
9 *  http://www.rtems.com/license/LICENSE.
10 *
11 *  $Id$
12 */
13
14#if HAVE_CONFIG_H
15#include "config.h"
16#endif
17
18#include <pthread.h>
19/* #include <errno.h> */
20
21#include <rtems/system.h>
22#include <rtems/posix/pthread.h>
23#include <rtems/posix/psignal.h>
24#include <rtems/posix/time.h>
25
26/*
27 *  _POSIX_signals_Ualarm_TSR
28 */
29
30static void _POSIX_signals_Ualarm_TSR(
31  Objects_Id      id __attribute__((unused)),
32  void           *argument __attribute__((unused))
33)
34{
35  /*
36   * Send a SIGALRM but if there is a problem, ignore it.
37   * It's OK, there isn't a way this should fail.
38   */
39  (void) kill( getpid(), SIGALRM );
40
41  /*
42   * If the reset interval is non-zero, reschedule ourselves.
43   */
44  _Watchdog_Reset( &_POSIX_signals_Ualarm_timer );
45}
46
47useconds_t ualarm(
48  useconds_t useconds,
49  useconds_t interval
50)
51{
52  useconds_t        remaining = 0;
53  Watchdog_Control *the_timer;
54  Watchdog_Interval ticks;
55  struct timespec   tp;
56
57  the_timer = &_POSIX_signals_Ualarm_timer;
58
59  /*
60   *  Initialize the timer used to implement alarm().
61   */
62
63  if ( !the_timer->routine ) {
64    _Watchdog_Initialize( the_timer, _POSIX_signals_Ualarm_TSR, 0, NULL );
65  } else {
66    Watchdog_States state;
67
68    state = _Watchdog_Remove( the_timer );
69    if ( (state == WATCHDOG_ACTIVE) || (state == WATCHDOG_REMOVE_IT) ) {
70      /*
71       *  The stop_time and start_time fields are snapshots of ticks since
72       *  boot.  Since alarm() is dealing in seconds, we must account for
73       *  this.
74       */
75
76      ticks = the_timer->initial;
77      ticks -= (the_timer->stop_time - the_timer->start_time);
78      /* remaining is now in ticks */
79
80      _Timespec_From_ticks( ticks, &tp );
81      remaining  = tp.tv_sec * TOD_MICROSECONDS_PER_SECOND;
82      remaining += tp.tv_nsec / 1000;
83    }
84  }
85
86  /*
87   *  If useconds is non-zero, then the caller wants to schedule
88   *  the alarm repeatedly at that interval.  If the interval is
89   *  less than a single clock tick, then fudge it to a clock tick.
90   */
91  if ( useconds ) {
92    Watchdog_Interval ticks;
93
94    tp.tv_sec = useconds / TOD_MICROSECONDS_PER_SECOND;
95    tp.tv_nsec = (useconds % TOD_MICROSECONDS_PER_SECOND) * 1000;
96    ticks = _Timespec_To_ticks( &tp );
97    if ( ticks == 0 )
98      ticks = 1;
99
100    _Watchdog_Insert_ticks( the_timer, _Timespec_To_ticks( &tp ) );
101  }
102
103  return remaining;
104}
Note: See TracBrowser for help on using the repository browser.