2014-09-28 10:15:49 -06:00
|
|
|
/****************************************************************************
|
|
|
|
|
* include/nuttx/semaphore.h
|
|
|
|
|
*
|
2020-03-29 11:56:18 -03: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
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
2014-09-28 10:15:49 -06:00
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#ifndef __INCLUDE_NUTTX_SEMAPHORE_H
|
|
|
|
|
#define __INCLUDE_NUTTX_SEMAPHORE_H
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Included Files
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
2018-08-27 06:07:50 -06:00
|
|
|
#include <errno.h>
|
2014-09-28 10:15:49 -06:00
|
|
|
#include <semaphore.h>
|
|
|
|
|
|
2016-01-21 11:54:26 -06:00
|
|
|
#include <nuttx/clock.h>
|
2014-09-28 10:15:49 -06:00
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Pre-processor Definitions
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2021-12-01 23:40:16 +08:00
|
|
|
/* Initializers */
|
|
|
|
|
|
|
|
|
|
#ifdef CONFIG_PRIORITY_INHERITANCE
|
|
|
|
|
# if CONFIG_SEM_PREALLOCHOLDERS > 0
|
|
|
|
|
# define NXSEM_INITIALIZER(c, f) \
|
|
|
|
|
{(c), (f), NULL} /* semcount, flags, hhead */
|
|
|
|
|
# else
|
|
|
|
|
# define NXSEM_INITIALIZER(c, f) \
|
2022-02-08 16:11:16 +08:00
|
|
|
{(c), (f), {SEMHOLDER_INITIALIZER, SEMHOLDER_INITIALIZER}} /* semcount, flags, holder[2] */
|
2021-12-01 23:40:16 +08:00
|
|
|
# endif
|
|
|
|
|
#else /* CONFIG_PRIORITY_INHERITANCE */
|
|
|
|
|
# define NXSEM_INITIALIZER(c, f) \
|
|
|
|
|
{(c)} /* semcount, flags */
|
|
|
|
|
#endif /* CONFIG_PRIORITY_INHERITANCE */
|
|
|
|
|
|
2017-10-08 11:52:32 -06:00
|
|
|
/* Most internal nxsem_* interfaces are not available in the user space in
|
2017-10-08 08:13:47 -06:00
|
|
|
* PROTECTED and KERNEL builds. In that context, the application semaphore
|
|
|
|
|
* interfaces must be used. The differences between the two sets of
|
|
|
|
|
* interfaces are: (1) the nxsem_* interfaces do not cause cancellation
|
|
|
|
|
* points and (2) they do not modify the errno variable.
|
|
|
|
|
*
|
|
|
|
|
* This is only important when compiling libraries (libc or libnx) that are
|
|
|
|
|
* used both by the OS (libkc.a and libknx.a) or by the applications
|
2020-05-02 03:23:04 +08:00
|
|
|
* (libc.a and libnx.a). In that case, the correct interface must be
|
2017-10-08 08:13:47 -06:00
|
|
|
* used for the build context.
|
|
|
|
|
*
|
2018-09-26 06:57:49 -06:00
|
|
|
* REVISIT: In the flat build, the same functions must be used both by
|
|
|
|
|
* the OS and by applications. We have to use the normal user functions
|
|
|
|
|
* in this case or we will fail to set the errno or fail to create the
|
|
|
|
|
* cancellation point.
|
2017-10-08 08:13:47 -06:00
|
|
|
*/
|
|
|
|
|
|
2018-09-26 06:57:49 -06:00
|
|
|
#if !defined(CONFIG_BUILD_FLAT) && defined(__KERNEL__)
|
2017-10-08 11:52:32 -06:00
|
|
|
# define _SEM_INIT(s,p,c) nxsem_init(s,p,c)
|
|
|
|
|
# define _SEM_DESTROY(s) nxsem_destroy(s)
|
|
|
|
|
# define _SEM_WAIT(s) nxsem_wait(s)
|
|
|
|
|
# define _SEM_TRYWAIT(s) nxsem_trywait(s)
|
|
|
|
|
# define _SEM_TIMEDWAIT(s,t) nxsem_timedwait(s,t)
|
2020-07-27 13:46:03 +08:00
|
|
|
# define _SEM_CLOCKWAIT(s,c,t) nxsem_clockwait(s,c,t)
|
2017-10-08 11:52:32 -06:00
|
|
|
# define _SEM_POST(s) nxsem_post(s)
|
2021-07-26 10:36:13 +08:00
|
|
|
# define _SEM_GETVALUE(s,v) nxsem_get_value(s,v)
|
2020-05-17 07:56:21 -06:00
|
|
|
# define _SEM_GETPROTOCOL(s,p) nxsem_get_protocol(s,p)
|
|
|
|
|
# define _SEM_SETPROTOCOL(s,p) nxsem_set_protocol(s,p)
|
2017-10-08 11:52:32 -06:00
|
|
|
# define _SEM_ERRNO(r) (-(r))
|
|
|
|
|
# define _SEM_ERRVAL(r) (r)
|
2017-10-08 08:13:47 -06:00
|
|
|
#else
|
2017-10-08 11:52:32 -06:00
|
|
|
# define _SEM_INIT(s,p,c) sem_init(s,p,c)
|
|
|
|
|
# define _SEM_DESTROY(s) sem_destroy(s)
|
|
|
|
|
# define _SEM_WAIT(s) sem_wait(s)
|
|
|
|
|
# define _SEM_TRYWAIT(s) sem_trywait(s)
|
|
|
|
|
# define _SEM_TIMEDWAIT(s,t) sem_timedwait(s,t)
|
2020-07-27 13:46:03 +08:00
|
|
|
# define _SEM_CLOCKWAIT(s,c,t) sem_clockwait(s,c,t)
|
2017-10-08 11:52:32 -06:00
|
|
|
# define _SEM_GETVALUE(s,v) sem_getvalue(s,v)
|
|
|
|
|
# define _SEM_POST(s) sem_post(s)
|
|
|
|
|
# define _SEM_GETPROTOCOL(s,p) sem_getprotocol(s,p)
|
|
|
|
|
# define _SEM_SETPROTOCOL(s,p) sem_setprotocol(s,p)
|
|
|
|
|
# define _SEM_ERRNO(r) errno
|
|
|
|
|
# define _SEM_ERRVAL(r) (-errno)
|
2017-10-08 08:13:47 -06:00
|
|
|
#endif
|
|
|
|
|
|
2014-09-28 10:15:49 -06:00
|
|
|
/****************************************************************************
|
2015-08-01 07:30:23 -06:00
|
|
|
* Public Type Definitions
|
2014-09-28 10:15:49 -06:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
2014-09-28 13:02:36 -06:00
|
|
|
#ifdef CONFIG_FS_NAMED_SEMAPHORES
|
2014-09-28 10:15:49 -06:00
|
|
|
/* This is the named semaphore inode */
|
|
|
|
|
|
2014-09-28 15:58:56 -06:00
|
|
|
struct inode;
|
2014-09-28 14:35:17 -06:00
|
|
|
struct nsem_inode_s
|
2014-09-28 10:15:49 -06:00
|
|
|
{
|
2016-11-02 14:24:16 -06:00
|
|
|
/* This must be the first element of the structure. In sem_close() this
|
|
|
|
|
* structure must be cast compatible with sem_t.
|
|
|
|
|
*/
|
|
|
|
|
|
2016-11-02 09:05:18 -06:00
|
|
|
sem_t ns_sem; /* The contained semaphore */
|
|
|
|
|
|
2016-11-02 18:21:46 -06:00
|
|
|
/* Inode payload unique to named semaphores. */
|
2014-09-28 15:58:56 -06:00
|
|
|
|
2015-10-23 07:09:25 +08:00
|
|
|
FAR struct inode *ns_inode; /* Containing inode */
|
2014-09-28 10:15:49 -06:00
|
|
|
};
|
2014-09-28 13:02:36 -06:00
|
|
|
#endif
|
2014-09-28 10:15:49 -06:00
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Public Data
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#ifndef __ASSEMBLY__
|
|
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
#define EXTERN extern "C"
|
|
|
|
|
extern "C"
|
|
|
|
|
{
|
|
|
|
|
#else
|
|
|
|
|
#define EXTERN extern
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Public Function Prototypes
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2015-08-01 07:30:23 -06:00
|
|
|
/****************************************************************************
|
2017-10-03 12:51:15 -06:00
|
|
|
* Name: nxsem_init
|
|
|
|
|
*
|
|
|
|
|
* Description:
|
2019-09-08 15:59:14 -06:00
|
|
|
* This function initializes the UNNAMED semaphore sem. Following a
|
2017-10-03 12:51:15 -06:00
|
|
|
* successful call to nxsem_init(), the semaphore may be used in subsequent
|
2017-10-03 15:35:24 -06:00
|
|
|
* calls to nxsem_wait(), nxsem_post(), and nxsem_trywait(). The semaphore
|
2017-10-03 12:51:15 -06:00
|
|
|
* remains usable until it is destroyed.
|
|
|
|
|
*
|
|
|
|
|
* Only sem itself may be used for performing synchronization. The result
|
|
|
|
|
* of referring to copies of sem in calls to sem_wait(), sem_trywait(),
|
|
|
|
|
* sem_post(), and sem_destroy() is undefined.
|
|
|
|
|
*
|
2018-03-13 09:52:27 -06:00
|
|
|
* Input Parameters:
|
2017-10-03 12:51:15 -06:00
|
|
|
* sem - Semaphore to be initialized
|
|
|
|
|
* pshared - Process sharing (not used)
|
|
|
|
|
* value - Semaphore initialization value
|
|
|
|
|
*
|
2018-02-01 10:00:02 -06:00
|
|
|
* Returned Value:
|
2017-10-03 15:35:24 -06:00
|
|
|
* This is an internal OS interface and should not be used by applications.
|
2017-10-03 12:51:15 -06:00
|
|
|
* It follows the NuttX internal error return policy: Zero (OK) is
|
|
|
|
|
* returned on success. A negated errno value is returned on failure.
|
|
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
int nxsem_init(FAR sem_t *sem, int pshared, unsigned int value);
|
|
|
|
|
|
2017-10-03 15:35:24 -06:00
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nxsem_destroy
|
|
|
|
|
*
|
|
|
|
|
* Description:
|
|
|
|
|
* This function is used to destroy the un-named semaphore indicated by
|
|
|
|
|
* 'sem'. Only a semaphore that was created using nxsem_init() may be
|
2020-03-29 11:56:18 -03:00
|
|
|
* destroyed using nxsem_destroy(); the effect of calling nxsem_destroy()
|
|
|
|
|
* with a named semaphore is undefined. The effect of subsequent use of
|
|
|
|
|
* the semaphore sem is undefined until sem is re-initialized by another
|
|
|
|
|
* call to nxsem_init().
|
2017-10-03 15:35:24 -06:00
|
|
|
*
|
|
|
|
|
* The effect of destroying a semaphore upon which other processes are
|
|
|
|
|
* currently blocked is undefined.
|
|
|
|
|
*
|
2018-03-13 09:52:27 -06:00
|
|
|
* Input Parameters:
|
2017-10-03 15:35:24 -06:00
|
|
|
* sem - Semaphore to be destroyed.
|
|
|
|
|
*
|
2018-02-01 10:00:02 -06:00
|
|
|
* Returned Value:
|
2017-10-03 15:35:24 -06: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.
|
|
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2020-01-02 10:49:34 -06:00
|
|
|
int nxsem_destroy(FAR sem_t *sem);
|
2017-10-04 15:22:27 -06:00
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nxsem_wait
|
|
|
|
|
*
|
|
|
|
|
* Description:
|
|
|
|
|
* This function attempts to lock the semaphore referenced by 'sem'. If
|
|
|
|
|
* the semaphore value is (<=) zero, then the calling task will not return
|
|
|
|
|
* until it successfully acquires the lock.
|
|
|
|
|
*
|
|
|
|
|
* This is an internal OS interface. It is functionally equivalent to
|
|
|
|
|
* sem_wait except that:
|
|
|
|
|
*
|
2019-08-23 11:57:35 -06:00
|
|
|
* - It is not a cancellation point, and
|
2017-10-04 15:22:27 -06:00
|
|
|
* - It does not modify the errno value.
|
|
|
|
|
*
|
2018-03-13 09:52:27 -06:00
|
|
|
* Input Parameters:
|
2017-10-04 15:22:27 -06:00
|
|
|
* sem - Semaphore descriptor.
|
|
|
|
|
*
|
2018-02-01 10:00:02 -06:00
|
|
|
* Returned Value:
|
2017-10-04 15:22:27 -06: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.
|
|
|
|
|
* Possible returned errors:
|
|
|
|
|
*
|
2017-10-05 07:59:06 -06:00
|
|
|
* EINVAL - Invalid attempt to get the semaphore
|
|
|
|
|
* EINTR - The wait was interrupted by the receipt of a signal.
|
2017-10-04 15:22:27 -06:00
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
int nxsem_wait(FAR sem_t *sem);
|
2017-10-03 15:35:24 -06:00
|
|
|
|
2017-10-05 07:59:06 -06:00
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nxsem_trywait
|
|
|
|
|
*
|
|
|
|
|
* Description:
|
|
|
|
|
* This function locks the specified semaphore only if the semaphore is
|
|
|
|
|
* currently not locked. Otherwise, it locks the semaphore. In either
|
|
|
|
|
* case, the call returns without blocking.
|
|
|
|
|
*
|
2018-03-13 09:52:27 -06:00
|
|
|
* Input Parameters:
|
2017-10-05 07:59:06 -06:00
|
|
|
* sem - the semaphore descriptor
|
|
|
|
|
*
|
2018-02-01 10:00:02 -06:00
|
|
|
* Returned Value:
|
2017-10-05 07:59:06 -06: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.
|
|
|
|
|
* Possible returned errors:
|
|
|
|
|
*
|
|
|
|
|
* EINVAL - Invalid attempt to get the semaphore
|
|
|
|
|
* EAGAIN - The semaphore is not available.
|
|
|
|
|
*
|
|
|
|
|
* Assumptions:
|
|
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
int nxsem_trywait(FAR sem_t *sem);
|
|
|
|
|
|
2017-10-05 07:24:54 -06:00
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nxsem_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.
|
|
|
|
|
*
|
|
|
|
|
* This is an internal OS interface. It is functionally equivalent to
|
|
|
|
|
* sem_wait except that:
|
|
|
|
|
*
|
2019-08-23 11:57:35 -06:00
|
|
|
* - It is not a cancellation point, and
|
2017-10-05 07:24:54 -06:00
|
|
|
* - It does not modify the errno value.
|
|
|
|
|
*
|
2018-03-13 09:52:27 -06:00
|
|
|
* Input Parameters:
|
2017-10-05 07:24:54 -06:00
|
|
|
* sem - Semaphore object
|
|
|
|
|
* abstime - The absolute time to wait until a timeout is declared.
|
|
|
|
|
*
|
2018-02-01 10:00:02 -06:00
|
|
|
* Returned Value:
|
2017-10-05 07:24:54 -06: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:
|
|
|
|
|
*
|
|
|
|
|
* 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 11:56:18 -03:00
|
|
|
* ECANCELED May be returned if the thread is canceled while waiting.
|
2017-10-05 07:24:54 -06:00
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
int nxsem_timedwait(FAR sem_t *sem, FAR const struct timespec *abstime);
|
|
|
|
|
|
2020-07-27 13:46:03 +08:00
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nxsem_clockwait
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
* This is an internal OS interface. It is functionally equivalent to
|
|
|
|
|
* sem_wait except that:
|
|
|
|
|
*
|
|
|
|
|
* - It is not a cancellation point, and
|
|
|
|
|
* - It does not modify the errno value.
|
|
|
|
|
*
|
|
|
|
|
* Input Parameters:
|
|
|
|
|
* sem - Semaphore object
|
|
|
|
|
* clockid - The timing source to use in the conversion
|
|
|
|
|
* abstime - The absolute time to wait until a timeout is declared.
|
|
|
|
|
*
|
|
|
|
|
* Returned Value:
|
|
|
|
|
* 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:
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
* ECANCELED May be returned if the thread is canceled while waiting.
|
|
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
int nxsem_clockwait(FAR sem_t *sem, clockid_t clockid,
|
|
|
|
|
FAR const struct timespec *abstime);
|
|
|
|
|
|
2017-10-03 12:51:15 -06:00
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nxsem_tickwait
|
2015-08-01 07:30:23 -06:00
|
|
|
*
|
|
|
|
|
* Description:
|
|
|
|
|
* This function is a lighter weight version of sem_timedwait(). It is
|
|
|
|
|
* non-standard and intended only for use within the RTOS.
|
|
|
|
|
*
|
2018-03-13 09:52:27 -06:00
|
|
|
* Input Parameters:
|
2015-08-01 07:30:23 -06:00
|
|
|
* sem - Semaphore object
|
2015-08-01 14:57:31 -06:00
|
|
|
* delay - Ticks to wait from the start time until the semaphore is
|
|
|
|
|
* posted. If ticks is zero, then this function is equivalent
|
|
|
|
|
* to sem_trywait().
|
2015-08-01 07:30:23 -06:00
|
|
|
*
|
2018-02-01 10:00:02 -06:00
|
|
|
* Returned Value:
|
2017-10-03 12:51:15 -06:00
|
|
|
* This is an internal OS interface, not available to applications, and
|
|
|
|
|
* hence follows the NuttX internal error return policy: Zero (OK) is
|
2020-03-29 11:56:18 -03:00
|
|
|
* returned on success. A negated errno value is returned on failure:
|
|
|
|
|
*
|
|
|
|
|
* -ETIMEDOUT is returned on the timeout condition.
|
|
|
|
|
* -ECANCELED may be returned if the thread is canceled while waiting.
|
2017-10-03 12:51:15 -06:00
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2022-05-10 08:33:09 +08:00
|
|
|
int nxsem_tickwait(FAR sem_t *sem, uint32_t delay);
|
2017-10-03 12:51:15 -06:00
|
|
|
|
2017-10-03 15:35:24 -06:00
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nxsem_post
|
|
|
|
|
*
|
|
|
|
|
* Description:
|
|
|
|
|
* When a kernel thread has finished with a semaphore, it will call
|
|
|
|
|
* nxsem_post(). This function unlocks the semaphore referenced by sem
|
|
|
|
|
* by performing the semaphore unlock operation on that semaphore.
|
|
|
|
|
*
|
|
|
|
|
* If the semaphore value resulting from this operation is positive, then
|
|
|
|
|
* no tasks were blocked waiting for the semaphore to become unlocked; the
|
|
|
|
|
* semaphore is simply incremented.
|
|
|
|
|
*
|
|
|
|
|
* If the value of the semaphore resulting from this operation is zero,
|
|
|
|
|
* then one of the tasks blocked waiting for the semaphore shall be
|
|
|
|
|
* allowed to return successfully from its call to sem_wait().
|
|
|
|
|
*
|
2018-03-13 09:52:27 -06:00
|
|
|
* Input Parameters:
|
2017-10-03 15:35:24 -06:00
|
|
|
* sem - Semaphore descriptor
|
|
|
|
|
*
|
2018-02-01 10:00:02 -06:00
|
|
|
* Returned Value:
|
2017-10-03 15:35:24 -06: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.
|
|
|
|
|
*
|
|
|
|
|
* Assumptions:
|
|
|
|
|
* This function may be called from an interrupt handler.
|
|
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
int nxsem_post(FAR sem_t *sem);
|
|
|
|
|
|
2017-10-03 12:51:15 -06:00
|
|
|
/****************************************************************************
|
2020-05-17 07:56:21 -06:00
|
|
|
* Name: nxsem_get_value
|
2017-10-03 12:51:15 -06:00
|
|
|
*
|
|
|
|
|
* Description:
|
|
|
|
|
* This function updates the location referenced by 'sval' argument to
|
|
|
|
|
* have the value of the semaphore referenced by 'sem' without effecting
|
|
|
|
|
* the state of the semaphore. The updated value represents the actual
|
|
|
|
|
* semaphore value that occurred at some unspecified time during the call,
|
|
|
|
|
* but may not reflect the actual value of the semaphore when it is
|
|
|
|
|
* returned to the calling task.
|
|
|
|
|
*
|
2020-05-17 07:56:21 -06:00
|
|
|
* If 'sem' is locked, the value return by nxsem_get_value() will either be
|
2017-10-03 12:51:15 -06:00
|
|
|
* zero or a negative number whose absolute value represents the number
|
|
|
|
|
* of tasks waiting for the semaphore.
|
|
|
|
|
*
|
2018-03-13 09:52:27 -06:00
|
|
|
* Input Parameters:
|
2017-10-03 12:51:15 -06:00
|
|
|
* sem - Semaphore descriptor
|
|
|
|
|
* sval - Buffer by which the value is returned
|
|
|
|
|
*
|
2018-02-01 10:00:02 -06:00
|
|
|
* Returned Value:
|
2017-10-03 12:51:15 -06: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.
|
2015-08-01 07:30:23 -06:00
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2020-05-17 07:56:21 -06:00
|
|
|
int nxsem_get_value(FAR sem_t *sem, FAR int *sval);
|
2015-08-01 07:30:23 -06:00
|
|
|
|
2016-03-05 07:33:24 -06:00
|
|
|
/****************************************************************************
|
2017-10-03 12:51:15 -06:00
|
|
|
* Name: nxsem_reset
|
2016-03-05 07:33:24 -06:00
|
|
|
*
|
|
|
|
|
* Description:
|
2016-03-06 13:50:26 -06:00
|
|
|
* Reset a semaphore count to a specific value. This is similar to part
|
2017-10-03 12:51:15 -06:00
|
|
|
* of the operation of nxsem_init(). But nxsem_reset() may need to wake up
|
2016-03-06 13:50:26 -06:00
|
|
|
* tasks waiting on a count. This kind of operation is sometimes required
|
|
|
|
|
* within the OS (only) for certain error handling conditions.
|
2016-03-05 07:33:24 -06:00
|
|
|
*
|
2018-03-13 09:52:27 -06:00
|
|
|
* Input Parameters:
|
2016-03-05 07:33:24 -06:00
|
|
|
* sem - Semaphore descriptor to be reset
|
|
|
|
|
* count - The requested semaphore count
|
|
|
|
|
*
|
2018-02-01 10:00:02 -06:00
|
|
|
* Returned Value:
|
2017-10-03 12:51:15 -06:00
|
|
|
* This is an internal OS interface, not available to applications, and
|
|
|
|
|
* hence follows the NuttX internal error return policy: Zero (OK) is
|
|
|
|
|
* returned on success. A negated errno value is returned on failure.
|
2016-03-05 07:33:24 -06:00
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2017-10-03 12:51:15 -06:00
|
|
|
int nxsem_reset(FAR sem_t *sem, int16_t count);
|
2016-03-05 07:33:24 -06:00
|
|
|
|
2017-10-03 15:35:24 -06:00
|
|
|
/****************************************************************************
|
2020-05-17 07:56:21 -06:00
|
|
|
* Name: nxsem_get_protocol
|
2017-10-03 15:35:24 -06:00
|
|
|
*
|
|
|
|
|
* Description:
|
|
|
|
|
* Return the value of the semaphore protocol attribute.
|
|
|
|
|
*
|
2018-03-13 09:52:27 -06:00
|
|
|
* Input Parameters:
|
2017-10-03 15:35:24 -06:00
|
|
|
* sem - A pointer to the semaphore whose attributes are to be
|
|
|
|
|
* queried.
|
|
|
|
|
* protocol - The user provided location in which to store the protocol
|
|
|
|
|
* value.
|
|
|
|
|
*
|
2018-02-01 10:00:02 -06:00
|
|
|
* Returned Value:
|
2017-10-03 15:35:24 -06: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.
|
|
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2020-05-17 07:56:21 -06:00
|
|
|
#define nxsem_get_protocol(s,p) sem_getprotocol(s,p)
|
2017-10-03 15:35:24 -06:00
|
|
|
|
|
|
|
|
/****************************************************************************
|
2020-05-17 07:56:21 -06:00
|
|
|
* Name: nxsem_set_protocol
|
2017-10-03 15:35:24 -06:00
|
|
|
*
|
|
|
|
|
* Description:
|
|
|
|
|
* Set semaphore protocol attribute.
|
|
|
|
|
*
|
2019-09-08 15:59:14 -06:00
|
|
|
* One particularly important use of this function is when a semaphore
|
2017-10-03 15:35:24 -06:00
|
|
|
* is used for inter-task communication like:
|
|
|
|
|
*
|
|
|
|
|
* TASK A TASK B
|
|
|
|
|
* sem_init(sem, 0, 0);
|
|
|
|
|
* sem_wait(sem);
|
|
|
|
|
* sem_post(sem);
|
|
|
|
|
* Awakens as holder
|
|
|
|
|
*
|
|
|
|
|
* In this case priority inheritance can interfere with the operation of
|
|
|
|
|
* the semaphore. The problem is that when TASK A is restarted it is a
|
|
|
|
|
* holder of the semaphore. However, it never calls sem_post(sem) so it
|
|
|
|
|
* becomes *permanently* a holder of the semaphore and may have its
|
|
|
|
|
* priority boosted when any other task tries to acquire the semaphore.
|
|
|
|
|
*
|
2020-05-17 07:56:21 -06:00
|
|
|
* The fix is to call nxsem_set_protocol(SEM_PRIO_NONE) immediately after
|
2017-10-03 15:35:24 -06:00
|
|
|
* the sem_init() call so that there will be no priority inheritance
|
|
|
|
|
* operations on this semaphore.
|
|
|
|
|
*
|
2018-03-13 09:52:27 -06:00
|
|
|
* Input Parameters:
|
2017-10-03 15:35:24 -06:00
|
|
|
* sem - A pointer to the semaphore whose attributes are to be
|
|
|
|
|
* modified
|
|
|
|
|
* protocol - The new protocol to use
|
|
|
|
|
*
|
2018-02-01 10:00:02 -06:00
|
|
|
* Returned Value:
|
2017-10-03 15:35:24 -06: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.
|
|
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2020-05-17 07:56:21 -06:00
|
|
|
int nxsem_set_protocol(FAR sem_t *sem, int protocol);
|
2017-10-03 15:35:24 -06:00
|
|
|
|
2018-08-27 06:07:50 -06:00
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nxsem_wait_uninterruptible
|
|
|
|
|
*
|
|
|
|
|
* Description:
|
2020-03-29 11:56:18 -03:00
|
|
|
* This function is wrapped version of nxsem_wait(), which is
|
|
|
|
|
* uninterruptible and convenient for use.
|
2018-08-27 06:07:50 -06:00
|
|
|
*
|
|
|
|
|
* Parameters:
|
|
|
|
|
* sem - Semaphore descriptor.
|
|
|
|
|
*
|
|
|
|
|
* Return Value:
|
2020-04-05 10:39:35 -06:00
|
|
|
* Zero(OK) - On success
|
|
|
|
|
* EINVAL - Invalid attempt to get the semaphore
|
|
|
|
|
* ECANCELED - May be returned if the thread is canceled while waiting.
|
2018-08-27 06:07:50 -06:00
|
|
|
*
|
2020-04-06 09:39:49 -06:00
|
|
|
* NOTE: It is essential that callers of this function handle the
|
|
|
|
|
* ECANCELED error. Correct handling is that the function should return the
|
|
|
|
|
* error and the error should propagate back up the calling tree to the
|
|
|
|
|
* cancellation point interface function where the thread termination will
|
|
|
|
|
* be handled gracefully
|
|
|
|
|
*
|
2018-08-27 06:07:50 -06: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 14:51:28 -06:00
|
|
|
int nxsem_wait_uninterruptible(FAR sem_t *sem);
|
2018-08-27 06:07:50 -06:00
|
|
|
|
2020-01-02 10:49:34 -06: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 11:56:18 -03:00
|
|
|
* ECANCELED May be returned if the thread is canceled while waiting.
|
2020-01-02 10:49:34 -06:00
|
|
|
*
|
2020-04-06 09:39:49 -06:00
|
|
|
* NOTE: It is essential that callers of this function handle the
|
|
|
|
|
* ECANCELED error. Correct handling is that the function should return the
|
|
|
|
|
* error and the error should propagate back up the calling tree to the
|
|
|
|
|
* cancellation point interface function where the thread termination will
|
|
|
|
|
* be handled gracefully
|
|
|
|
|
*
|
2020-01-02 10:49:34 -06: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 14:51:28 -06:00
|
|
|
int nxsem_timedwait_uninterruptible(FAR sem_t *sem,
|
|
|
|
|
FAR const struct timespec *abstime);
|
2020-01-02 10:49:34 -06:00
|
|
|
|
2020-07-27 13:46:03 +08:00
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nxsem_clockwait_uninterruptible
|
|
|
|
|
*
|
|
|
|
|
* Description:
|
2022-05-10 09:32:31 +08:00
|
|
|
* This function is wrapped version of nxsem_clockwait(), which is
|
2020-07-27 13:46:03 +08:00
|
|
|
* uninterruptible and convenient for use.
|
|
|
|
|
*
|
|
|
|
|
* Input Parameters:
|
|
|
|
|
* sem - Semaphore object
|
|
|
|
|
* clockid - The timing source to use in the conversion
|
|
|
|
|
* 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.
|
|
|
|
|
* ECANCELED May be returned if the thread is canceled while waiting.
|
|
|
|
|
*
|
|
|
|
|
* NOTE: It is essential that callers of this function handle the
|
|
|
|
|
* ECANCELED error. Correct handling is that the function should return the
|
|
|
|
|
* error and the error should propagate back up the calling tree to the
|
|
|
|
|
* cancellation point interface function where the thread termination will
|
|
|
|
|
* be handled gracefully
|
|
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
int nxsem_clockwait_uninterruptible(FAR sem_t *sem, clockid_t clockid,
|
|
|
|
|
FAR const struct timespec *abstime);
|
|
|
|
|
|
2020-01-02 10:49:34 -06:00
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nxsem_tickwait_uninterruptible
|
|
|
|
|
*
|
|
|
|
|
* Description:
|
|
|
|
|
* This function is wrapped version of nxsem_tickwait(), which is
|
|
|
|
|
* uninterruptible and convenient for use.
|
|
|
|
|
*
|
|
|
|
|
* Input Parameters:
|
|
|
|
|
* sem - Semaphore object
|
|
|
|
|
* delay - Ticks to wait from the start time until the semaphore is
|
|
|
|
|
* posted. If ticks is zero, then this function is equivalent
|
|
|
|
|
* to sem_trywait().
|
|
|
|
|
*
|
|
|
|
|
* Returned Value:
|
2020-03-29 11:56:18 -03:00
|
|
|
* This is an internal OS interface, not available to applications, and
|
|
|
|
|
* hence follows the NuttX internal error return policy: Zero (OK) is
|
|
|
|
|
* returned on success. A negated errno value is returned on failure:
|
|
|
|
|
*
|
|
|
|
|
* -ETIMEDOUT is returned on the timeout condition.
|
|
|
|
|
* -ECANCELED may be returned if the thread is canceled while waiting.
|
2020-01-02 10:49:34 -06:00
|
|
|
*
|
2020-04-06 09:39:49 -06:00
|
|
|
* NOTE: It is essential that callers of this function handle the
|
|
|
|
|
* ECANCELED error. Correct handling is that the function should return the
|
|
|
|
|
* error and the error should propagate back up the calling tree to the
|
|
|
|
|
* cancellation point interface function where the thread termination will
|
|
|
|
|
* be handled gracefully
|
|
|
|
|
*
|
2020-01-02 10:49:34 -06:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
2022-05-10 08:33:09 +08:00
|
|
|
int nxsem_tickwait_uninterruptible(FAR sem_t *sem, uint32_t delay);
|
2020-01-02 10:49:34 -06:00
|
|
|
|
2014-09-28 10:15:49 -06:00
|
|
|
#undef EXTERN
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#endif /* __ASSEMBLY__ */
|
|
|
|
|
#endif /* __INCLUDE_NUTTX_SEMAPHORE_H */
|