Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:21

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @ingroup POSIXAPI
0007  *
0008  * @brief System Generates Signal for process after realtime seconds elapsed
0009  */
0010 
0011 /*
0012  *  3.4.1 Schedule Alarm, P1003.1b-1993, p. 79
0013  */
0014 
0015 /*  COPYRIGHT (c) 1989-2013.
0016  *  On-Line Applications Research Corporation (OAR).
0017  *
0018  * Redistribution and use in source and binary forms, with or without
0019  * modification, are permitted provided that the following conditions
0020  * are met:
0021  * 1. Redistributions of source code must retain the above copyright
0022  *    notice, this list of conditions and the following disclaimer.
0023  * 2. Redistributions in binary form must reproduce the above copyright
0024  *    notice, this list of conditions and the following disclaimer in the
0025  *    documentation and/or other materials provided with the distribution.
0026  *
0027  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0028  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0029  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0030  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0031  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0032  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0033  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0034  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0035  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0036  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0037  * POSSIBILITY OF SUCH DAMAGE.
0038  */
0039 
0040 #ifdef HAVE_CONFIG_H
0041 #include "config.h"
0042 #endif
0043 
0044 #include <unistd.h>
0045 #include <signal.h>
0046 
0047 #include <rtems/score/todimpl.h>
0048 #include <rtems/score/watchdogimpl.h>
0049 
0050 #if ISR_LOCK_NEEDS_OBJECT
0051 static ISR_lock_Control _POSIX_signals_Alarm_lock =
0052   ISR_LOCK_INITIALIZER( "POSIX Alarm" );
0053 #endif
0054 
0055 static void _POSIX_signals_Alarm_TSR( Watchdog_Control *the_watchdog )
0056 {
0057   int status;
0058 
0059   status = kill( getpid(), SIGALRM );
0060 
0061   #if defined(RTEMS_DEBUG)
0062     /*
0063      *  There is no reason to think this might fail but we should be
0064      *  cautious.
0065      */
0066     _Assert(status == 0);
0067   #else
0068     (void) status;
0069   #endif
0070 }
0071 
0072 static Watchdog_Control _POSIX_signals_Alarm_watchdog = WATCHDOG_INITIALIZER(
0073   _POSIX_signals_Alarm_TSR
0074 );
0075 
0076 unsigned int alarm(
0077   unsigned int seconds
0078 )
0079 {
0080   unsigned int      remaining;
0081   Watchdog_Control *the_watchdog;
0082   ISR_lock_Context  lock_context;
0083   ISR_lock_Context  lock_context2;
0084   Per_CPU_Control  *cpu;
0085   uint64_t          now;
0086   uint32_t          ticks_per_second;
0087   uint32_t          ticks;
0088 
0089   the_watchdog = &_POSIX_signals_Alarm_watchdog;
0090   ticks_per_second = TOD_TICKS_PER_SECOND;
0091   ticks = seconds * ticks_per_second;
0092 
0093   _ISR_lock_ISR_disable_and_acquire(
0094     &_POSIX_signals_Alarm_lock,
0095     &lock_context
0096   );
0097 
0098   cpu = _Watchdog_Get_CPU( the_watchdog );
0099   _Watchdog_Per_CPU_acquire_critical( cpu, &lock_context2 );
0100   now = cpu->Watchdog.ticks;
0101 
0102   remaining = (unsigned long) _Watchdog_Cancel(
0103     &cpu->Watchdog.Header[ PER_CPU_WATCHDOG_TICKS ],
0104     the_watchdog,
0105     now
0106   );
0107 
0108   if ( ticks != 0 ) {
0109     _Watchdog_Insert(
0110       &cpu->Watchdog.Header[ PER_CPU_WATCHDOG_TICKS ],
0111       the_watchdog,
0112       now + ticks
0113     );
0114   }
0115 
0116   _Watchdog_Per_CPU_release_critical( cpu, &lock_context2 );
0117   _ISR_lock_Release_and_ISR_enable(
0118     &_POSIX_signals_Alarm_lock,
0119     &lock_context
0120   );
0121 
0122   return ( remaining + ticks_per_second - 1 ) / ticks_per_second;
0123 }