Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
193 changes: 178 additions & 15 deletions include/nuttx/mutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,17 @@
* Pre-processor Definitions
****************************************************************************/

#define NXMUTEX_NO_HOLDER ((pid_t)-1)
#define NXMUTEX_INITIALIZER {NXSEM_INITIALIZER(1, SEM_TYPE_MUTEX | \
SEM_PRIO_INHERIT), NXMUTEX_NO_HOLDER}
#define NXRMUTEX_INITIALIZER {NXMUTEX_INITIALIZER, 0}
#define NXMUTEX_NO_HOLDER ((pid_t)-1)
#define NXMUTEX_RESET ((pid_t)-2)

#define NXMUTEX_INITIALIZER {NXSEM_INITIALIZER(1, SEM_TYPE_MUTEX | \
SEM_PRIO_INHERIT), NXMUTEX_NO_HOLDER}
#define NXRMUTEX_INITIALIZER {NXMUTEX_INITIALIZER, 0}

#define NXMUTEX_HOLDER(m) ((m)->holder)

/****************************************************************************
* Public Type Definitions
* Public Type Declarations
****************************************************************************/

struct mutex_s
Expand Down Expand Up @@ -126,11 +130,28 @@ int nxmutex_destroy(FAR mutex_t *mutex);
* mutex - mutex descriptor.
*
* Return Value:
* Return true if mutex is hold by current thread.
*
****************************************************************************/

bool nxmutex_is_hold(FAR mutex_t *mutex);

/****************************************************************************
* Name: nxmutex_is_reset
*
* Description:
* This function check whether the mutex is be reset
*
* Parameters:
* mutex - mutex descriptor.
*
* Return Value:
* Return true if mutex is be reset.
*
****************************************************************************/

#define nxmutex_is_reset(mutex) (NXMUTEX_HOLDER(mutex) == NXMUTEX_RESET)

/****************************************************************************
* Name: nxmutex_is_locked
*
Expand All @@ -141,6 +162,7 @@ bool nxmutex_is_hold(FAR mutex_t *mutex);
* mutex - mutex descriptor.
*
* Return Value:
* Return true if mutex is locked.
*
****************************************************************************/

Expand Down Expand Up @@ -195,7 +217,7 @@ int nxmutex_trylock(FAR mutex_t *mutex);
* Name: nxmutex_timedlock
*
* Description:
* This function attempts to lock the mutex . If the mutex value
* This function attempts to lock the mutex. If the mutex value
* is (<=) zero,then the calling task will not return until it
* successfully acquires the lock or timed out
*
Expand All @@ -217,6 +239,34 @@ int nxmutex_trylock(FAR mutex_t *mutex);

int nxmutex_timedlock(FAR mutex_t *mutex, unsigned int timeout);

/****************************************************************************
* Name: nxmutex_clocklock
*
* Description:
* This function attempts to lock the mutex. If the mutex value
* is (<=) zero,then the calling task will not return until it
* successfully acquires the lock or timed out
*
* Input Parameters:
* mutex - Mutex object
* clockid - The timing source to use in the conversion
* abs_timeout - The abs time when mutex lock timed out
*
* Returned Value:
* OK The mutex successfully acquires
* EINVAL The mutex argument does not refer to a valid mutex. 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 mutex could not be locked before the specified timeout
* expired.
* EDEADLK A deadlock condition was detected.
*
****************************************************************************/

int nxmutex_clocklock(FAR mutex_t *mutex, clockid_t clockid,
FAR const struct timespec *abs_timeout);

/****************************************************************************
* Name: nxmutex_unlock
*
Expand Down Expand Up @@ -250,18 +300,18 @@ int nxmutex_unlock(FAR mutex_t *mutex);
*
****************************************************************************/

#if defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__)
void nxmutex_reset(FAR mutex_t *mutex);
#endif

/****************************************************************************
* Name: nxmutex_breaklock
*
* Description:
* This function attempts to break the mutex
*
* Parameters:
* Input Parameters:
* mutex - Mutex descriptor.
*
* Output Parameters:
* locked - Is the mutex break success
*
* Return Value:
Expand Down Expand Up @@ -293,6 +343,47 @@ int nxmutex_breaklock(FAR mutex_t *mutex, FAR bool *locked);

int nxmutex_restorelock(FAR mutex_t *mutex, bool locked);

/****************************************************************************
* Name: nxmutex_set_protocol
*
* Description:
* Set mutex protocol attribute.
*
* Input Parameters:
* mutex - A pointer to the mutex whose attributes are to be
* modified
* protocol - The new protocol to use
*
* 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.
*
****************************************************************************/

int nxmutex_set_protocol(FAR mutex_t *mutex, int protocol);

/****************************************************************************
* Name: nxmutex_get_protocol
*
* Description:
* Return the value of the mutex protocol attribute.
*
* Input Parameters:
* mutex - A pointer to the mutex whose attributes are to be
* queried.
* protocol - The user provided location in which to store the protocol
* value.
*
* 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.
*
****************************************************************************/

int nxmutex_get_protocol(FAR mutex_t *mutex, FAR int *protocol);

/****************************************************************************
* Name: nxrmutex_init
*
Expand Down Expand Up @@ -347,7 +438,7 @@ int nxrmutex_destroy(FAR rmutex_t *rmutex);
*
****************************************************************************/

bool nxrmutex_is_hold(FAR rmutex_t *rmutex);
#define nxrmutex_is_hold(rmutex) nxmutex_is_hold(&(rmutex)->mutex)

/****************************************************************************
* Name: nxrmutex_is_locked
Expand All @@ -363,7 +454,7 @@ bool nxrmutex_is_hold(FAR rmutex_t *rmutex);
*
****************************************************************************/

bool nxrmutex_is_locked(FAR rmutex_t *rmutex);
#define nxrmutex_is_locked(rmutex) nxmutex_is_locked(&(rmutex)->mutex)

/****************************************************************************
* Name: nrxmutex_lock
Expand Down Expand Up @@ -415,7 +506,7 @@ int nxrmutex_trylock(FAR rmutex_t *rmutex);
* Name: nxrmutex_timedlock
*
* Description:
* This function attempts to lock the mutex . If the mutex value
* This function attempts to lock the mutex. If the mutex value
* is (<=) zero,then the calling task will not return until it
* successfully acquires the lock or timed out
*
Expand All @@ -438,6 +529,34 @@ int nxrmutex_trylock(FAR rmutex_t *rmutex);

int nxrmutex_timedlock(FAR rmutex_t *rmutex, unsigned int timeout);

/****************************************************************************
* Name: nxrmutex_clocklock
*
* Description:
* This function attempts to lock the mutex. If the mutex value
* is (<=) zero,then the calling task will not return until it
* successfully acquires the lock or timed out
*
* Input Parameters:
* rmutex - Rmutex object
* clockid - The timing source to use in the conversion
* abs_timeout - The abs time when mutex lock timed out
*
* Returned Value:
* OK The mutex successfully acquires
* EINVAL The mutex argument does not refer to a valid mutex. 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 mutex could not be locked before the specified timeout
* expired.
* EDEADLK A deadlock condition was detected.
*
****************************************************************************/

int nxrmutex_clocklock(FAR rmutex_t *rmutex, clockid_t clockid,
FAR const struct timespec *abs_timeout);

/****************************************************************************
* Name: nxrmutex_unlock
*
Expand Down Expand Up @@ -471,19 +590,21 @@ int nxrmutex_unlock(FAR rmutex_t *rmutex);
* rmutex - rmutex descriptor.
*
****************************************************************************/
#if defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__)

void nxrmutex_reset(FAR rmutex_t *rmutex);
#endif

/****************************************************************************
* Name: nrxmutex_breaklock
*
* Description:
* This function attempts to break the recursive mutex
*
* Parameters:
* Input Parameters:
* rmutex - Recursive mutex descriptor.
*
* Output Parameters:
* count - Return the count value before break.
*
* Return 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
Expand All @@ -502,6 +623,7 @@ int nxrmutex_breaklock(FAR rmutex_t *rmutex, FAR unsigned int *count);
*
* Parameters:
* rmutex - Recursive mutex descriptor.
* count - Count after restore.
*
* Return Value:
* This is an internal OS interface and should not be used by applications.
Expand All @@ -513,6 +635,47 @@ int nxrmutex_breaklock(FAR rmutex_t *rmutex, FAR unsigned int *count);

int nxrmutex_restorelock(FAR rmutex_t *rmutex, unsigned int count);

/****************************************************************************
* Name: nxrmutex_set_protocol
*
* Description:
* Set mutex protocol attribute.
*
* Input Parameters:
* rmutex - Recursive mutex descriptor.
* protocol - The new protocol to use
*
* 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.
*
****************************************************************************/

#define nxrmutex_set_protocol(rmutex, protocol) \
nxmutex_set_protocol(&(rmutex)->mutex, protocol)

/****************************************************************************
* Name: nxrmutex_get_protocol
*
* Description:
* Return the value of the mutex protocol attribute.
*
* Input Parameters:
* rmutex - Recursive mutex descriptor.
* protocol - The user provided location in which to store the protocol
* value.
*
* 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.
*
****************************************************************************/

#define nxrmutex_get_protocol(rmutex, protocol) \
nxmutex_get_protocol(&(rmutex)->mutex, protocol)

#undef EXTERN
#ifdef __cplusplus
}
Expand Down
2 changes: 1 addition & 1 deletion include/semaphore.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ typedef struct sem_s sem_t;
{(c), 0, SEM_WAITLIST_INITIALIZER}
#endif

# define SEM_WAITLIST(sem) (&((sem)->waitlist))
#define SEM_WAITLIST(sem) (&((sem)->waitlist))

/****************************************************************************
* Public Data
Expand Down
9 changes: 9 additions & 0 deletions include/sys/syscall_lookup.h
Original file line number Diff line number Diff line change
Expand Up @@ -380,3 +380,12 @@ SYSCALL_LOOKUP(munmap, 2)
#ifdef CONFIG_CRYPTO_RANDOM_POOL
SYSCALL_LOOKUP(arc4random_buf, 2)
#endif

SYSCALL_LOOKUP(nxmutex_breaklock, 2)
SYSCALL_LOOKUP(nxmutex_clocklock, 3)
SYSCALL_LOOKUP(nxmutex_destroy, 1)
SYSCALL_LOOKUP(nxmutex_lock, 1)
SYSCALL_LOOKUP(nxmutex_set_protocol, 2)
SYSCALL_LOOKUP(nxmutex_timedlock, 2)
SYSCALL_LOOKUP(nxmutex_trylock, 1)
SYSCALL_LOOKUP(nxmutex_unlock, 1)
1 change: 1 addition & 0 deletions libs/libc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ include lzf/Make.defs
include machine/Make.defs
include misc/Make.defs
include modlib/Make.defs
include mutex/Make.defs
include net/Make.defs
include netdb/Make.defs
include obstack/Make.defs
Expand Down
2 changes: 1 addition & 1 deletion libs/libc/misc/Make.defs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

CSRCS += lib_mknod.c lib_umask.c lib_utsname.c lib_getrandom.c
CSRCS += lib_xorshift128.c lib_tea_encrypt.c lib_tea_decrypt.c
CSRCS += lib_cxx_initialize.c lib_impure.c lib_memfd.c lib_mutex.c
CSRCS += lib_cxx_initialize.c lib_impure.c lib_memfd.c
CSRCS += lib_fchmodat.c lib_fstatat.c lib_getfullpath.c lib_openat.c
CSRCS += lib_mkdirat.c lib_utimensat.c

Expand Down
Loading