2016-08-07 16:25:30 +02:00
|
|
|
/****************************************************************************
|
2014-08-08 22:55:16 +02:00
|
|
|
* sched/timer/timer_create.c
|
2007-03-21 01:56:49 +01:00
|
|
|
*
|
2018-08-19 19:19:43 +02:00
|
|
|
* Copyright (C) 2007-2009, 2011, 2014-2017 Gregory Nutt. All rights
|
|
|
|
* reserved.
|
2012-07-14 21:30:31 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2007-03-21 01:56:49 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2007-12-31 00:34:20 +01:00
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
2007-03-21 01:56:49 +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.
|
|
|
|
*
|
2016-08-07 16:25:30 +02:00
|
|
|
****************************************************************************/
|
2007-03-21 01:56:49 +01:00
|
|
|
|
2016-08-07 16:25:30 +02:00
|
|
|
/****************************************************************************
|
2007-03-21 01:56:49 +01:00
|
|
|
* Included Files
|
2016-08-07 16:25:30 +02:00
|
|
|
****************************************************************************/
|
2007-03-21 01:56:49 +01:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2009-12-14 20:30:18 +01:00
|
|
|
|
|
|
|
#include <stdint.h>
|
2007-03-21 18:21:26 +01:00
|
|
|
#include <unistd.h>
|
2007-03-21 01:56:49 +01:00
|
|
|
#include <time.h>
|
2008-06-03 03:21:12 +02:00
|
|
|
#include <string.h>
|
2007-03-21 01:56:49 +01:00
|
|
|
#include <errno.h>
|
2009-12-14 20:30:18 +01:00
|
|
|
|
2016-02-14 15:17:46 +01:00
|
|
|
#include <nuttx/irq.h>
|
2014-08-21 19:16:55 +02:00
|
|
|
#include <nuttx/wdog.h>
|
2011-04-03 22:41:49 +02:00
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
|
2014-08-08 22:55:16 +02:00
|
|
|
#include "timer/timer.h"
|
2007-03-21 01:56:49 +01:00
|
|
|
|
2007-03-21 18:21:26 +01:00
|
|
|
#ifndef CONFIG_DISABLE_POSIX_TIMERS
|
2007-03-21 01:56:49 +01:00
|
|
|
|
2016-08-07 16:25:30 +02:00
|
|
|
/****************************************************************************
|
2007-03-21 01:56:49 +01:00
|
|
|
* Private Functions
|
2016-08-07 16:25:30 +02:00
|
|
|
****************************************************************************/
|
2007-03-21 01:56:49 +01:00
|
|
|
|
2016-08-07 16:25:30 +02:00
|
|
|
/****************************************************************************
|
2012-07-14 21:30:31 +02:00
|
|
|
* Name: timer_allocate
|
2007-03-21 18:21:26 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Allocate one POSIX timer and place it into the allocated timer list.
|
|
|
|
*
|
2016-08-07 16:25:30 +02:00
|
|
|
****************************************************************************/
|
2007-03-21 18:21:26 +01:00
|
|
|
|
2015-12-01 19:54:57 +01:00
|
|
|
static FAR struct posix_timer_s *timer_allocate(void)
|
2007-03-21 18:21:26 +01:00
|
|
|
{
|
2015-10-08 03:59:14 +02:00
|
|
|
FAR struct posix_timer_s *ret;
|
|
|
|
irqstate_t flags;
|
|
|
|
uint8_t pt_flags;
|
2007-03-21 18:21:26 +01:00
|
|
|
|
|
|
|
/* Try to get a preallocated timer from the free list */
|
|
|
|
|
|
|
|
#if CONFIG_PREALLOC_TIMERS > 0
|
2016-02-14 15:17:46 +01:00
|
|
|
flags = enter_critical_section();
|
2015-10-08 03:59:14 +02:00
|
|
|
ret = (FAR struct posix_timer_s *)sq_remfirst((FAR sq_queue_t *)&g_freetimers);
|
2016-02-14 15:17:46 +01:00
|
|
|
leave_critical_section(flags);
|
2007-03-21 18:21:26 +01:00
|
|
|
|
|
|
|
/* Did we get one? */
|
2014-04-13 22:32:20 +02:00
|
|
|
|
2008-06-01 19:46:26 +02:00
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
pt_flags = PT_FLAGS_PREALLOCATED;
|
|
|
|
}
|
|
|
|
else
|
2007-03-21 18:21:26 +01:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* Allocate a new timer from the heap */
|
|
|
|
|
2020-01-12 17:07:54 +01:00
|
|
|
ret = (FAR struct posix_timer_s *)
|
|
|
|
kmm_malloc(sizeof(struct posix_timer_s));
|
2008-06-01 19:46:26 +02:00
|
|
|
pt_flags = 0;
|
2007-03-21 18:21:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If we have a timer, then put it into the allocated timer list */
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
{
|
2008-06-01 19:46:26 +02:00
|
|
|
/* Initialize the timer structure */
|
|
|
|
|
|
|
|
memset(ret, 0, sizeof(struct posix_timer_s));
|
|
|
|
ret->pt_flags = pt_flags;
|
|
|
|
|
|
|
|
/* And add it to the end of the list of allocated timers */
|
|
|
|
|
2016-02-14 15:17:46 +01:00
|
|
|
flags = enter_critical_section();
|
2015-10-08 03:59:14 +02:00
|
|
|
sq_addlast((FAR sq_entry_t *)ret, (FAR sq_queue_t *)&g_alloctimers);
|
2016-02-14 15:17:46 +01:00
|
|
|
leave_critical_section(flags);
|
2007-03-21 18:21:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-08-07 16:25:30 +02:00
|
|
|
/****************************************************************************
|
2007-03-21 01:56:49 +01:00
|
|
|
* Public Functions
|
2016-08-07 16:25:30 +02:00
|
|
|
****************************************************************************/
|
2007-03-21 01:56:49 +01:00
|
|
|
|
2016-08-07 16:25:30 +02:00
|
|
|
/****************************************************************************
|
2012-07-14 21:30:31 +02:00
|
|
|
* Name: timer_create
|
2007-03-21 01:56:49 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2016-08-07 16:25:30 +02:00
|
|
|
* The timer_create() function creates per-thread timer using the
|
|
|
|
* specified clock, clock_id, as the timing base. The timer_create()
|
|
|
|
* function returns, in the location referenced by timerid, a timer ID of
|
|
|
|
* type timer_t used to identify the timer in timer requests. This timer
|
|
|
|
* ID is unique until the timer is deleted. The particular clock, clock_id,
|
|
|
|
* is defined in <time.h>. The timer whose ID is returned will be in a
|
|
|
|
* disarmed state upon return from timer_create().
|
2007-03-21 01:56:49 +01:00
|
|
|
*
|
2016-08-07 16:25:30 +02:00
|
|
|
* The evp argument, if non-NULL, points to a sigevent structure. This
|
|
|
|
* structure is allocated by the called and defines the asynchronous
|
|
|
|
* notification to occur. If the evp argument is NULL, the effect is as
|
|
|
|
* if the evp argument pointed to a sigevent structure with the
|
|
|
|
* sigev_notify member having the value SIGEV_SIGNAL, the sigev_signo
|
|
|
|
* having a default signal number, and the sigev_value member having the
|
|
|
|
* value of the timer ID.
|
2007-03-21 01:56:49 +01:00
|
|
|
*
|
2016-08-07 16:25:30 +02:00
|
|
|
* Each implementation defines a set of clocks that can be used as timing
|
|
|
|
* bases for per-thread timers. All implementations shall support a
|
|
|
|
* clock_id of CLOCK_REALTIME.
|
2007-03-21 01:56:49 +01:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2007-03-21 01:56:49 +01:00
|
|
|
* clockid - Specifies the clock to use as the timing base.
|
|
|
|
* evp - Refers to a user allocated sigevent structure that defines the
|
|
|
|
* asynchronous notification. evp may be NULL (see above).
|
|
|
|
* timerid - The pre-thread timer created by the call to timer_create().
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2007-03-21 01:56:49 +01:00
|
|
|
* If the call succeeds, timer_create() will return 0 (OK) and update the
|
|
|
|
* location referenced by timerid to a timer_t, which can be passed to the
|
2016-08-07 16:25:30 +02:00
|
|
|
* other per-thread timer calls. If an error occurs, the function will
|
|
|
|
* return a value of -1 (ERROR) and set errno to indicate the error.
|
2007-03-21 01:56:49 +01:00
|
|
|
*
|
2016-08-07 16:25:30 +02:00
|
|
|
* EAGAIN - The system lacks sufficient signal queuing resources to honor
|
|
|
|
* the request.
|
|
|
|
* EAGAIN - The calling process has already created all of the timers it
|
|
|
|
* is allowed by this implementation.
|
2007-03-21 01:56:49 +01:00
|
|
|
* EINVAL - The specified clock ID is not defined.
|
2016-08-07 16:25:30 +02:00
|
|
|
* ENOTSUP - The implementation does not support the creation of a timer
|
|
|
|
* attached to the CPU-time clock that is specified by clock_id and
|
|
|
|
* associated with a thread different thread invoking timer_create().
|
2007-03-21 01:56:49 +01:00
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
2016-08-07 16:25:30 +02:00
|
|
|
****************************************************************************/
|
2007-03-21 01:56:49 +01:00
|
|
|
|
2016-08-07 16:25:30 +02:00
|
|
|
int timer_create(clockid_t clockid, FAR struct sigevent *evp,
|
|
|
|
FAR timer_t *timerid)
|
2007-03-21 01:56:49 +01:00
|
|
|
{
|
2015-12-01 19:54:57 +01:00
|
|
|
FAR struct posix_timer_s *ret;
|
|
|
|
WDOG_ID wdog;
|
2007-03-21 18:21:26 +01:00
|
|
|
|
|
|
|
/* Sanity checks. Also, we support only CLOCK_REALTIME */
|
|
|
|
|
2016-08-07 16:25:30 +02:00
|
|
|
if (timerid == NULL || clockid != CLOCK_REALTIME)
|
2007-03-21 18:21:26 +01:00
|
|
|
{
|
2014-08-29 01:00:24 +02:00
|
|
|
set_errno(EINVAL);
|
2007-03-21 18:21:26 +01:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate a watchdog to provide the underling CLOCK_REALTIME timer */
|
|
|
|
|
|
|
|
wdog = wd_create();
|
|
|
|
if (!wdog)
|
|
|
|
{
|
2014-08-29 01:00:24 +02:00
|
|
|
set_errno(EAGAIN);
|
2007-03-21 18:21:26 +01:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate a timer instance to contain the watchdog */
|
|
|
|
|
|
|
|
ret = timer_allocate();
|
|
|
|
if (!ret)
|
|
|
|
{
|
2017-09-25 15:17:09 +02:00
|
|
|
wd_delete(wdog);
|
2014-08-29 01:00:24 +02:00
|
|
|
set_errno(EAGAIN);
|
2007-03-21 18:21:26 +01:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the timer instance */
|
|
|
|
|
2008-06-03 03:21:12 +02:00
|
|
|
ret->pt_crefs = 1;
|
2007-03-21 18:21:26 +01:00
|
|
|
ret->pt_owner = getpid();
|
|
|
|
ret->pt_delay = 0;
|
|
|
|
ret->pt_wdog = wdog;
|
|
|
|
|
2016-01-28 23:38:54 +01:00
|
|
|
/* Was a struct sigevent provided? */
|
|
|
|
|
2007-03-21 18:21:26 +01:00
|
|
|
if (evp)
|
|
|
|
{
|
2016-01-28 23:38:54 +01:00
|
|
|
/* Yes, copy the entire struct sigevent content */
|
|
|
|
|
2015-12-30 20:20:31 +01:00
|
|
|
memcpy(&ret->pt_event, evp, sizeof(struct sigevent));
|
2007-03-21 18:21:26 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-01-28 23:38:54 +01:00
|
|
|
/* "If the evp argument is NULL, the effect is as if the evp argument
|
|
|
|
* pointed to a sigevent structure with the sigev_notify member
|
|
|
|
* having the value SIGEV_SIGNAL, the sigev_signo having a default
|
|
|
|
* signal number, and the sigev_value member having the value of the
|
|
|
|
* timer ID."
|
|
|
|
*/
|
|
|
|
|
2016-01-28 23:50:20 +01:00
|
|
|
ret->pt_event.sigev_notify = SIGEV_SIGNAL;
|
|
|
|
ret->pt_event.sigev_signo = SIGALRM;
|
|
|
|
ret->pt_event.sigev_value.sival_ptr = ret;
|
2016-01-28 23:38:54 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_SIG_EVTHREAD
|
2016-01-28 23:50:20 +01:00
|
|
|
ret->pt_event.sigev_notify_function = NULL;
|
|
|
|
ret->pt_event.sigev_notify_attributes = NULL;
|
2016-01-28 23:38:54 +01:00
|
|
|
#endif
|
2007-03-21 18:21:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the timer */
|
|
|
|
|
|
|
|
*timerid = ret;
|
|
|
|
return OK;
|
2007-03-21 01:56:49 +01:00
|
|
|
}
|
|
|
|
|
2007-03-21 18:21:26 +01:00
|
|
|
#endif /* CONFIG_DISABLE_POSIX_TIMERS */
|