2016-08-07 16:25:30 +02:00
|
|
|
/****************************************************************************
|
2014-08-08 22:55:16 +02:00
|
|
|
* sched/timer/timer_initialize.c
|
2007-03-21 18:21:26 +01:00
|
|
|
*
|
2020-03-11 21:09:41 +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-03-21 18:21:26 +01:00
|
|
|
*
|
2020-03-11 21:09:41 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-03-21 18:21:26 +01:00
|
|
|
*
|
2020-03-11 21:09:41 +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-03-21 18:21:26 +01:00
|
|
|
*
|
2016-08-07 16:25:30 +02:00
|
|
|
****************************************************************************/
|
2007-03-21 18:21:26 +01:00
|
|
|
|
2016-08-07 16:25:30 +02:00
|
|
|
/****************************************************************************
|
2007-03-21 18:21:26 +01:00
|
|
|
* Included Files
|
2016-08-07 16:25:30 +02:00
|
|
|
****************************************************************************/
|
2007-03-21 18:21:26 +01:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2009-12-14 19:39:29 +01:00
|
|
|
#include <nuttx/compiler.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2007-03-21 18:21:26 +01:00
|
|
|
#include <time.h>
|
|
|
|
#include <errno.h>
|
2009-12-14 19:39:29 +01:00
|
|
|
|
2016-02-14 15:17:46 +01:00
|
|
|
#include <nuttx/irq.h>
|
2022-09-25 17:08:38 +02:00
|
|
|
#include <nuttx/queue.h>
|
2023-02-22 04:08:22 +01:00
|
|
|
#include <nuttx/trace.h>
|
2015-07-24 17:03:21 +02:00
|
|
|
|
2014-08-08 22:55:16 +02:00
|
|
|
#include "timer/timer.h"
|
2007-03-21 18:21:26 +01:00
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_POSIX_TIMERS
|
|
|
|
|
2016-08-07 16:25:30 +02:00
|
|
|
/****************************************************************************
|
2007-03-21 18:21:26 +01:00
|
|
|
* Private Data
|
2016-08-07 16:25:30 +02:00
|
|
|
****************************************************************************/
|
2007-03-21 18:21:26 +01:00
|
|
|
|
|
|
|
/* These are the preallocated times */
|
|
|
|
|
|
|
|
#if CONFIG_PREALLOC_TIMERS > 0
|
|
|
|
static struct posix_timer_s g_prealloctimers[CONFIG_PREALLOC_TIMERS];
|
|
|
|
#endif
|
|
|
|
|
2016-08-07 16:25:30 +02:00
|
|
|
/****************************************************************************
|
2007-03-21 18:21:26 +01:00
|
|
|
* Public Data
|
2016-08-07 16:25:30 +02:00
|
|
|
****************************************************************************/
|
2007-03-21 18:21:26 +01:00
|
|
|
|
2016-08-07 16:25:30 +02:00
|
|
|
#if CONFIG_PREALLOC_TIMERS > 0
|
2007-03-21 18:21:26 +01:00
|
|
|
/* This is a list of free, preallocated timer structures */
|
|
|
|
|
|
|
|
volatile sq_queue_t g_freetimers;
|
|
|
|
#endif
|
|
|
|
|
2016-08-07 16:25:30 +02:00
|
|
|
/* This is a list of instantiated timer structures -- active and inactive.
|
|
|
|
* The timers are place on this list by timer_create() and removed from the
|
|
|
|
* list by timer_delete() or when the owning thread exits.
|
2007-03-21 18:21:26 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
volatile sq_queue_t g_alloctimers;
|
|
|
|
|
2016-08-07 16:25:30 +02:00
|
|
|
/****************************************************************************
|
2007-03-21 18:21:26 +01:00
|
|
|
* Public Functions
|
2016-08-07 16:25:30 +02:00
|
|
|
****************************************************************************/
|
2007-03-21 18:21:26 +01:00
|
|
|
|
2016-08-07 16:25:30 +02:00
|
|
|
/****************************************************************************
|
2012-07-14 21:30:31 +02:00
|
|
|
* Name: timer_initialize
|
2007-03-21 18:21:26 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Boot up configuration of the POSIX timer facility.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2007-03-21 18:21:26 +01:00
|
|
|
* None
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2007-03-21 18:21:26 +01:00
|
|
|
* None
|
|
|
|
*
|
2016-08-07 16:25:30 +02:00
|
|
|
****************************************************************************/
|
2007-03-21 18:21:26 +01:00
|
|
|
|
2022-08-16 06:20:39 +02:00
|
|
|
void timer_initialize(void)
|
2007-03-21 18:21:26 +01:00
|
|
|
{
|
2023-02-22 04:08:22 +01:00
|
|
|
sched_trace_begin();
|
|
|
|
|
2009-12-13 02:27:04 +01:00
|
|
|
#if CONFIG_PREALLOC_TIMERS > 0
|
2007-03-21 18:21:26 +01:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Place all of the pre-allocated timers into the free timer list */
|
|
|
|
|
2015-10-08 03:59:14 +02:00
|
|
|
sq_init((FAR sq_queue_t *)&g_freetimers);
|
2007-03-21 18:21:26 +01:00
|
|
|
|
|
|
|
for (i = 0; i < CONFIG_PREALLOC_TIMERS; i++)
|
|
|
|
{
|
|
|
|
g_prealloctimers[i].pt_flags = PT_FLAGS_PREALLOCATED;
|
2015-10-08 03:59:14 +02:00
|
|
|
sq_addlast((FAR sq_entry_t *)&g_prealloctimers[i],
|
|
|
|
(FAR sq_queue_t *)&g_freetimers);
|
2007-03-21 18:21:26 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Initialize the list of allocated timers */
|
|
|
|
|
2015-10-08 03:59:14 +02:00
|
|
|
sq_init((FAR sq_queue_t *)&g_alloctimers);
|
2023-02-22 04:08:22 +01:00
|
|
|
sched_trace_end();
|
2007-03-21 18:21:26 +01:00
|
|
|
}
|
|
|
|
|
2016-08-07 16:25:30 +02:00
|
|
|
/****************************************************************************
|
2012-07-14 21:30:31 +02:00
|
|
|
* Name: timer_deleteall
|
2007-03-21 18:21:26 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2016-08-07 16:25:30 +02:00
|
|
|
* This function is called whenever a thread exits. Any timers owned by
|
|
|
|
* that thread are deleted as though called by timer_delete().
|
2007-03-21 18:21:26 +01:00
|
|
|
*
|
|
|
|
* It is provided in this file so that it can be weakly defined but also,
|
|
|
|
* like timer_intitialize(), be brought into the link whenever the timer
|
|
|
|
* resources are referenced.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2013-01-25 01:01:08 +01:00
|
|
|
* pid - the task ID of the thread that exited
|
2007-03-21 18:21:26 +01:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2007-03-21 18:21:26 +01:00
|
|
|
* None
|
|
|
|
*
|
2016-08-07 16:25:30 +02:00
|
|
|
****************************************************************************/
|
2007-03-21 18:21:26 +01:00
|
|
|
|
2022-08-16 06:20:39 +02:00
|
|
|
void timer_deleteall(pid_t pid)
|
2007-03-21 18:21:26 +01:00
|
|
|
{
|
|
|
|
FAR struct posix_timer_s *timer;
|
|
|
|
FAR struct posix_timer_s *next;
|
|
|
|
irqstate_t flags;
|
|
|
|
|
2016-02-14 15:17:46 +01:00
|
|
|
flags = enter_critical_section();
|
2016-08-07 16:25:30 +02:00
|
|
|
for (timer = (FAR struct posix_timer_s *)g_alloctimers.head;
|
|
|
|
timer != NULL;
|
|
|
|
timer = next)
|
2007-03-21 18:21:26 +01:00
|
|
|
{
|
|
|
|
next = timer->flink;
|
2007-03-24 00:22:22 +01:00
|
|
|
if (timer->pt_owner == pid)
|
2007-03-21 18:21:26 +01:00
|
|
|
{
|
|
|
|
timer_delete((timer_t)timer);
|
|
|
|
}
|
|
|
|
}
|
2012-07-14 21:30:31 +02:00
|
|
|
|
2016-02-14 15:17:46 +01:00
|
|
|
leave_critical_section(flags);
|
2007-03-21 18:21:26 +01:00
|
|
|
}
|
|
|
|
|
2022-10-31 19:11:18 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: timer_gethandle
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Returns the posix timer in the activity from the corresponding timerid
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* timerid - The pre-thread timer, previously created by the call to
|
|
|
|
* timer_create(), to be be set.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* On success, timer_gethandle() returns pointer to the posix_timer_s;
|
|
|
|
* On error, NULL is returned.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
FAR struct posix_timer_s *timer_gethandle(timer_t timerid)
|
|
|
|
{
|
|
|
|
FAR struct posix_timer_s *timer = NULL;
|
|
|
|
FAR sq_entry_t *entry;
|
2023-11-20 06:30:27 +01:00
|
|
|
irqstate_t flags;
|
2022-10-31 19:11:18 +01:00
|
|
|
|
|
|
|
if (timerid != NULL)
|
|
|
|
{
|
2023-11-20 06:30:27 +01:00
|
|
|
flags = spin_lock_irqsave(NULL);
|
2022-10-31 19:11:18 +01:00
|
|
|
|
|
|
|
sq_for_every(&g_alloctimers, entry)
|
|
|
|
{
|
|
|
|
if (entry == timerid)
|
|
|
|
{
|
|
|
|
timer = (FAR struct posix_timer_s *)timerid;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-20 06:30:27 +01:00
|
|
|
spin_unlock_irqrestore(NULL, flags);
|
2022-10-31 19:11:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return timer;
|
|
|
|
}
|
|
|
|
|
2007-03-21 18:21:26 +01:00
|
|
|
#endif /* CONFIG_DISABLE_POSIX_TIMERS */
|