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

Last change on this file was 0a645dad, checked in by Joel Sherrill <joel@…>, on 02/16/22 at 22:31:50

cpukit/posix/src/[a-o]*.c: Change license to BSD-2

Updates #3053.

  • Property mode set to 100644
File size: 3.4 KB
Line 
1/* SPDX-License-Identifier: BSD-2-Clause */
2
3/**
4 * @file
5 *
6 * @ingroup POSIXAPI
7 *
8 * @brief System Generates Signal for process after realtime seconds elapsed
9 */
10
11/*
12 *  3.4.1 Schedule Alarm, P1003.1b-1993, p. 79
13 */
14
15/*  COPYRIGHT (c) 1989-2013.
16 *  On-Line Applications Research Corporation (OAR).
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 *    notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 *    notice, this list of conditions and the following disclaimer in the
25 *    documentation and/or other materials provided with the distribution.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40#ifdef HAVE_CONFIG_H
41#include "config.h"
42#endif
43
44#include <unistd.h>
45#include <signal.h>
46
47#include <rtems/score/todimpl.h>
48#include <rtems/score/watchdogimpl.h>
49
50ISR_LOCK_DEFINE( static, _POSIX_signals_Alarm_lock, "POSIX Alarm" )
51
52static void _POSIX_signals_Alarm_TSR( Watchdog_Control *the_watchdog )
53{
54  int status;
55
56  status = kill( getpid(), SIGALRM );
57
58  #if defined(RTEMS_DEBUG)
59    /*
60     *  There is no reason to think this might fail but we should be
61     *  cautious.
62     */
63    _Assert(status == 0);
64  #else
65    (void) status;
66  #endif
67}
68
69static Watchdog_Control _POSIX_signals_Alarm_watchdog = WATCHDOG_INITIALIZER(
70  _POSIX_signals_Alarm_TSR
71);
72
73unsigned int alarm(
74  unsigned int seconds
75)
76{
77  unsigned int      remaining;
78  Watchdog_Control *the_watchdog;
79  ISR_lock_Context  lock_context;
80  ISR_lock_Context  lock_context2;
81  Per_CPU_Control  *cpu;
82  uint64_t          now;
83  uint32_t          ticks_per_second;
84  uint32_t          ticks;
85
86  the_watchdog = &_POSIX_signals_Alarm_watchdog;
87  ticks_per_second = TOD_TICKS_PER_SECOND;
88  ticks = seconds * ticks_per_second;
89
90  _ISR_lock_ISR_disable_and_acquire(
91    &_POSIX_signals_Alarm_lock,
92    &lock_context
93  );
94
95  cpu = _Watchdog_Get_CPU( the_watchdog );
96  _Watchdog_Per_CPU_acquire_critical( cpu, &lock_context2 );
97  now = cpu->Watchdog.ticks;
98
99  remaining = (unsigned long) _Watchdog_Cancel(
100    &cpu->Watchdog.Header[ PER_CPU_WATCHDOG_TICKS ],
101    the_watchdog,
102    now
103  );
104
105  if ( ticks != 0 ) {
106    _Watchdog_Insert(
107      &cpu->Watchdog.Header[ PER_CPU_WATCHDOG_TICKS ],
108      the_watchdog,
109      now + ticks
110    );
111  }
112
113  _Watchdog_Per_CPU_release_critical( cpu, &lock_context2 );
114  _ISR_lock_Release_and_ISR_enable(
115    &_POSIX_signals_Alarm_lock,
116    &lock_context
117  );
118
119  return ( remaining + ticks_per_second - 1 ) / ticks_per_second;
120}
Note: See TracBrowser for help on using the repository browser.