2008-01-31 18:59:22 +01:00
|
|
|
/****************************************************************************
|
2014-08-08 20:55:02 +02:00
|
|
|
* sched/signal/sig_timedwait.c
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2024-09-11 13:45:11 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2021-02-08 16:33:58 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2021-02-08 16:33:58 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2021-02-08 16:33:58 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2008-01-31 18:59:22 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-01-31 18:59:22 +01:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Included Files
|
2008-01-31 18:59:22 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2009-12-14 19:39:29 +01:00
|
|
|
#include <nuttx/config.h>
|
2012-12-24 18:49:58 +01:00
|
|
|
#include <nuttx/compiler.h>
|
2009-12-14 19:39:29 +01:00
|
|
|
|
|
|
|
#include <stdint.h>
|
2007-02-21 22:55:16 +01:00
|
|
|
#include <string.h>
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <signal.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <debug.h>
|
|
|
|
#include <sched.h>
|
2012-05-30 17:36:46 +02:00
|
|
|
#include <errno.h>
|
|
|
|
|
2016-02-14 15:17:46 +01:00
|
|
|
#include <nuttx/irq.h>
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <nuttx/arch.h>
|
2014-08-21 19:16:55 +02:00
|
|
|
#include <nuttx/wdog.h>
|
2017-10-06 16:28:20 +02:00
|
|
|
#include <nuttx/signal.h>
|
2016-12-10 16:08:26 +01:00
|
|
|
#include <nuttx/cancelpt.h>
|
2022-11-01 02:52:11 +01:00
|
|
|
#include <nuttx/queue.h>
|
2012-05-30 17:36:46 +02:00
|
|
|
|
2014-08-09 01:53:55 +02:00
|
|
|
#include "sched/sched.h"
|
2014-08-08 20:44:44 +02:00
|
|
|
#include "signal/signal.h"
|
2014-08-08 22:43:02 +02:00
|
|
|
#include "clock/clock.h"
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-01-31 18:59:22 +01:00
|
|
|
/****************************************************************************
|
2012-05-30 17:36:46 +02:00
|
|
|
* Pre-processor Definitions
|
2008-01-31 18:59:22 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2017-10-12 16:55:19 +02:00
|
|
|
/* These are special values of si_signo that mean that either the wait was
|
|
|
|
* awakened with a timeout, or the wait was canceled... not the receipt of a
|
|
|
|
* signal.
|
2012-05-30 17:36:46 +02:00
|
|
|
*/
|
|
|
|
|
2017-10-12 16:55:19 +02:00
|
|
|
#define SIG_CANCEL_TIMEOUT 0xfe
|
|
|
|
#define SIG_WAIT_TIMEOUT 0xff
|
2012-05-30 17:36:46 +02:00
|
|
|
|
2008-01-31 18:59:22 +01:00
|
|
|
/****************************************************************************
|
2014-04-25 20:38:56 +02:00
|
|
|
* Private Functions
|
2008-01-31 18:59:22 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-01-31 18:59:22 +01:00
|
|
|
/****************************************************************************
|
This change renames all internal, private NuttX signal-related functions to use the prefix nxsig_ so that they cannot be confused with application interfaces that begin, primarily, with sig_
This is analogous to similar renaming that was done previously for semaphores.
Squashed commit of the following:
sched/signal: Fix a few compile warnings introduced by naming changes.
sched/signal: Rename all private, internal signl functions to use the nxsig_ prefix.
sched/signal: Rename sig_removependingsignal, sig_unmaskpendingsignal, and sig_mqnotempty to nxsig_remove_pendingsignal, nxsig_unmask_pendingsignal, and nxsig_mqnotempty to make it clear that these are OS internal interfaces.
sched/signal: Rename sig_findaction and sig_lowest to nxsig_find_action and nxsig_lowest to make it clear that these are OS internal interfaces.
sched/signal: Rename sig_allocatepingsigaction and sig_deliver to nxsig_alloc_pendingsigaction and nxsig_deliver to make it clear that these are OS internal interfaces.
sched/signal: Rename sig_cleanup, sig_release, sig_releasependingaction, and sig_releasependingsignal to nxsig_cleanup, nxsig_release, nxsig_release_pendingaction, and nxsig_release_pendingsignal to make it clear that these are OS internal interfaces.
sched/signal: Rename sig_tcbdispatch and sig_dispatch to nxsig_tcbdispatch and nxsig_dispatch to make it clear that these are OS internal interfaces.
sched/signal: Rename sig_releaseaction and sig_pendingset to nxsig_release_action and nxsig_pendingset to make it clear that these are OS internal interfaces.
sched/signal: Rename sig_initialize and sig_allocateactionblock to nxsig_initialize and nxsig_alloc_actionblock to make it clear that these are OS internal interfaces.
2017-10-05 21:25:25 +02:00
|
|
|
* Name: nxsig_timeout
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2017-02-24 17:07:23 +01:00
|
|
|
* A timeout elapsed while waiting for signals to be queued.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* This function executes in the context of the timer interrupt handler.
|
|
|
|
* Local interrupts are assumed to be disabled on entry.
|
2008-01-31 18:59:22 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2020-08-09 20:29:35 +02:00
|
|
|
static void nxsig_timeout(wdparm_t arg)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2020-08-09 20:29:35 +02:00
|
|
|
FAR struct tcb_s *wtcb = (FAR struct tcb_s *)(uintptr_t)arg;
|
2017-02-24 17:07:23 +01:00
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
irqstate_t flags;
|
2007-02-21 22:55:16 +01:00
|
|
|
|
2022-11-15 09:52:40 +01:00
|
|
|
/* We must be in a critical section in order to call up_switch_context()
|
2017-02-24 17:07:23 +01:00
|
|
|
* below. If we are running on a single CPU architecture, then we know
|
|
|
|
* interrupts a disabled an there is no need to explicitly call
|
|
|
|
* enter_critical_section(). However, in the SMP case,
|
|
|
|
* enter_critical_section() does much more than just disable interrupts on
|
|
|
|
* the local CPU; it also manages spinlocks to assure the stability of the
|
|
|
|
* TCB that we are manipulating.
|
|
|
|
*/
|
|
|
|
|
|
|
|
flags = enter_critical_section();
|
|
|
|
#endif
|
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
/* There may be a race condition -- make sure the task is
|
|
|
|
* still waiting for a signal
|
|
|
|
*/
|
|
|
|
|
2020-08-09 20:29:35 +02:00
|
|
|
if (wtcb->task_state == TSTATE_WAIT_SIG)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2022-11-01 02:52:11 +01:00
|
|
|
FAR struct tcb_s *rtcb = this_task();
|
|
|
|
|
2024-03-26 02:15:32 +01:00
|
|
|
if (wtcb->sigunbinfo != NULL)
|
|
|
|
{
|
|
|
|
wtcb->sigunbinfo->si_signo = SIG_WAIT_TIMEOUT;
|
|
|
|
wtcb->sigunbinfo->si_code = SI_TIMER;
|
|
|
|
wtcb->sigunbinfo->si_errno = ETIMEDOUT;
|
|
|
|
wtcb->sigunbinfo->si_value.sival_int = 0;
|
2013-01-12 20:58:45 +01:00
|
|
|
#ifdef CONFIG_SCHED_HAVE_PARENT
|
2024-03-26 02:15:32 +01:00
|
|
|
wtcb->sigunbinfo->si_pid = 0; /* Not applicable */
|
|
|
|
wtcb->sigunbinfo->si_status = OK;
|
2013-01-12 20:58:45 +01:00
|
|
|
#endif
|
2024-03-26 02:15:32 +01:00
|
|
|
}
|
2022-11-01 02:52:11 +01:00
|
|
|
|
|
|
|
/* Remove the task from waitting list */
|
|
|
|
|
2024-03-19 05:33:08 +01:00
|
|
|
dq_rem((FAR dq_entry_t *)wtcb, list_waitingforsignal());
|
2022-11-01 02:52:11 +01:00
|
|
|
|
|
|
|
/* Add the task to ready-to-run task list, and
|
|
|
|
* perform the context switch if one is needed
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (nxsched_add_readytorun(wtcb))
|
|
|
|
{
|
2022-11-15 09:52:40 +01:00
|
|
|
up_switch_context(wtcb, rtcb);
|
2022-11-01 02:52:11 +01:00
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
2017-02-24 17:07:23 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
leave_critical_section(flags);
|
|
|
|
#endif
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
2008-01-31 18:59:22 +01:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Public Functions
|
2008-01-31 18:59:22 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2017-10-12 16:55:19 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxsig_wait_irq
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* An error event has occurred and the signal wait must be terminated with
|
|
|
|
* an error.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_CANCELLATION_POINTS
|
|
|
|
void nxsig_wait_irq(FAR struct tcb_s *wtcb, int errcode)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
irqstate_t flags;
|
|
|
|
|
2022-11-15 09:52:40 +01:00
|
|
|
/* We must be in a critical section in order to call up_switch_context()
|
2017-10-12 16:55:19 +02:00
|
|
|
* below. If we are running on a single CPU architecture, then we know
|
|
|
|
* interrupts a disabled an there is no need to explicitly call
|
|
|
|
* enter_critical_section(). However, in the SMP case,
|
|
|
|
* enter_critical_section() does much more than just disable interrupts on
|
|
|
|
* the local CPU; it also manages spinlocks to assure the stability of the
|
|
|
|
* TCB that we are manipulating.
|
|
|
|
*/
|
|
|
|
|
|
|
|
flags = enter_critical_section();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* There may be a race condition -- make sure the task is
|
|
|
|
* still waiting for a signal
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (wtcb->task_state == TSTATE_WAIT_SIG)
|
|
|
|
{
|
2022-11-01 02:52:11 +01:00
|
|
|
FAR struct tcb_s *rtcb = this_task();
|
|
|
|
|
2024-03-26 02:15:32 +01:00
|
|
|
if (wtcb->sigunbinfo != NULL)
|
|
|
|
{
|
|
|
|
wtcb->sigunbinfo->si_signo = SIG_CANCEL_TIMEOUT;
|
|
|
|
wtcb->sigunbinfo->si_code = SI_USER;
|
|
|
|
wtcb->sigunbinfo->si_errno = errcode;
|
|
|
|
wtcb->sigunbinfo->si_value.sival_int = 0;
|
2017-10-12 16:55:19 +02:00
|
|
|
#ifdef CONFIG_SCHED_HAVE_PARENT
|
2024-03-26 02:15:32 +01:00
|
|
|
wtcb->sigunbinfo->si_pid = 0; /* Not applicable */
|
|
|
|
wtcb->sigunbinfo->si_status = OK;
|
2017-10-12 16:55:19 +02:00
|
|
|
#endif
|
2024-03-26 02:15:32 +01:00
|
|
|
}
|
2022-11-01 02:52:11 +01:00
|
|
|
|
|
|
|
/* Remove the task from waitting list */
|
|
|
|
|
2024-03-19 05:33:08 +01:00
|
|
|
dq_rem((FAR dq_entry_t *)wtcb, list_waitingforsignal());
|
2022-11-01 02:52:11 +01:00
|
|
|
|
|
|
|
/* Add the task to ready-to-run task list, and
|
|
|
|
* perform the context switch if one is needed
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (nxsched_add_readytorun(wtcb))
|
|
|
|
{
|
2022-11-15 09:52:40 +01:00
|
|
|
up_switch_context(wtcb, rtcb);
|
2022-11-01 02:52:11 +01:00
|
|
|
}
|
2017-10-12 16:55:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
leave_critical_section(flags);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_CANCELLATION_POINTS */
|
|
|
|
|
2008-01-31 18:59:22 +01:00
|
|
|
/****************************************************************************
|
2017-10-06 16:28:20 +02:00
|
|
|
* Name: nxsig_timedwait
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2008-01-31 18:59:22 +01:00
|
|
|
* This function selects the pending signal set specified by the argument
|
|
|
|
* set. If multiple signals are pending in set, it will remove and return
|
|
|
|
* the lowest numbered one. If no signals in set are pending at the time
|
|
|
|
* of the call, the calling process will be suspended until one of the
|
|
|
|
* signals in set becomes pending, OR until the process is interrupted by
|
|
|
|
* an unblocked signal, OR until the time interval specified by timeout
|
|
|
|
* (if any), has expired. If timeout is NULL, then the timeout interval
|
|
|
|
* is forever.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2008-01-31 18:59:22 +01:00
|
|
|
* If the info argument is non-NULL, the selected signal number is stored
|
2020-11-22 22:47:10 +01:00
|
|
|
* in the si_signo member and the cause of the signal is stored in the
|
2014-04-25 20:38:56 +02:00
|
|
|
* si_code member. The content of si_value is only meaningful if the
|
2017-10-07 18:57:09 +02:00
|
|
|
* signal was generated by sigqueue() (or nxsig_queue).
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2017-10-06 16:28:20 +02:00
|
|
|
* This is an internal OS interface. It is functionally equivalent to
|
|
|
|
* sigtimedwait() except that:
|
|
|
|
*
|
2019-08-23 19:57:35 +02:00
|
|
|
* - It is not a cancellation point, and
|
2017-10-06 16:28:20 +02:00
|
|
|
* - It does not modify the errno value.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-03-02 13:39:05 +01:00
|
|
|
* set - The pending signal set.
|
|
|
|
* info - The returned value (may be NULL).
|
|
|
|
* timeout - The amount of time to wait (may be NULL)
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2017-10-06 16:28:20 +02:00
|
|
|
* This is an internal OS interface and should not be used by applications.
|
|
|
|
* It follows the NuttX internal error return policy: Zero (OK) is
|
|
|
|
* returned on success. A negated errno value is returned on failure.
|
2012-05-30 17:36:46 +02:00
|
|
|
*
|
|
|
|
* EAGAIN - No signal specified by set was generated within the specified
|
|
|
|
* timeout period.
|
|
|
|
* EINTR - The wait was interrupted by an unblocked, caught signal.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2008-01-31 18:59:22 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2017-10-06 16:28:20 +02:00
|
|
|
int nxsig_timedwait(FAR const sigset_t *set, FAR struct siginfo *info,
|
|
|
|
FAR const struct timespec *timeout)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2016-02-07 00:44:41 +01:00
|
|
|
FAR struct tcb_s *rtcb = this_task();
|
2013-02-04 19:46:28 +01:00
|
|
|
sigset_t intersection;
|
2007-02-27 22:17:21 +01:00
|
|
|
FAR sigpendq_t *sigpend;
|
2016-02-14 15:17:46 +01:00
|
|
|
irqstate_t flags;
|
2023-05-04 09:17:17 +02:00
|
|
|
sclock_t waitticks;
|
2024-08-14 05:52:59 +02:00
|
|
|
siginfo_t unbinfo;
|
2017-10-06 16:28:20 +02:00
|
|
|
int ret;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2020-08-04 12:31:31 +02:00
|
|
|
DEBUGASSERT(set != NULL);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* Several operations must be performed below: We must determine if any
|
|
|
|
* signal is pending and, if not, wait for the signal. Since signals can
|
|
|
|
* be posted from the interrupt level, there is a race condition that
|
|
|
|
* can only be eliminated by disabling interrupts!
|
|
|
|
*/
|
|
|
|
|
2016-02-14 15:17:46 +01:00
|
|
|
flags = enter_critical_section();
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* Check if there is a pending signal corresponding to one of the
|
|
|
|
* signals in the pending signal set argument.
|
|
|
|
*/
|
|
|
|
|
2023-03-23 21:36:14 +01:00
|
|
|
intersection = nxsig_pendingset(rtcb);
|
2023-07-05 05:55:21 +02:00
|
|
|
sigandset(&intersection, &intersection, set);
|
2023-03-23 21:36:14 +01:00
|
|
|
if (!sigisemptyset(&intersection))
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
|
|
|
/* One or more of the signals in intersections is sufficient to cause
|
|
|
|
* us to not wait. Pick the lowest numbered signal and mark it not
|
|
|
|
* pending.
|
|
|
|
*/
|
|
|
|
|
2020-03-10 05:50:49 +01:00
|
|
|
sigpend = nxsig_remove_pendingsignal(rtcb,
|
|
|
|
nxsig_lowest(&intersection));
|
2017-10-06 16:28:20 +02:00
|
|
|
DEBUGASSERT(sigpend);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* Return the signal info to the caller if so requested */
|
|
|
|
|
2017-03-02 13:39:05 +01:00
|
|
|
if (info != NULL)
|
2007-02-21 22:55:16 +01:00
|
|
|
{
|
|
|
|
memcpy(info, &sigpend->info, sizeof(struct siginfo));
|
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2017-07-27 20:32:47 +02:00
|
|
|
/* The return value is the number of the signal that awakened us */
|
|
|
|
|
|
|
|
ret = sigpend->info.si_signo;
|
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
/* Then dispose of the pending signal structure properly */
|
|
|
|
|
This change renames all internal, private NuttX signal-related functions to use the prefix nxsig_ so that they cannot be confused with application interfaces that begin, primarily, with sig_
This is analogous to similar renaming that was done previously for semaphores.
Squashed commit of the following:
sched/signal: Fix a few compile warnings introduced by naming changes.
sched/signal: Rename all private, internal signl functions to use the nxsig_ prefix.
sched/signal: Rename sig_removependingsignal, sig_unmaskpendingsignal, and sig_mqnotempty to nxsig_remove_pendingsignal, nxsig_unmask_pendingsignal, and nxsig_mqnotempty to make it clear that these are OS internal interfaces.
sched/signal: Rename sig_findaction and sig_lowest to nxsig_find_action and nxsig_lowest to make it clear that these are OS internal interfaces.
sched/signal: Rename sig_allocatepingsigaction and sig_deliver to nxsig_alloc_pendingsigaction and nxsig_deliver to make it clear that these are OS internal interfaces.
sched/signal: Rename sig_cleanup, sig_release, sig_releasependingaction, and sig_releasependingsignal to nxsig_cleanup, nxsig_release, nxsig_release_pendingaction, and nxsig_release_pendingsignal to make it clear that these are OS internal interfaces.
sched/signal: Rename sig_tcbdispatch and sig_dispatch to nxsig_tcbdispatch and nxsig_dispatch to make it clear that these are OS internal interfaces.
sched/signal: Rename sig_releaseaction and sig_pendingset to nxsig_release_action and nxsig_pendingset to make it clear that these are OS internal interfaces.
sched/signal: Rename sig_initialize and sig_allocateactionblock to nxsig_initialize and nxsig_alloc_actionblock to make it clear that these are OS internal interfaces.
2017-10-05 21:25:25 +02:00
|
|
|
nxsig_release_pendingsignal(sigpend);
|
2016-02-14 15:17:46 +01:00
|
|
|
leave_critical_section(flags);
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We will have to wait for a signal to be posted to this task. */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
2017-10-12 17:53:17 +02:00
|
|
|
#ifdef CONFIG_CANCELLATION_POINTS
|
|
|
|
/* nxsig_timedwait() is not a cancellation point, but it may be called
|
|
|
|
* from a cancellation point. So if a cancellation is pending, we
|
|
|
|
* must exit immediately without waiting.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (check_cancellation_point())
|
|
|
|
{
|
|
|
|
/* If there is a pending cancellation, then do not perform
|
|
|
|
* the wait. Exit now with ECANCELED.
|
|
|
|
*/
|
|
|
|
|
|
|
|
leave_critical_section(flags);
|
|
|
|
return -ECANCELED;
|
2019-06-30 18:35:10 +02:00
|
|
|
}
|
2017-10-12 17:53:17 +02:00
|
|
|
#endif
|
2019-10-24 18:49:28 +02:00
|
|
|
|
2024-08-14 05:52:59 +02:00
|
|
|
rtcb->sigunbinfo = (info == NULL) ? &unbinfo : info;
|
2024-03-14 10:35:30 +01:00
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
/* Check if we should wait for the timeout */
|
|
|
|
|
2017-03-02 13:39:05 +01:00
|
|
|
if (timeout != NULL)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2012-12-24 15:31:02 +01:00
|
|
|
/* Convert the timespec to system clock ticks, making sure that
|
2014-04-25 20:38:56 +02:00
|
|
|
* the resulting delay is greater than or equal to the requested
|
2012-12-24 15:31:02 +01:00
|
|
|
* time in nanoseconds.
|
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2023-03-10 05:29:35 +01:00
|
|
|
#ifdef CONFIG_SYSTEM_TIME64
|
|
|
|
waitticks = ((uint64_t)timeout->tv_sec * NSEC_PER_SEC +
|
|
|
|
(uint64_t)timeout->tv_nsec + NSEC_PER_TICK - 1) /
|
|
|
|
NSEC_PER_TICK;
|
2012-12-24 15:31:02 +01:00
|
|
|
#else
|
|
|
|
uint32_t waitmsec;
|
|
|
|
|
|
|
|
DEBUGASSERT(timeout->tv_sec < UINT32_MAX / MSEC_PER_SEC);
|
|
|
|
waitmsec = timeout->tv_sec * MSEC_PER_SEC +
|
2017-08-15 01:19:27 +02:00
|
|
|
(timeout->tv_nsec + NSEC_PER_MSEC - 1) / NSEC_PER_MSEC;
|
2014-08-08 02:00:38 +02:00
|
|
|
waitticks = MSEC2TICK(waitmsec);
|
2012-12-24 15:31:02 +01:00
|
|
|
#endif
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2022-11-12 23:50:02 +01:00
|
|
|
if (waitticks > 0)
|
|
|
|
{
|
|
|
|
/* Save the set of pending signals to wait for */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2022-11-12 23:50:02 +01:00
|
|
|
rtcb->sigwaitmask = *set;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2022-11-12 23:50:02 +01:00
|
|
|
/* Start the watchdog */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2022-11-12 23:50:02 +01:00
|
|
|
wd_start(&rtcb->waitdog, waitticks,
|
|
|
|
nxsig_timeout, (uintptr_t)rtcb);
|
|
|
|
|
|
|
|
/* Now wait for either the signal or the watchdog, but
|
|
|
|
* first, make sure this is not the idle task,
|
|
|
|
* descheduling that isn't going to end well.
|
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2022-11-12 23:50:02 +01:00
|
|
|
DEBUGASSERT(!is_idle_task(rtcb));
|
2022-11-13 13:39:14 +01:00
|
|
|
|
|
|
|
/* Remove the tcb task from the ready-to-run list. */
|
|
|
|
|
2024-01-19 13:57:04 +01:00
|
|
|
nxsched_remove_self(rtcb);
|
2022-11-13 13:39:14 +01:00
|
|
|
|
|
|
|
/* Add the task to the specified blocked task list */
|
|
|
|
|
|
|
|
rtcb->task_state = TSTATE_WAIT_SIG;
|
2024-03-19 05:33:08 +01:00
|
|
|
dq_addlast((FAR dq_entry_t *)rtcb, list_waitingforsignal());
|
2022-11-13 13:39:14 +01:00
|
|
|
|
|
|
|
/* Now, perform the context switch if one is needed */
|
|
|
|
|
2024-01-19 13:57:04 +01:00
|
|
|
up_switch_context(this_task(), rtcb);
|
2014-01-23 00:14:10 +01:00
|
|
|
|
2022-11-12 23:50:02 +01:00
|
|
|
/* We no longer need the watchdog */
|
|
|
|
|
|
|
|
wd_cancel(&rtcb->waitdog);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-03-26 02:15:32 +01:00
|
|
|
rtcb->sigunbinfo = NULL;
|
|
|
|
|
2022-11-12 23:50:02 +01:00
|
|
|
leave_critical_section(flags);
|
|
|
|
return -EAGAIN;
|
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* No timeout, just wait */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
2022-11-12 23:50:02 +01:00
|
|
|
/* Save the set of pending signals to wait for */
|
|
|
|
|
|
|
|
rtcb->sigwaitmask = *set;
|
|
|
|
|
2018-11-30 13:54:15 +01:00
|
|
|
/* And wait until one of the unblocked signals is posted,
|
|
|
|
* but first make sure this is not the idle task,
|
|
|
|
* descheduling that isn't going to end well.
|
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2022-10-28 08:22:17 +02:00
|
|
|
DEBUGASSERT(!is_idle_task(rtcb));
|
2022-11-13 13:39:14 +01:00
|
|
|
|
2024-01-19 13:57:04 +01:00
|
|
|
/* Remove the tcb task from the running list. */
|
2022-11-13 13:39:14 +01:00
|
|
|
|
2024-01-19 13:57:04 +01:00
|
|
|
nxsched_remove_self(rtcb);
|
2022-11-13 13:39:14 +01:00
|
|
|
|
|
|
|
/* Add the task to the specified blocked task list */
|
|
|
|
|
|
|
|
rtcb->task_state = TSTATE_WAIT_SIG;
|
2024-03-19 05:33:08 +01:00
|
|
|
dq_addlast((FAR dq_entry_t *)rtcb, list_waitingforsignal());
|
2022-11-13 13:39:14 +01:00
|
|
|
|
2024-01-19 13:57:04 +01:00
|
|
|
/* Now, perform the context switch */
|
2022-11-13 13:39:14 +01:00
|
|
|
|
2024-01-19 13:57:04 +01:00
|
|
|
up_switch_context(this_task(), rtcb);
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We are running again, clear the sigwaitmask */
|
|
|
|
|
2023-03-23 21:36:14 +01:00
|
|
|
sigemptyset(&rtcb->sigwaitmask);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2012-05-30 17:36:46 +02:00
|
|
|
/* When we awaken, the cause will be in the TCB. Get the signal number
|
|
|
|
* or timeout) that awakened us.
|
2007-02-18 00:21:28 +01:00
|
|
|
*/
|
|
|
|
|
2024-03-14 10:35:30 +01:00
|
|
|
if (GOOD_SIGNO(rtcb->sigunbinfo->si_signo))
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2020-03-10 05:50:49 +01:00
|
|
|
/* We were awakened by a signal... but is it one of the signals
|
|
|
|
* that we were waiting for?
|
2012-05-30 17:36:46 +02:00
|
|
|
*/
|
2014-03-21 18:22:50 +01:00
|
|
|
|
2024-03-14 10:35:30 +01:00
|
|
|
if (nxsig_ismember(set, rtcb->sigunbinfo->si_signo))
|
2012-05-30 17:36:46 +02:00
|
|
|
{
|
|
|
|
/* Yes.. the return value is the number of the signal that
|
|
|
|
* awakened us.
|
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2024-03-14 10:35:30 +01:00
|
|
|
ret = rtcb->sigunbinfo->si_signo;
|
2012-05-30 17:36:46 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-10-06 16:28:20 +02:00
|
|
|
/* No... then report the EINTR error */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2017-10-06 16:28:20 +02:00
|
|
|
ret = -EINTR;
|
2012-05-30 17:36:46 +02:00
|
|
|
}
|
2011-11-29 19:21:52 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-10-12 16:55:19 +02:00
|
|
|
/* Otherwise, we must have been awakened by the timeout or,
|
|
|
|
* perhaps, the wait was cancelled.
|
2012-05-30 17:36:46 +02:00
|
|
|
*/
|
2011-11-29 19:21:52 +01:00
|
|
|
|
2017-10-12 16:55:19 +02:00
|
|
|
#ifdef CONFIG_CANCELLATION_POINTS
|
2024-03-14 10:35:30 +01:00
|
|
|
if (rtcb->sigunbinfo->si_signo == SIG_CANCEL_TIMEOUT)
|
2017-10-12 16:55:19 +02:00
|
|
|
{
|
|
|
|
/* The wait was canceled */
|
|
|
|
|
2024-03-14 10:35:30 +01:00
|
|
|
ret = -rtcb->sigunbinfo->si_errno;
|
2017-10-12 16:55:19 +02:00
|
|
|
DEBUGASSERT(ret < 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
2019-03-01 17:50:02 +01:00
|
|
|
{
|
|
|
|
/* We were awakened by a timeout. Set EAGAIN and return an
|
|
|
|
* error.
|
|
|
|
*/
|
|
|
|
|
2024-03-14 10:35:30 +01:00
|
|
|
DEBUGASSERT(rtcb->sigunbinfo->si_signo == SIG_WAIT_TIMEOUT);
|
2019-03-01 17:50:02 +01:00
|
|
|
ret = -EAGAIN;
|
|
|
|
}
|
2012-05-30 17:36:46 +02:00
|
|
|
}
|
2014-03-21 18:22:50 +01:00
|
|
|
|
2024-03-14 10:35:30 +01:00
|
|
|
rtcb->sigunbinfo = NULL;
|
|
|
|
|
2016-02-14 15:17:46 +01:00
|
|
|
leave_critical_section(flags);
|
2015-10-08 03:59:14 +02:00
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2017-10-06 16:28:20 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: sigtimedwait
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function selects the pending signal set specified by the argument
|
|
|
|
* set. If multiple signals are pending in set, it will remove and return
|
|
|
|
* the lowest numbered one. If no signals in set are pending at the time
|
|
|
|
* of the call, the calling process will be suspended until one of the
|
|
|
|
* signals in set becomes pending, OR until the process is interrupted by
|
|
|
|
* an unblocked signal, OR until the time interval specified by timeout
|
|
|
|
* (if any), has expired. If timeout is NULL, then the timeout interval
|
|
|
|
* is forever.
|
|
|
|
*
|
|
|
|
* If the info argument is non-NULL, the selected signal number is stored
|
2019-04-15 15:56:53 +02:00
|
|
|
* in the si_signo member and the cause of the signal is stored in the
|
2017-10-06 16:28:20 +02:00
|
|
|
* si_code member. The content of si_value is only meaningful if the
|
|
|
|
* signal was generated by sigqueue().
|
|
|
|
*
|
|
|
|
* The following values for si_code are defined in signal.h:
|
|
|
|
* SI_USER - Signal sent from kill, raise, or abort
|
|
|
|
* SI_QUEUE - Signal sent from sigqueue
|
|
|
|
* SI_TIMER - Signal is result of timer expiration
|
|
|
|
* SI_ASYNCIO - Signal is the result of asynch IO completion
|
|
|
|
* SI_MESGQ - Signal generated by arrival of a message on an
|
|
|
|
* empty message queue.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-10-06 16:28:20 +02:00
|
|
|
* set - The pending signal set.
|
|
|
|
* info - The returned value (may be NULL).
|
|
|
|
* timeout - The amount of time to wait (may be NULL)
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2017-10-06 16:28:20 +02:00
|
|
|
* Signal number that cause the wait to be terminated, otherwise -1 (ERROR)
|
|
|
|
* is returned with errno set to either:
|
|
|
|
*
|
|
|
|
* EAGAIN - No signal specified by set was generated within the specified
|
|
|
|
* timeout period.
|
|
|
|
* EINTR - The wait was interrupted by an unblocked, caught signal.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int sigtimedwait(FAR const sigset_t *set, FAR struct siginfo *info,
|
|
|
|
FAR const struct timespec *timeout)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* sigtimedwait() is a cancellation point */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
enter_cancellation_point();
|
2017-10-06 16:28:20 +02:00
|
|
|
|
|
|
|
/* Let nxsig_timedwait() do the work. */
|
|
|
|
|
|
|
|
ret = nxsig_timedwait(set, info, timeout);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
set_errno(-ret);
|
|
|
|
ret = ERROR;
|
|
|
|
}
|
|
|
|
|
2016-12-09 23:50:34 +01:00
|
|
|
leave_cancellation_point();
|
2015-10-08 03:59:14 +02:00
|
|
|
return ret;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|