source: rtems/cpukit/posix/src/alarm.c

Last change on this file was 9480815a, checked in by Sebastian Huber <sebastian.huber@…>, on Dec 21, 2017 at 1:36:52 PM

score: Introduce new monotonic clock

Rename PER_CPU_WATCHDOG_MONOTONIC to PER_CPU_WATCHDOG_TICKS. Add new
PER_CPU_WATCHDOG_MONOTONIC which is based on the system uptime (measured
by timecounter).

Close #3264.

  • Property mode set to 100644
File size: 2.2 KB
Line 
1/**
2 * @file
3 *
4 * @brief System Generates Signal for process after realtime seconds elapsed
5 * @ingroup POSIXAPI
6 */
7
8/*
9 *  3.4.1 Schedule Alarm, P1003.1b-1993, p. 79
10 */
11
12/*  COPYRIGHT (c) 1989-2013.
13 *  On-Line Applications Research Corporation (OAR).
14 *
15 *  The license and distribution terms for this file may be
16 *  found in the file LICENSE in this distribution or at
17 *  http://www.rtems.org/license/LICENSE.
18 */
19
20#if HAVE_CONFIG_H
21#include "config.h"
22#endif
23
24#include <unistd.h>
25#include <signal.h>
26
27#include <rtems/score/todimpl.h>
28#include <rtems/score/watchdogimpl.h>
29
30ISR_LOCK_DEFINE( static, _POSIX_signals_Alarm_lock, "POSIX Alarm" )
31
32static void _POSIX_signals_Alarm_TSR( Watchdog_Control *the_watchdog )
33{
34  int status;
35
36  status = kill( getpid(), SIGALRM );
37
38  #if defined(RTEMS_DEBUG)
39    /*
40     *  There is no reason to think this might fail but we should be
41     *  cautious.
42     */
43    _Assert(status == 0);
44  #else
45    (void) status;
46  #endif
47}
48
49static Watchdog_Control _POSIX_signals_Alarm_watchdog = WATCHDOG_INITIALIZER(
50  _POSIX_signals_Alarm_TSR
51);
52
53unsigned int alarm(
54  unsigned int seconds
55)
56{
57  unsigned int      remaining;
58  Watchdog_Control *the_watchdog;
59  ISR_lock_Context  lock_context;
60  ISR_lock_Context  lock_context2;
61  Per_CPU_Control  *cpu;
62  uint64_t          now;
63  uint32_t          ticks_per_second;
64  uint32_t          ticks;
65
66  the_watchdog = &_POSIX_signals_Alarm_watchdog;
67  ticks_per_second = TOD_TICKS_PER_SECOND;
68  ticks = seconds * ticks_per_second;
69
70  _ISR_lock_ISR_disable_and_acquire(
71    &_POSIX_signals_Alarm_lock,
72    &lock_context
73  );
74
75  cpu = _Watchdog_Get_CPU( the_watchdog );
76  _Watchdog_Per_CPU_acquire_critical( cpu, &lock_context2 );
77  now = cpu->Watchdog.ticks;
78
79  remaining = (unsigned long) _Watchdog_Cancel(
80    &cpu->Watchdog.Header[ PER_CPU_WATCHDOG_TICKS ],
81    the_watchdog,
82    now
83  );
84
85  if ( ticks != 0 ) {
86    _Watchdog_Insert(
87      &cpu->Watchdog.Header[ PER_CPU_WATCHDOG_TICKS ],
88      the_watchdog,
89      now + ticks
90    );
91  }
92
93  _Watchdog_Per_CPU_release_critical( cpu, &lock_context2 );
94  _ISR_lock_Release_and_ISR_enable(
95    &_POSIX_signals_Alarm_lock,
96    &lock_context
97  );
98
99  return ( remaining + ticks_per_second - 1 ) / ticks_per_second;
100}
Note: See TracBrowser for help on using the repository browser.