2011-07-19 15:40:15 +02:00
|
|
|
/****************************************************************************
|
2014-08-08 21:53:29 +02:00
|
|
|
* sched/semaphore/sem_timedwait.c
|
2011-07-19 15:40:15 +02:00
|
|
|
*
|
2020-03-29 16:56:18 +02: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
|
2011-07-19 15:40:15 +02:00
|
|
|
*
|
2020-03-29 16:56:18 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-07-19 15:40:15 +02:00
|
|
|
*
|
2020-03-29 16:56:18 +02: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.
|
2011-07-19 15:40:15 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2016-02-14 15:17:46 +01:00
|
|
|
#include <nuttx/irq.h>
|
2011-07-19 15:40:15 +02:00
|
|
|
#include <nuttx/arch.h>
|
2014-08-21 19:16:55 +02:00
|
|
|
#include <nuttx/wdog.h>
|
2016-12-10 16:08:26 +01:00
|
|
|
#include <nuttx/cancelpt.h>
|
2011-07-19 15:40:15 +02:00
|
|
|
|
2014-08-09 01:53:55 +02:00
|
|
|
#include "sched/sched.h"
|
2014-08-08 22:43:02 +02:00
|
|
|
#include "clock/clock.h"
|
2014-08-08 21:53:29 +02:00
|
|
|
#include "semaphore/semaphore.h"
|
2011-07-19 15:40:15 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-10-05 15:24:54 +02:00
|
|
|
* Name: nxsem_timedwait
|
2011-07-19 15:40:15 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function will lock the semaphore referenced by sem as in the
|
|
|
|
* sem_wait() function. However, if the semaphore cannot be locked without
|
|
|
|
* waiting for another process or thread to unlock the semaphore by
|
|
|
|
* performing a sem_post() function, this wait will be terminated when the
|
|
|
|
* specified timeout expires.
|
|
|
|
*
|
|
|
|
* The timeout will expire when the absolute time specified by abstime
|
|
|
|
* passes, as measured by the clock on which timeouts are based (that is,
|
|
|
|
* when the value of that clock equals or exceeds abstime), or if the
|
|
|
|
* absolute time specified by abstime has already been passed at the
|
|
|
|
* time of the call.
|
|
|
|
*
|
2017-10-05 15:24:54 +02:00
|
|
|
* This is an internal OS interface. It is functionally equivalent to
|
|
|
|
* sem_wait except that:
|
|
|
|
*
|
2019-08-23 19:57:35 +02:00
|
|
|
* - It is not a cancellation point, and
|
2017-10-05 15:24:54 +02:00
|
|
|
* - It does not modify the errno value.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-10-05 15:24:54 +02:00
|
|
|
* sem - Semaphore object
|
2011-07-19 15:40:15 +02:00
|
|
|
* abstime - The absolute time to wait until a timeout is declared.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2017-10-05 15:24:54 +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.
|
|
|
|
* That may be one of:
|
2011-07-19 15:40:15 +02:00
|
|
|
*
|
|
|
|
* EINVAL The sem argument does not refer to a valid semaphore. Or the
|
|
|
|
* thread would have blocked, and the abstime parameter specified
|
|
|
|
* a nanoseconds field value less than zero or greater than or
|
|
|
|
* equal to 1000 million.
|
|
|
|
* ETIMEDOUT The semaphore could not be locked before the specified timeout
|
|
|
|
* expired.
|
|
|
|
* EDEADLK A deadlock condition was detected.
|
|
|
|
* EINTR A signal interrupted this function.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-10-05 15:24:54 +02:00
|
|
|
int nxsem_timedwait(FAR sem_t *sem, FAR const struct timespec *abstime)
|
2011-07-19 15:40:15 +02:00
|
|
|
{
|
2016-02-07 00:44:41 +01:00
|
|
|
FAR struct tcb_s *rtcb = this_task();
|
2011-07-19 15:40:15 +02:00
|
|
|
irqstate_t flags;
|
2018-06-16 20:16:13 +02:00
|
|
|
sclock_t ticks;
|
2017-10-05 15:24:54 +02:00
|
|
|
int status;
|
|
|
|
int ret = ERROR;
|
2011-07-19 15:40:15 +02:00
|
|
|
|
2013-02-06 16:43:28 +01:00
|
|
|
DEBUGASSERT(up_interrupt_context() == false && rtcb->waitdog == NULL);
|
2011-07-19 15:40:15 +02:00
|
|
|
|
|
|
|
/* Verify the input parameters and, in case of an error, set
|
|
|
|
* errno appropriately.
|
|
|
|
*/
|
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2011-07-19 15:40:15 +02:00
|
|
|
if (!abstime || !sem)
|
|
|
|
{
|
2017-10-05 15:24:54 +02:00
|
|
|
return -EINVAL;
|
2011-07-19 15:40:15 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Create a watchdog. We will not actually need this watchdog
|
2013-08-27 17:40:19 +02:00
|
|
|
* unless the semaphore is unavailable, but we will reserve it up
|
2011-07-19 15:40:15 +02:00
|
|
|
* front before we enter the following critical section.
|
|
|
|
*/
|
|
|
|
|
2013-02-06 16:43:28 +01:00
|
|
|
rtcb->waitdog = wd_create();
|
|
|
|
if (!rtcb->waitdog)
|
2011-07-19 15:40:15 +02:00
|
|
|
{
|
2017-10-05 15:24:54 +02:00
|
|
|
return -ENOMEM;
|
2011-07-19 15:40:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We will disable interrupts until we have completed the semaphore
|
|
|
|
* wait. We need to do this (as opposed to just disabling pre-emption)
|
2015-02-20 14:07:36 +01:00
|
|
|
* because there could be interrupt handlers that are asynchronously
|
2011-07-19 15:40:15 +02:00
|
|
|
* posting semaphores and to prevent race conditions with watchdog
|
|
|
|
* timeout. This is not too bad because interrupts will be re-
|
|
|
|
* enabled while we are blocked waiting for the semaphore.
|
|
|
|
*/
|
|
|
|
|
2016-02-14 15:17:46 +01:00
|
|
|
flags = enter_critical_section();
|
2011-07-19 15:40:15 +02:00
|
|
|
|
|
|
|
/* Try to take the semaphore without waiting. */
|
|
|
|
|
2017-10-05 15:59:06 +02:00
|
|
|
ret = nxsem_trywait(sem);
|
2014-04-22 17:01:20 +02:00
|
|
|
if (ret == OK)
|
2011-07-19 15:40:15 +02:00
|
|
|
{
|
|
|
|
/* We got it! */
|
|
|
|
|
2015-08-01 15:30:23 +02:00
|
|
|
goto success_with_irqdisabled;
|
2011-07-19 15:40:15 +02:00
|
|
|
}
|
|
|
|
|
2014-08-08 21:53:29 +02:00
|
|
|
/* We will have to wait for the semaphore. Make sure that we were provided
|
2011-07-19 15:40:15 +02:00
|
|
|
* with a valid timeout.
|
|
|
|
*/
|
|
|
|
|
2015-02-20 14:07:36 +01:00
|
|
|
if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
|
2011-07-19 15:40:15 +02:00
|
|
|
{
|
2017-10-05 15:24:54 +02:00
|
|
|
ret = -EINVAL;
|
2015-08-01 15:30:23 +02:00
|
|
|
goto errout_with_irqdisabled;
|
2011-07-19 15:40:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert the timespec to clock ticks. We must have interrupts
|
|
|
|
* disabled here so that this time stays valid until the wait begins.
|
2017-10-05 15:24:54 +02:00
|
|
|
*
|
|
|
|
* clock_abstime2ticks() returns zero on success or a POSITIVE errno
|
|
|
|
* value on failure.
|
2011-07-19 15:40:15 +02:00
|
|
|
*/
|
|
|
|
|
2017-10-05 15:24:54 +02:00
|
|
|
status = clock_abstime2ticks(CLOCK_REALTIME, abstime, &ticks);
|
2011-07-19 15:40:15 +02:00
|
|
|
|
|
|
|
/* If the time has already expired return immediately. */
|
|
|
|
|
2017-10-05 15:24:54 +02:00
|
|
|
if (status == OK && ticks <= 0)
|
2011-07-19 15:40:15 +02:00
|
|
|
{
|
2017-10-05 15:24:54 +02:00
|
|
|
ret = -ETIMEDOUT;
|
2015-08-01 15:30:23 +02:00
|
|
|
goto errout_with_irqdisabled;
|
2011-07-19 15:40:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle any time-related errors */
|
|
|
|
|
2017-10-05 15:24:54 +02:00
|
|
|
if (status != OK)
|
2011-07-19 15:40:15 +02:00
|
|
|
{
|
2017-10-05 15:24:54 +02:00
|
|
|
ret = -status;
|
2015-08-01 15:30:23 +02:00
|
|
|
goto errout_with_irqdisabled;
|
2011-07-19 15:40:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Start the watchdog */
|
|
|
|
|
2020-03-13 07:44:16 +01:00
|
|
|
wd_start(rtcb->waitdog, ticks, nxsem_timeout, 1, getpid());
|
2011-07-19 15:40:15 +02:00
|
|
|
|
2017-10-05 15:24:54 +02:00
|
|
|
/* Now perform the blocking wait. If nxsem_wait() fails, the
|
|
|
|
* negated errno value will be returned below.
|
|
|
|
*/
|
2011-07-19 15:40:15 +02:00
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
ret = nxsem_wait(sem);
|
2011-07-19 15:40:15 +02:00
|
|
|
|
|
|
|
/* Stop the watchdog timer */
|
|
|
|
|
2013-02-06 16:43:28 +01:00
|
|
|
wd_cancel(rtcb->waitdog);
|
2011-07-19 15:40:15 +02:00
|
|
|
|
|
|
|
/* We can now restore interrupts and delete the watchdog */
|
|
|
|
|
2015-08-01 15:30:23 +02:00
|
|
|
success_with_irqdisabled:
|
2017-10-05 15:24:54 +02:00
|
|
|
errout_with_irqdisabled:
|
2016-02-14 15:17:46 +01:00
|
|
|
leave_critical_section(flags);
|
2013-02-06 16:43:28 +01:00
|
|
|
wd_delete(rtcb->waitdog);
|
|
|
|
rtcb->waitdog = NULL;
|
2017-10-05 15:24:54 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2011-07-19 15:40:15 +02:00
|
|
|
|
include/nuttx: Fix improper use of inline
I finally figured out why the ez80 code has gotten so big. It is because people have been put putting big inline functions in header files. That is a violation of the coding standard, since only c89 compatibility is required in all common code. But we have been tolerating inline function it because include/nuttx/compiler.h defines 'inline' to be nothing for C89 compilers.
As a result, static inline functions declared within a C file not so bad; the inline qualifier is ignored, if not supported, but otherwise all is well.
But it is catastrophic in header files. Those static inline functions are included as static functions and implemented in EVERY file that includes those header files, even if the functions are never called. That makes the code base huge!So there is another PR coming to fix some of the worst offenders.
This commit fixes two of the worst offenders I have encountered so far: include/nuttx/sempahore.h and cache.h. But there may be a few other changes needed. Under include/nuttx there are still inline functions thread.h, inclue/nuttx/list.h, mutex.h, tree.h, and include/nuttx/crypto/blake2s.h with no protection for compilers that do not handler the inline qualifier. Otherwise we are clean.
With the changes to these two header files, the size of the z20x build is reduced by about 40%. And incredible size savings.
2020-03-02 21:51:28 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxsem_timedwait_uninterruptible
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function is wrapped version of nxsem_timedwait(), which is
|
|
|
|
* uninterruptible and convenient for use.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* sem - Semaphore object
|
|
|
|
* abstime - The absolute time to wait until a timeout is declared.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* EINVAL The sem argument does not refer to a valid semaphore. Or the
|
|
|
|
* thread would have blocked, and the abstime parameter specified
|
|
|
|
* a nanoseconds field value less than zero or greater than or
|
|
|
|
* equal to 1000 million.
|
|
|
|
* ETIMEDOUT The semaphore could not be locked before the specified timeout
|
|
|
|
* expired.
|
|
|
|
* EDEADLK A deadlock condition was detected.
|
2020-03-29 16:56:18 +02:00
|
|
|
* ECANCELED May be returned if the thread is canceled while waiting.
|
include/nuttx: Fix improper use of inline
I finally figured out why the ez80 code has gotten so big. It is because people have been put putting big inline functions in header files. That is a violation of the coding standard, since only c89 compatibility is required in all common code. But we have been tolerating inline function it because include/nuttx/compiler.h defines 'inline' to be nothing for C89 compilers.
As a result, static inline functions declared within a C file not so bad; the inline qualifier is ignored, if not supported, but otherwise all is well.
But it is catastrophic in header files. Those static inline functions are included as static functions and implemented in EVERY file that includes those header files, even if the functions are never called. That makes the code base huge!So there is another PR coming to fix some of the worst offenders.
This commit fixes two of the worst offenders I have encountered so far: include/nuttx/sempahore.h and cache.h. But there may be a few other changes needed. Under include/nuttx there are still inline functions thread.h, inclue/nuttx/list.h, mutex.h, tree.h, and include/nuttx/crypto/blake2s.h with no protection for compilers that do not handler the inline qualifier. Otherwise we are clean.
With the changes to these two header files, the size of the z20x build is reduced by about 40%. And incredible size savings.
2020-03-02 21:51:28 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int nxsem_timedwait_uninterruptible(FAR sem_t *sem,
|
|
|
|
FAR const struct timespec *abstime)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/* Take the semaphore (perhaps waiting) */
|
|
|
|
|
|
|
|
ret = nxsem_timedwait(sem, abstime);
|
|
|
|
}
|
2020-03-29 16:56:18 +02:00
|
|
|
while (ret == -EINTR);
|
include/nuttx: Fix improper use of inline
I finally figured out why the ez80 code has gotten so big. It is because people have been put putting big inline functions in header files. That is a violation of the coding standard, since only c89 compatibility is required in all common code. But we have been tolerating inline function it because include/nuttx/compiler.h defines 'inline' to be nothing for C89 compilers.
As a result, static inline functions declared within a C file not so bad; the inline qualifier is ignored, if not supported, but otherwise all is well.
But it is catastrophic in header files. Those static inline functions are included as static functions and implemented in EVERY file that includes those header files, even if the functions are never called. That makes the code base huge!So there is another PR coming to fix some of the worst offenders.
This commit fixes two of the worst offenders I have encountered so far: include/nuttx/sempahore.h and cache.h. But there may be a few other changes needed. Under include/nuttx there are still inline functions thread.h, inclue/nuttx/list.h, mutex.h, tree.h, and include/nuttx/crypto/blake2s.h with no protection for compilers that do not handler the inline qualifier. Otherwise we are clean.
With the changes to these two header files, the size of the z20x build is reduced by about 40%. And incredible size savings.
2020-03-02 21:51:28 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-10-05 15:24:54 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: sem_timedwait
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function will lock the semaphore referenced by sem as in the
|
|
|
|
* sem_wait() function. However, if the semaphore cannot be locked without
|
|
|
|
* waiting for another process or thread to unlock the semaphore by
|
|
|
|
* performing a sem_post() function, this wait will be terminated when the
|
|
|
|
* specified timeout expires.
|
|
|
|
*
|
|
|
|
* The timeout will expire when the absolute time specified by abstime
|
|
|
|
* passes, as measured by the clock on which timeouts are based (that is,
|
|
|
|
* when the value of that clock equals or exceeds abstime), or if the
|
|
|
|
* absolute time specified by abstime has already been passed at the
|
|
|
|
* time of the call.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-10-05 15:24:54 +02:00
|
|
|
* sem - Semaphore object
|
|
|
|
* abstime - The absolute time to wait until a timeout is declared.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2017-10-05 15:24:54 +02:00
|
|
|
* Zero (OK) is returned on success. On failure, -1 (ERROR) is returned
|
|
|
|
* and the errno is set appropriately:
|
|
|
|
*
|
|
|
|
* EINVAL The sem argument does not refer to a valid semaphore. Or the
|
|
|
|
* thread would have blocked, and the abstime parameter specified
|
|
|
|
* a nanoseconds field value less than zero or greater than or
|
|
|
|
* equal to 1000 million.
|
|
|
|
* ETIMEDOUT The semaphore could not be locked before the specified timeout
|
|
|
|
* expired.
|
|
|
|
* EDEADLK A deadlock condition was detected.
|
|
|
|
* EINTR A signal interrupted this function.
|
2020-03-29 16:56:18 +02:00
|
|
|
* ECANCELED May be returned if the thread is canceled while waiting.
|
2017-10-05 15:24:54 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
2011-07-19 15:40:15 +02:00
|
|
|
|
2017-10-05 15:24:54 +02:00
|
|
|
int sem_timedwait(FAR sem_t *sem, FAR const struct timespec *abstime)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* sem_timedwait() is a cancellation point */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
enter_cancellation_point();
|
2017-10-05 15:24:54 +02:00
|
|
|
|
|
|
|
/* Let nxsem_timedout() do the work */
|
|
|
|
|
|
|
|
ret = nxsem_timedwait(sem, abstime);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
set_errno(-ret);
|
|
|
|
ret = ERROR;
|
|
|
|
}
|
2013-02-06 16:43:28 +01:00
|
|
|
|
2016-12-09 23:50:34 +01:00
|
|
|
leave_cancellation_point();
|
2017-10-05 15:24:54 +02:00
|
|
|
return ret;
|
2011-07-19 15:40:15 +02:00
|
|
|
}
|