2008-01-31 18:59:22 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* sched/sig_timedwait.c
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2009-12-14 19:39:29 +01:00
|
|
|
* Copyright (C) 2007-2009 Gregory Nutt. All rights reserved.
|
2007-02-18 00:21:28 +01:00
|
|
|
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
2008-01-31 18:59:22 +01:00
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
2007-02-18 00:21:28 +01:00
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
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>
|
|
|
|
|
|
|
|
#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 <wdog.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <debug.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#include <nuttx/arch.h>
|
|
|
|
#include "os_internal.h"
|
|
|
|
#include "sig_internal.h"
|
|
|
|
#include "clock_internal.h"
|
|
|
|
|
2008-01-31 18:59:22 +01:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Definitions
|
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
|
|
|
* Private Type Declarations
|
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
|
|
|
* Global Variables
|
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
|
|
|
* Private Variables
|
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
|
|
|
* Private Functionss
|
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
|
|
|
* Function: sig_timeout
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* A timeout elapsed while waiting for signals to be queued.
|
2008-01-31 18:59:22 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2009-12-14 19:39:29 +01:00
|
|
|
static void sig_timeout(int argc, uint32_t itcb)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2008-01-31 18:59:22 +01:00
|
|
|
/* On many small machines, pointers are encoded and cannot be simply cast
|
2009-12-14 19:39:29 +01:00
|
|
|
* from uint32_t to _TCB*. The following union works around this
|
2008-01-31 18:59:22 +01:00
|
|
|
* (see wdogparm_t). This odd logic could be conditioned on
|
|
|
|
* CONFIG_CAN_CAST_POINTERS, but it is not too bad in any case.
|
2007-02-21 22:55:16 +01:00
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-02-21 22:55:16 +01:00
|
|
|
union
|
|
|
|
{
|
2007-02-27 22:17:21 +01:00
|
|
|
FAR _TCB *wtcb;
|
2009-12-14 19:39:29 +01:00
|
|
|
uint32_t itcb;
|
2007-02-21 22:55:16 +01:00
|
|
|
} u;
|
|
|
|
|
|
|
|
u.itcb = itcb;
|
|
|
|
|
|
|
|
if (!u.wtcb)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
|
|
|
PANIC(OSERR_TIMEOUTNOTCB);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* There may be a race condition -- make sure the task is
|
|
|
|
* still waiting for a signal
|
|
|
|
*/
|
|
|
|
|
2007-02-21 22:55:16 +01:00
|
|
|
if (u.wtcb->task_state == TSTATE_WAIT_SIG)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2008-01-31 18:59:22 +01:00
|
|
|
u.wtcb->sigunbinfo.si_signo = ERROR;
|
|
|
|
u.wtcb->sigunbinfo.si_code = SI_TIMER;
|
2007-02-21 22:55:16 +01:00
|
|
|
u.wtcb->sigunbinfo.si_value.sival_int = 0;
|
|
|
|
up_unblock_task(u.wtcb);
|
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
|
|
|
|
2008-01-31 18:59:22 +01:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Function: sigtimedwait
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* in the si_signo member and the cause of the signal is store in the
|
|
|
|
* si_code emember. The content of si_value is only meaningful if the
|
|
|
|
* signal was generated by sigqueue().
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* The following values for si_code are defined in signal.h:
|
2007-03-20 17:51:12 +01:00
|
|
|
* SI_USER - Signal sent from kill, raise, or abort
|
2007-02-18 00:21:28 +01:00
|
|
|
* SI_QUEUE - Signal sent from sigqueue
|
2007-03-20 17:51:12 +01:00
|
|
|
* SI_TIMER - Signal is result of timer expiration
|
|
|
|
* SI_ASYNCIO - Signal is the result of asynch IO completion
|
2007-02-18 00:21:28 +01:00
|
|
|
* SI_MESGQ - Signal generated by arrival of a message on an
|
2007-03-20 17:51:12 +01:00
|
|
|
* empty message queue.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* set - The pending signal set.
|
|
|
|
* info - The returned value
|
|
|
|
* timeout - The amount of time to wait
|
|
|
|
*
|
|
|
|
* Return Value:
|
2008-01-31 18:59:22 +01:00
|
|
|
* Signal number that cause the wait to be terminated, otherwise -1 (ERROR)
|
|
|
|
* is returned.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
2008-01-31 18:59:22 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-01-31 18:59:22 +01:00
|
|
|
int sigtimedwait(FAR const sigset_t *set, FAR struct siginfo *info,
|
|
|
|
FAR const struct timespec *timeout)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2007-02-27 22:17:21 +01:00
|
|
|
FAR _TCB *rtcb = (FAR _TCB*)g_readytorun.head;
|
|
|
|
sigset_t intersection;
|
|
|
|
FAR sigpendq_t *sigpend;
|
|
|
|
WDOG_ID wdog;
|
|
|
|
irqstate_t saved_state;
|
2009-12-14 19:39:29 +01:00
|
|
|
int32_t waitticks;
|
2007-02-27 22:17:21 +01:00
|
|
|
int ret = ERROR;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
sched_lock(); /* Not necessary */
|
|
|
|
|
|
|
|
/* 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!
|
|
|
|
*/
|
|
|
|
|
|
|
|
saved_state = irqsave();
|
|
|
|
|
|
|
|
/* Check if there is a pending signal corresponding to one of the
|
|
|
|
* signals in the pending signal set argument.
|
|
|
|
*/
|
|
|
|
|
|
|
|
intersection = *set & sig_pendingset(rtcb);
|
|
|
|
if (intersection != NULL_SIGNAL_SET)
|
|
|
|
{
|
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
sigpend = sig_removependingsignal(rtcb, sig_lowest(&intersection));
|
|
|
|
if (!sigpend)
|
|
|
|
{
|
|
|
|
PANIC(OSERR_NOPENDINGSIGNAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the signal info to the caller if so requested */
|
|
|
|
|
2007-02-21 22:55:16 +01:00
|
|
|
if (info)
|
|
|
|
{
|
|
|
|
memcpy(info, &sigpend->info, sizeof(struct siginfo));
|
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* Then dispose of the pending signal structure properly */
|
|
|
|
|
|
|
|
sig_releasependingsignal(sigpend);
|
|
|
|
irqrestore(saved_state);
|
|
|
|
|
|
|
|
/* The return value is the number of the signal that awakened us */
|
|
|
|
|
|
|
|
ret = info->si_signo;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We will have to wait for a signal to be posted to this task. */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Save the set of pending signals to wait for */
|
|
|
|
|
|
|
|
rtcb->sigwaitmask = *set;
|
|
|
|
|
|
|
|
/* Check if we should wait for the timeout */
|
|
|
|
|
|
|
|
if (timeout)
|
|
|
|
{
|
|
|
|
/* Convert the timespec to milliseconds */
|
|
|
|
|
|
|
|
waitticks = MSEC2TICK(timeout->tv_sec * MSEC_PER_SEC
|
|
|
|
+ timeout->tv_nsec / NSEC_PER_MSEC);
|
|
|
|
|
|
|
|
/* Create a watchdog */
|
|
|
|
|
|
|
|
wdog = wd_create();
|
|
|
|
if (wdog)
|
|
|
|
{
|
2007-02-21 22:55:16 +01:00
|
|
|
/* This little of nonsense is necessary for some
|
2009-12-14 19:39:29 +01:00
|
|
|
* processors where sizeof(pointer) < sizeof(uint32_t).
|
2007-02-21 22:55:16 +01:00
|
|
|
* see wdog.h.
|
|
|
|
*/
|
|
|
|
|
|
|
|
wdparm_t wdparm;
|
2008-01-31 18:59:22 +01:00
|
|
|
wdparm.pvarg = (FAR void*)rtcb;
|
2007-02-21 22:55:16 +01:00
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
/* Start the watchdog */
|
|
|
|
|
2008-01-31 18:59:22 +01:00
|
|
|
wd_start(wdog, waitticks, (wdentry_t)sig_timeout, 1, wdparm.dwarg);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* Now wait for either the signal or the watchdog */
|
|
|
|
|
|
|
|
up_block_task(rtcb, TSTATE_WAIT_SIG);
|
|
|
|
|
|
|
|
/* We no longer need the watchdog */
|
|
|
|
|
|
|
|
wd_delete(wdog);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No timeout, just wait */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* And wait until one of the unblocked signals is posted */
|
|
|
|
|
|
|
|
up_block_task(rtcb, TSTATE_WAIT_SIG);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We are running again, clear the sigwaitmask */
|
|
|
|
|
|
|
|
rtcb->sigwaitmask = NULL_SIGNAL_SET;
|
|
|
|
|
|
|
|
/* When we awaken, the cause will be in the TCB. Return the signal
|
|
|
|
* info to the caller if so requested
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (info)
|
|
|
|
{
|
2007-02-21 22:55:16 +01:00
|
|
|
memcpy(info, &rtcb->sigunbinfo, sizeof(struct siginfo));
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2011-11-29 19:21:52 +01:00
|
|
|
/* The return value is the number of the signal that awakened us */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2011-11-29 19:21:52 +01:00
|
|
|
ret = info->si_signo;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* We don't know which signal awakened us. This is probably a bug. */
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
irqrestore(saved_state);
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
sched_unlock();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|