原始版本
This commit is contained in:
9
RT_Thread/components/libc/posix/pthreads/SConscript
Normal file
9
RT_Thread/components/libc/posix/pthreads/SConscript
Normal file
@ -0,0 +1,9 @@
|
||||
from building import *
|
||||
|
||||
cwd = GetCurrentDir()
|
||||
src = Glob('*.c')
|
||||
CPPPATH = [cwd]
|
||||
|
||||
group = DefineGroup('POSIX', src, depend = ['RT_USING_PTHREADS'], CPPPATH = CPPPATH)
|
||||
|
||||
Return('group')
|
||||
26
RT_Thread/components/libc/posix/pthreads/posix_types.h
Normal file
26
RT_Thread/components/libc/posix/pthreads/posix_types.h
Normal file
@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2021, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2013-12-23 Bernard Add the checking for ESHUTDOWN
|
||||
*/
|
||||
|
||||
#ifndef __POSIX_TYPES_H__
|
||||
#define __POSIX_TYPES_H__
|
||||
|
||||
#include <rtthread.h>
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/time.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/errno.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#endif
|
||||
1368
RT_Thread/components/libc/posix/pthreads/pthread.c
Normal file
1368
RT_Thread/components/libc/posix/pthreads/pthread.c
Normal file
File diff suppressed because it is too large
Load Diff
296
RT_Thread/components/libc/posix/pthreads/pthread.h
Normal file
296
RT_Thread/components/libc/posix/pthreads/pthread.h
Normal file
@ -0,0 +1,296 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2021, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2010-10-26 Bernard the first version
|
||||
*/
|
||||
|
||||
#ifndef __PTHREAD_H__
|
||||
#define __PTHREAD_H__
|
||||
|
||||
#include <rtthread.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <posix_types.h>
|
||||
#include <sched.h>
|
||||
|
||||
#define PTHREAD_KEY_MAX 8
|
||||
|
||||
#define PTHREAD_COND_INITIALIZER {-1}
|
||||
#define PTHREAD_RWLOCK_INITIALIZER {-1}
|
||||
#define PTHREAD_MUTEX_INITIALIZER {-1}
|
||||
|
||||
#define PTHREAD_CREATE_JOINABLE 0x00
|
||||
#define PTHREAD_CREATE_DETACHED 0x01
|
||||
|
||||
#define PTHREAD_EXPLICIT_SCHED 0
|
||||
#define PTHREAD_INHERIT_SCHED 1
|
||||
|
||||
typedef long pthread_t;
|
||||
typedef long pthread_condattr_t;
|
||||
typedef long pthread_rwlockattr_t;
|
||||
typedef long pthread_mutexattr_t;
|
||||
typedef long pthread_barrierattr_t;
|
||||
|
||||
typedef int pthread_key_t;
|
||||
typedef int pthread_once_t;
|
||||
|
||||
enum
|
||||
{
|
||||
PTHREAD_CANCEL_ASYNCHRONOUS = 0,
|
||||
PTHREAD_CANCEL_ENABLE,
|
||||
PTHREAD_CANCEL_DEFERRED,
|
||||
PTHREAD_CANCEL_DISABLE,
|
||||
PTHREAD_CANCELED
|
||||
};
|
||||
|
||||
enum
|
||||
{
|
||||
PTHREAD_MUTEX_NORMAL = 0,
|
||||
PTHREAD_MUTEX_RECURSIVE = 1,
|
||||
PTHREAD_MUTEX_ERRORCHECK = 2,
|
||||
PTHREAD_MUTEX_ERRORCHECK_NP = PTHREAD_MUTEX_ERRORCHECK,
|
||||
PTHREAD_MUTEX_RECURSIVE_NP = PTHREAD_MUTEX_RECURSIVE,
|
||||
PTHREAD_MUTEX_DEFAULT = PTHREAD_MUTEX_NORMAL
|
||||
};
|
||||
|
||||
/* init value for pthread_once_t */
|
||||
#define PTHREAD_ONCE_INIT 0
|
||||
|
||||
enum
|
||||
{
|
||||
PTHREAD_PRIO_INHERIT =0,
|
||||
PTHREAD_PRIO_NONE,
|
||||
PTHREAD_PRIO_PROTECT,
|
||||
};
|
||||
|
||||
#define PTHREAD_PROCESS_PRIVATE 0
|
||||
#define PTHREAD_PROCESS_SHARED 1
|
||||
|
||||
#define PTHREAD_SCOPE_PROCESS 0
|
||||
#define PTHREAD_SCOPE_SYSTEM 1
|
||||
|
||||
struct sched_param
|
||||
{
|
||||
int sched_priority;
|
||||
};
|
||||
|
||||
struct pthread_attr
|
||||
{
|
||||
void* stackaddr; /* stack address of thread */
|
||||
int stacksize; /* stack size of thread */
|
||||
|
||||
int inheritsched; /* Inherit parent prio/policy */
|
||||
int schedpolicy; /* scheduler policy */
|
||||
struct sched_param schedparam; /* sched parameter */
|
||||
|
||||
int detachstate; /* detach state */
|
||||
};
|
||||
typedef struct pthread_attr pthread_attr_t;
|
||||
|
||||
struct pthread_mutex
|
||||
{
|
||||
pthread_mutexattr_t attr;
|
||||
struct rt_mutex lock;
|
||||
};
|
||||
typedef struct pthread_mutex pthread_mutex_t;
|
||||
|
||||
struct pthread_cond
|
||||
{
|
||||
pthread_condattr_t attr;
|
||||
struct rt_semaphore sem;
|
||||
};
|
||||
typedef struct pthread_cond pthread_cond_t;
|
||||
|
||||
struct pthread_rwlock
|
||||
{
|
||||
pthread_rwlockattr_t attr;
|
||||
|
||||
pthread_mutex_t rw_mutex; /* basic lock on this struct */
|
||||
pthread_cond_t rw_condreaders; /* for reader threads waiting */
|
||||
pthread_cond_t rw_condwriters; /* for writer threads waiting */
|
||||
|
||||
int rw_nwaitreaders; /* the number of reader threads waiting */
|
||||
int rw_nwaitwriters; /* the number of writer threads waiting */
|
||||
int rw_refcount; /* 0: unlocked, -1: locked by writer, > 0 locked by n readers */
|
||||
};
|
||||
typedef struct pthread_rwlock pthread_rwlock_t;
|
||||
|
||||
/* spinlock implementation, (ADVANCED REALTIME THREADS)*/
|
||||
struct pthread_spinlock
|
||||
{
|
||||
int lock;
|
||||
};
|
||||
typedef struct pthread_spinlock pthread_spinlock_t;
|
||||
|
||||
struct pthread_barrier
|
||||
{
|
||||
int count;
|
||||
pthread_cond_t cond;
|
||||
pthread_mutex_t mutex;
|
||||
};
|
||||
typedef struct pthread_barrier pthread_barrier_t;
|
||||
|
||||
/* pthread thread interface */
|
||||
int pthread_attr_destroy(pthread_attr_t *attr);
|
||||
int pthread_attr_init(pthread_attr_t *attr);
|
||||
int pthread_attr_setdetachstate(pthread_attr_t *attr, int state);
|
||||
int pthread_attr_getdetachstate(pthread_attr_t const *attr, int *state);
|
||||
int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);
|
||||
int pthread_attr_getschedpolicy(pthread_attr_t const *attr, int *policy);
|
||||
int pthread_attr_setschedparam(pthread_attr_t *attr,struct sched_param const *param);
|
||||
int pthread_attr_getschedparam(pthread_attr_t const *attr,struct sched_param *param);
|
||||
int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stack_size);
|
||||
int pthread_attr_getstacksize(pthread_attr_t const *attr, size_t *stack_size);
|
||||
int pthread_attr_setstackaddr(pthread_attr_t *attr, void *stack_addr);
|
||||
int pthread_attr_getstackaddr(pthread_attr_t const *attr, void **stack_addr);
|
||||
int pthread_attr_setstack(pthread_attr_t *attr,
|
||||
void *stack_base,
|
||||
size_t stack_size);
|
||||
int pthread_attr_getstack(pthread_attr_t const *attr,
|
||||
void **stack_base,
|
||||
size_t *stack_size);
|
||||
int pthread_attr_setguardsize(pthread_attr_t *attr, size_t guard_size);
|
||||
int pthread_attr_getguardsize(pthread_attr_t const *attr, size_t *guard_size);
|
||||
int pthread_attr_setinheritsched(pthread_attr_t *attr, int inheritsched);
|
||||
int pthread_attr_getinheritsched(const pthread_attr_t *attr, int *inheritsched);
|
||||
int pthread_attr_setscope(pthread_attr_t *attr, int scope);
|
||||
int pthread_attr_getscope(pthread_attr_t const *attr, int *scope);
|
||||
int pthread_create (pthread_t *tid, const pthread_attr_t *attr,
|
||||
void *(*start) (void *), void *arg);
|
||||
|
||||
int pthread_detach (pthread_t thread);
|
||||
int pthread_join (pthread_t thread, void **value_ptr);
|
||||
|
||||
rt_inline int pthread_equal (pthread_t t1, pthread_t t2)
|
||||
{
|
||||
return t1 == t2;
|
||||
}
|
||||
|
||||
pthread_t pthread_self (void);
|
||||
|
||||
int pthread_getcpuclockid(pthread_t thread, clockid_t *clock_id);
|
||||
int pthread_getconcurrency(void);
|
||||
int pthread_setconcurrency(int new_level);
|
||||
int pthread_getschedparam(pthread_t thread, int *policy, struct sched_param *param);
|
||||
int pthread_setschedparam(pthread_t thread, int policy, const struct sched_param *param);
|
||||
int pthread_setschedprio(pthread_t thread, int prio);
|
||||
|
||||
void pthread_exit (void *value_ptr);
|
||||
int pthread_once(pthread_once_t * once_control, void (*init_routine) (void));
|
||||
|
||||
#ifdef RT_USING_SIGNALS
|
||||
int pthread_sigmask(int how, const sigset_t *set, sigset_t *oset);
|
||||
#endif
|
||||
|
||||
/* pthread cleanup */
|
||||
void pthread_cleanup_pop(int execute);
|
||||
void pthread_cleanup_push(void (*routine)(void*), void *arg);
|
||||
|
||||
/* pthread cancel */
|
||||
int pthread_cancel(pthread_t thread);
|
||||
void pthread_testcancel(void);
|
||||
int pthread_setcancelstate(int state, int *oldstate);
|
||||
int pthread_setcanceltype(int type, int *oldtype);
|
||||
|
||||
int pthread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void));
|
||||
int pthread_kill(pthread_t thread, int sig);
|
||||
|
||||
/* pthread mutex interface */
|
||||
int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr);
|
||||
int pthread_mutex_destroy(pthread_mutex_t *mutex);
|
||||
int pthread_mutex_lock(pthread_mutex_t *mutex);
|
||||
int pthread_mutex_unlock(pthread_mutex_t *mutex);
|
||||
int pthread_mutex_trylock(pthread_mutex_t *mutex);
|
||||
int pthread_mutex_getprioceiling(const pthread_mutex_t *mutex, int *prioceiling);
|
||||
int pthread_mutex_setprioceiling(pthread_mutex_t *mutex, int prioceiling, int *old_ceiling);
|
||||
|
||||
int pthread_mutexattr_init(pthread_mutexattr_t *attr);
|
||||
int pthread_mutexattr_destroy(pthread_mutexattr_t *attr);
|
||||
int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type);
|
||||
int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type);
|
||||
int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared);
|
||||
int pthread_mutexattr_getpshared(pthread_mutexattr_t *attr, int *pshared);
|
||||
int pthread_mutexattr_getprioceiling(const pthread_mutexattr_t *attr, int *prioceiling);
|
||||
int pthread_mutexattr_setprioceiling(const pthread_mutexattr_t *attr, int prioceiling);
|
||||
int pthread_mutexattr_getprotocol(const pthread_mutexattr_t *attr, int *protocol);
|
||||
int pthread_mutexattr_setprotocol(const pthread_mutexattr_t *attr, int protocol);
|
||||
|
||||
|
||||
/* pthread condition interface */
|
||||
int pthread_condattr_destroy(pthread_condattr_t *attr);
|
||||
int pthread_condattr_init(pthread_condattr_t *attr);
|
||||
|
||||
/* ADVANCED REALTIME feature in IEEE Std 1003.1, 2004 Edition */
|
||||
int pthread_condattr_getclock(const pthread_condattr_t *attr,
|
||||
clockid_t *clock_id);
|
||||
int pthread_condattr_setclock(pthread_condattr_t *attr,
|
||||
clockid_t clock_id);
|
||||
|
||||
int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr);
|
||||
int pthread_cond_destroy(pthread_cond_t *cond);
|
||||
int pthread_cond_broadcast(pthread_cond_t *cond);
|
||||
int pthread_cond_signal(pthread_cond_t *cond);
|
||||
|
||||
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
|
||||
int pthread_cond_timedwait(pthread_cond_t *cond,
|
||||
pthread_mutex_t *mutex,
|
||||
const struct timespec *abstime);
|
||||
|
||||
/* pthread rwlock interface */
|
||||
int pthread_rwlockattr_init (pthread_rwlockattr_t *attr);
|
||||
int pthread_rwlockattr_destroy (pthread_rwlockattr_t *attr);
|
||||
int pthread_rwlockattr_getpshared (const pthread_rwlockattr_t *attr, int *pshared);
|
||||
int pthread_rwlockattr_setpshared (pthread_rwlockattr_t *attr, int pshared);
|
||||
|
||||
int pthread_rwlock_init (pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr);
|
||||
int pthread_rwlock_destroy (pthread_rwlock_t *rwlock);
|
||||
|
||||
int pthread_rwlock_rdlock (pthread_rwlock_t *rwlock);
|
||||
int pthread_rwlock_tryrdlock (pthread_rwlock_t *rwlock);
|
||||
|
||||
int pthread_rwlock_timedrdlock (pthread_rwlock_t *rwlock, const struct timespec *abstime);
|
||||
int pthread_rwlock_timedwrlock (pthread_rwlock_t *rwlock, const struct timespec *abstime);
|
||||
|
||||
int pthread_rwlock_unlock (pthread_rwlock_t *rwlock);
|
||||
|
||||
int pthread_rwlock_wrlock (pthread_rwlock_t *rwlock);
|
||||
int pthread_rwlock_trywrlock (pthread_rwlock_t *rwlock);
|
||||
|
||||
/* pthread spinlock interface */
|
||||
int pthread_spin_init (pthread_spinlock_t *lock, int pshared);
|
||||
int pthread_spin_destroy (pthread_spinlock_t *lock);
|
||||
|
||||
int pthread_spin_lock (pthread_spinlock_t * lock);
|
||||
int pthread_spin_trylock (pthread_spinlock_t * lock);
|
||||
int pthread_spin_unlock (pthread_spinlock_t * lock);
|
||||
|
||||
/* pthread barrier interface */
|
||||
int pthread_barrierattr_destroy(pthread_barrierattr_t *attr);
|
||||
int pthread_barrierattr_init(pthread_barrierattr_t *attr);
|
||||
int pthread_barrierattr_getpshared(const pthread_barrierattr_t *attr, int *pshared);
|
||||
int pthread_barrierattr_setpshared(pthread_barrierattr_t *attr, int pshared);
|
||||
|
||||
int pthread_barrier_destroy(pthread_barrier_t *barrier);
|
||||
int pthread_barrier_init(pthread_barrier_t *barrier,
|
||||
const pthread_barrierattr_t *attr,
|
||||
unsigned count);
|
||||
|
||||
int pthread_barrier_wait(pthread_barrier_t *barrier);
|
||||
|
||||
int pthread_setspecific(pthread_key_t key, const void *value);
|
||||
void *pthread_getspecific(pthread_key_t key);
|
||||
int pthread_key_create(pthread_key_t *key, void (*destructor)(void *));
|
||||
int pthread_key_delete(pthread_key_t key);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
615
RT_Thread/components/libc/posix/pthreads/pthread_attr.c
Normal file
615
RT_Thread/components/libc/posix/pthreads/pthread_attr.c
Normal file
@ -0,0 +1,615 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2010-10-26 Bernard the first version
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include "pthread.h"
|
||||
#include "sched.h"
|
||||
#include <string.h>
|
||||
|
||||
#define DEFAULT_STACK_SIZE 2048
|
||||
#define DEFAULT_PRIORITY (RT_THREAD_PRIORITY_MAX/2 + RT_THREAD_PRIORITY_MAX/4)
|
||||
|
||||
const pthread_attr_t pthread_default_attr =
|
||||
{
|
||||
0, /* stack base */
|
||||
DEFAULT_STACK_SIZE, /* stack size */
|
||||
|
||||
PTHREAD_INHERIT_SCHED, /* Inherit parent prio/policy */
|
||||
SCHED_FIFO, /* scheduler policy */
|
||||
{
|
||||
DEFAULT_PRIORITY, /* scheduler priority */
|
||||
},
|
||||
PTHREAD_CREATE_JOINABLE, /* detach state */
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief This function will initialize thread attributes object.
|
||||
*
|
||||
* @note The pthread_attr_t type should be treated as opaque: any access to the object other
|
||||
* than via pthreads functions is nonportable and produces undefined results.
|
||||
* The resulting attribute object (possibly modified by setting individual attribute values),
|
||||
* when used by pthread_create(), defines the attributes of the thread created. A single attributes
|
||||
* object can be used in multiple simultaneous calls to pthread_create().
|
||||
*
|
||||
* @see pthread_create()
|
||||
*
|
||||
* @param attr is a thread attributes object.
|
||||
*
|
||||
* @return Upon successful completion, pthread_attr_init() return a value of 0.
|
||||
* Otherwise, it means that the event detach failed.
|
||||
*
|
||||
* @warning This function will fail if attr is null.
|
||||
*/
|
||||
int pthread_attr_init(pthread_attr_t *attr)
|
||||
{
|
||||
RT_ASSERT(attr != RT_NULL);
|
||||
|
||||
*attr = pthread_default_attr;
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(pthread_attr_init);
|
||||
|
||||
/**
|
||||
* @brief This function will destroy thread attributes object.
|
||||
*
|
||||
* @note When a thread attributes object is no longer required, it should be destroyed
|
||||
* using the pthread_attr_destroy() function. Destroying a thread attributes object
|
||||
* has no effect on threads that were created using that object.
|
||||
* Once a thread attributes object has been destroyed, it can be reinitialized using pthread_attr_init().
|
||||
* Any other use of a destroyed thread attributes object has undefined results.
|
||||
*
|
||||
* @see pthread_attr_init(), pthread_attr_getdetachstate(), pthread_create()
|
||||
*
|
||||
* @param attr is a thread attributes object.
|
||||
*
|
||||
* @return Upon successful completion, pthread_attr_destroy() and shall return a value of 0;
|
||||
* Otherwise, an error number shall be returned to indicate the error.
|
||||
*
|
||||
* @warning This function will fail if attr is null.
|
||||
*/
|
||||
int pthread_attr_destroy(pthread_attr_t *attr)
|
||||
{
|
||||
RT_ASSERT(attr != RT_NULL);
|
||||
|
||||
memset(attr, 0, sizeof(pthread_attr_t));
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(pthread_attr_destroy);
|
||||
|
||||
/**
|
||||
* @brief This function set detach state attribute in thread attributes object.
|
||||
*
|
||||
* @note This function sets the detach state attribute of the thread attributes object
|
||||
* referred to by attr to the value specified in detachstate. The detach state
|
||||
* attribute determines whether a thread created using the thread attributes
|
||||
* object attr will be created in a joinable or a detached state.
|
||||
*
|
||||
* @see pthread_attr_init(), pthread_create(), pthread_detach(), pthread_join(), pthreads()
|
||||
*
|
||||
* @param attr is a thread attributes object.
|
||||
*
|
||||
* @param state is attribute in the attr object.
|
||||
* attribute controls whether the thread is created in a detached state.
|
||||
* The detachstate can be ONE of the following values:
|
||||
*
|
||||
* PTHREAD_CREATE_DETACHED It causes all threads created with attr to be in the detached state.
|
||||
*
|
||||
* PTHREAD_CREATE_JOINABLE Default value, it causes all threads created with attr to be in the joinable state.
|
||||
*
|
||||
* @return Upon successful completion, pthread_attr_setdetachstate() and return a value of 0.
|
||||
* Otherwise, an error number is returned to indicate the error.
|
||||
*
|
||||
* @warning The pthread_attr_setdetachstate() function will fail if:
|
||||
* [EINVAL]
|
||||
* The value of detach state was not valid
|
||||
*/
|
||||
int pthread_attr_setdetachstate(pthread_attr_t *attr, int state)
|
||||
{
|
||||
RT_ASSERT(attr != RT_NULL);
|
||||
|
||||
if (state != PTHREAD_CREATE_JOINABLE && state != PTHREAD_CREATE_DETACHED)
|
||||
return EINVAL;
|
||||
|
||||
attr->detachstate = state;
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(pthread_attr_setdetachstate);
|
||||
|
||||
/**
|
||||
* @brief This function get detach state attribute in thread attributes object.
|
||||
*
|
||||
* @note The detachstate attribute controls whether the thread is created in a detached state.
|
||||
* If the thread is created detached, then use of the ID of the newly created thread by
|
||||
* the pthread_detach() or pthread_join() function is an error.
|
||||
*
|
||||
* @see pthread_attr_destroy(), pthread_attr_getstackaddr(), pthread_attr_getstacksize(), pthread_create()
|
||||
*
|
||||
* @param attr is a thread attributes object.
|
||||
*
|
||||
* @param state is attribute in the attr object.
|
||||
* attribute controls whether the thread is created in a detached state.
|
||||
* The detachstate can be ONE of the following values:
|
||||
*
|
||||
* PTHREAD_CREATE_DETACHED It causes all threads created with attr to be in the detached state.
|
||||
*
|
||||
* PTHREAD_CREATE_JOINABLE Default value, it causes all threads created with attr to be in the joinable state.
|
||||
*
|
||||
* @return Upon successful completion, pthread_attr_getdetachstate() and shall return a value of 0;
|
||||
* otherwise, an error number shall be returned to indicate the error.
|
||||
*
|
||||
* The pthread_attr_getdetachstate() function stores the value of the detachstate
|
||||
* attribute in detachstate if successful.
|
||||
*/
|
||||
int pthread_attr_getdetachstate(pthread_attr_t const *attr, int *state)
|
||||
{
|
||||
RT_ASSERT(attr != RT_NULL);
|
||||
|
||||
*state = (int)attr->detachstate;
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(pthread_attr_getdetachstate);
|
||||
|
||||
/**
|
||||
* @brief This function sets schedpolicy attribute.
|
||||
*
|
||||
* @note The function function sets the scheduling policy attribute of the thread
|
||||
* attributes object referred to by attr to the value specified in policy.
|
||||
*
|
||||
* @see pthread_attr_init(), pthread_attr_setscope(), pthread_attr_setinheritsched(), pthread_attr_setschedparam(), pthread_create()
|
||||
*
|
||||
* @param attr is a thread attributes object.
|
||||
*
|
||||
* @param policy is attribute in the attr object.
|
||||
* The policy can be ONE of the following values:
|
||||
*
|
||||
* SCHED_FIFO First in-first out scheduling.
|
||||
*
|
||||
* SCHED_RR Round-robin scheduling.
|
||||
*
|
||||
* SCHED_OTHER Default Linux time-sharing scheduling.
|
||||
*
|
||||
* @return On success, these functions return 0.
|
||||
*/
|
||||
int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
|
||||
{
|
||||
RT_ASSERT(attr != RT_NULL);
|
||||
|
||||
attr->schedpolicy = policy;
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(pthread_attr_setschedpolicy);
|
||||
|
||||
/**
|
||||
* @brief This function gets schedpolicy attribute.
|
||||
*
|
||||
* @note The function gets the schedpolicy attribute in the attr argument.
|
||||
*
|
||||
* @see pthread_attr_destroy(), pthread_attr_getscope(), pthread_attr_getinheritsched(), pthread_attr_getschedparam(), pthread_create()
|
||||
*
|
||||
* @param attr is a thread attributes object.
|
||||
*
|
||||
* @param policy is attribute in the attr object.
|
||||
* The policy can be ONE of the following values:
|
||||
*
|
||||
* SCHED_FIFO First in-first out scheduling.
|
||||
*
|
||||
* SCHED_RR Round-robin scheduling.
|
||||
*
|
||||
* SCHED_OTHER Default Linux time-sharing scheduling.
|
||||
*
|
||||
* @return On success, these functions return 0.
|
||||
*/
|
||||
int pthread_attr_getschedpolicy(pthread_attr_t const *attr, int *policy)
|
||||
{
|
||||
RT_ASSERT(attr != RT_NULL);
|
||||
|
||||
*policy = (int)attr->schedpolicy;
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(pthread_attr_getschedpolicy);
|
||||
|
||||
/**
|
||||
* @brief This function set the scheduling parameter attributes in the attr argument.
|
||||
|
||||
* @see pthread_attr_init(), pthread_attr_setscope(), pthread_attr_setinheritsched(), pthread_attr_setschedpolicy()
|
||||
*
|
||||
* @param attr is a thread attributes object.
|
||||
*
|
||||
* @param param is scheduling parameter attributes in the attr argument.
|
||||
* The contents of the param structure are defined in <pthread.h>.
|
||||
* For the SCHED_FIFO and SCHED_RR policies, the only required member of param is sched_priority.
|
||||
*
|
||||
* @return On success, these functions return 0.
|
||||
*/
|
||||
int pthread_attr_setschedparam(pthread_attr_t *attr,
|
||||
struct sched_param const *param)
|
||||
{
|
||||
RT_ASSERT(attr != RT_NULL);
|
||||
RT_ASSERT(param != RT_NULL);
|
||||
|
||||
attr->schedparam.sched_priority = param->sched_priority;
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(pthread_attr_setschedparam);
|
||||
|
||||
/**
|
||||
* @brief This function get the scheduling parameter attributes in the attr argument.
|
||||
|
||||
* @see pthread_attr_init(), pthread_attr_setscope(), pthread_attr_setinheritsched(), pthread_attr_setschedpolicy()
|
||||
*
|
||||
* @param attr is a thread attributes object.
|
||||
*
|
||||
* @param param is scheduling parameter attributes in the attr argument.
|
||||
* The contents of the param structure are defined in <pthread.h>.
|
||||
* For the SCHED_FIFO and SCHED_RR policies, the only required member of param is sched_priority.
|
||||
*
|
||||
* @return On success, these functions return 0.
|
||||
*/
|
||||
int pthread_attr_getschedparam(pthread_attr_t const *attr,
|
||||
struct sched_param *param)
|
||||
{
|
||||
RT_ASSERT(attr != RT_NULL);
|
||||
RT_ASSERT(param != RT_NULL);
|
||||
|
||||
param->sched_priority = attr->schedparam.sched_priority;
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(pthread_attr_getschedparam);
|
||||
|
||||
/**
|
||||
* @brief This function set the thread creation stacksize attribute in the attr object.
|
||||
*
|
||||
* @see pthread_attr_init(), pthread_attr_setstackaddr(), pthread_attr_setdetachstate()
|
||||
*
|
||||
* @param attr is a thread attributes object.
|
||||
*
|
||||
* @param stack_size is the minimum stack size (in bytes) allocated for the created threads stack.
|
||||
*
|
||||
* @return Upon successful completion, This function return a value of 0.
|
||||
*/
|
||||
int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stack_size)
|
||||
{
|
||||
RT_ASSERT(attr != RT_NULL);
|
||||
|
||||
attr->stacksize = stack_size;
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(pthread_attr_setstacksize);
|
||||
|
||||
/**
|
||||
* @brief This function get the thread creation stacksize attribute in the attr object.
|
||||
*
|
||||
* @see pthread_attr_init(), pthread_attr_getstackaddr(), pthread_attr_getdetachstate()
|
||||
*
|
||||
* @param attr is a thread attributes object.
|
||||
*
|
||||
* @param stack_size is the minimum stack size (in bytes) allocated for the created threads stack.
|
||||
*
|
||||
* @return Upon successful completion, This function return a value of 0.
|
||||
*/
|
||||
int pthread_attr_getstacksize(pthread_attr_t const *attr, size_t *stack_size)
|
||||
{
|
||||
RT_ASSERT(attr != RT_NULL);
|
||||
|
||||
*stack_size = attr->stacksize;
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(pthread_attr_getstacksize);
|
||||
|
||||
/**
|
||||
* @brief This function sets the thread creation stackaddr attribute in the attr object.
|
||||
*
|
||||
* @see pthread_attr_init(), pthread_attr_setdetachstate(), pthread_attr_setstacksize()
|
||||
*
|
||||
* @param attr is a thread attributes object.
|
||||
*
|
||||
* @param The stack_addr attribute specifies the location of storage to be used for the created
|
||||
* thread's stack.
|
||||
*
|
||||
* @return Upon successful completion, This function return a value of 0.
|
||||
*/
|
||||
int pthread_attr_setstackaddr(pthread_attr_t *attr, void *stack_addr)
|
||||
{
|
||||
RT_ASSERT(attr != RT_NULL);
|
||||
|
||||
return EOPNOTSUPP;
|
||||
}
|
||||
RTM_EXPORT(pthread_attr_setstackaddr);
|
||||
|
||||
/**
|
||||
* @brief This function gets the thread creation stackaddr attribute in the attr object.
|
||||
*
|
||||
* @see pthread_attr_init(), pthread_attr_setdetachstate(), pthread_attr_setstacksize()
|
||||
*
|
||||
* @param attr is a thread attributes object.
|
||||
*
|
||||
* @param The stack_addr attribute specifies the location of storage to be used for the created
|
||||
* thread's stack.
|
||||
*
|
||||
* @return Upon successful completion, This function return a value of 0.
|
||||
*/
|
||||
int pthread_attr_getstackaddr(pthread_attr_t const *attr, void **stack_addr)
|
||||
{
|
||||
RT_ASSERT(attr != RT_NULL);
|
||||
|
||||
return EOPNOTSUPP;
|
||||
}
|
||||
RTM_EXPORT(pthread_attr_getstackaddr);
|
||||
|
||||
/**
|
||||
* @brief This function set the thread creation stack attributes stackaddr and stacksize in the attr object.
|
||||
*
|
||||
* @note The stack attributes specify the area of storage to be used for the created thread's stack.
|
||||
* The base (lowest addressable byte) of the storage shall be stack_base, and the size of the storage
|
||||
* shall be stack_size bytes.
|
||||
* All pages within the stack described by stackaddr and stacksize shall be both readable
|
||||
* and writable by the thread.
|
||||
*
|
||||
* @see pthread_attr_destroy, pthread_attr_getdetachstate, pthread_attr_getstacksize, pthread_create
|
||||
*
|
||||
* @param attr is a thread attributes object.
|
||||
*
|
||||
* @param stack_base is the base (lowest addressable byte) of the storage.
|
||||
*
|
||||
* @param stack_size is the size of the storage.
|
||||
*
|
||||
* @return Upon successful completion, these functions shall return a value of 0;
|
||||
* otherwise, an error number shall be returned to indicate the error.
|
||||
*
|
||||
* @warning The behavior is undefined if the value specified by the attr argument to or pthread_attr_setstack()
|
||||
* does not refer to an initialized thread attributes object.
|
||||
*/
|
||||
int pthread_attr_setstack(pthread_attr_t *attr,
|
||||
void *stack_base,
|
||||
size_t stack_size)
|
||||
{
|
||||
RT_ASSERT(attr != RT_NULL);
|
||||
|
||||
attr->stackaddr = stack_base;
|
||||
attr->stacksize = RT_ALIGN_DOWN(stack_size, RT_ALIGN_SIZE);
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(pthread_attr_setstack);
|
||||
|
||||
/**
|
||||
* @brief This function shall get the thread creation stack attributes stackaddr and stacksize in the attr object.
|
||||
*
|
||||
* @note The stack attributes specify the area of storage to be used for the created thread's stack.
|
||||
* The base (lowest addressable byte) of the storage shall be stack_base, and the size of the storage
|
||||
* shall be stack_size bytes.
|
||||
* All pages within the stack described by stack_base and stack_size shall be both readable
|
||||
* and writable by the thread.
|
||||
*
|
||||
* @see pthread_attr_destroy, pthread_attr_getdetachstate, pthread_attr_getstacksize, pthread_create
|
||||
*
|
||||
* @param attr is a thread attributes object.
|
||||
*
|
||||
* @param stack_base is the base (lowest addressable byte) of the storage.
|
||||
*
|
||||
* @param stack_size is the size of the storage.
|
||||
*
|
||||
* @return Upon successful completion, these functions shall return a value of 0;
|
||||
* otherwise, an error number shall be returned to indicate the error.
|
||||
* This function shall store the stack attribute values in stack_base and stack_size if successful.
|
||||
*/
|
||||
int pthread_attr_getstack(pthread_attr_t const *attr,
|
||||
void **stack_base,
|
||||
size_t *stack_size)
|
||||
{
|
||||
RT_ASSERT(attr != RT_NULL);
|
||||
|
||||
*stack_base = attr->stackaddr;
|
||||
*stack_size = attr->stacksize;
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(pthread_attr_getstack);
|
||||
|
||||
/**
|
||||
* @brief This function shall set the guardsize attribute in the attr object.
|
||||
*
|
||||
* @note The guardsize attribute controls the size of the guard area for the created thread's stack.
|
||||
* The guardsize attribute provides protection against overflow of the stack pointer.
|
||||
* If a thread's stack is created with guard protection, the implementation allocates extra
|
||||
* memory at the overflow end of the stack as a buffer against stack overflow of the stack pointer.
|
||||
* If an application overflows into this buffer an error shall result (possibly in a SIGSEGV signal
|
||||
* being delivered to the thread).
|
||||
*
|
||||
* @see <pthread.h>, <sys/mman.h>
|
||||
*
|
||||
* @param attr is a thread attributes object.
|
||||
*
|
||||
* @param guard_size is the size of the guard area for the created thread's stack.
|
||||
*
|
||||
* @return Upon successful completion, these functions shall return a value of 0;
|
||||
*
|
||||
* @warning The guardsize attribute is provided to the application for two reasons:
|
||||
*
|
||||
* 1. Overflow protection can potentially result in wasted system resources.
|
||||
* An application that creates a large number of threads, and which knows its threads
|
||||
* never overflow their stack, can save system resources by turning off guard areas.
|
||||
*
|
||||
* 2. When threads allocate large data structures on the stack, large guard areas may be
|
||||
* needed to detect stack overflow.
|
||||
*
|
||||
* The default size of the guard area is left implementation-defined since on systems
|
||||
* supporting very large page sizes, the overhead might be substantial if at least one guard
|
||||
* page is required by default.
|
||||
*/
|
||||
int pthread_attr_setguardsize(pthread_attr_t *attr, size_t guard_size)
|
||||
{
|
||||
return EOPNOTSUPP;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function get the guardsize attribute in the attr object.
|
||||
* This attribute shall be returned in the guard_size parameter.
|
||||
*
|
||||
* @note The guardsize attribute controls the size of the guard area for the created thread's stack.
|
||||
* The guardsize attribute provides protection against overflow of the stack pointer.
|
||||
* If a thread's stack is created with guard protection, the implementation allocates extra
|
||||
* memory at the overflow end of the stack as a buffer against stack overflow of the stack pointer.
|
||||
*
|
||||
* @see <pthread.h>, <sys/mman.h>
|
||||
*
|
||||
* @param attr is a thread attributes object.
|
||||
*
|
||||
* @param guard_size is the size of the guard area for the created thread's stack.
|
||||
*
|
||||
* @return Upon successful completion, these functions shall return a value of 0;
|
||||
*
|
||||
* @warning The guardsize attribute is provided to the application for two reasons:
|
||||
*
|
||||
* 1. Overflow protection can potentially result in wasted system resources.
|
||||
* An application that creates a large number of threads, and which knows its threads
|
||||
* never overflow their stack, can save system resources by turning off guard areas.
|
||||
*
|
||||
* 2. When threads allocate large data structures on the stack, large guard areas may be
|
||||
* needed to detect stack overflow.
|
||||
*
|
||||
* The default size of the guard area is left implementation-defined since on systems
|
||||
* supporting very large page sizes, the overhead might be substantial if at least one guard
|
||||
* page is required by default.
|
||||
*/
|
||||
int pthread_attr_getguardsize(pthread_attr_t const *attr, size_t *guard_size)
|
||||
{
|
||||
return EOPNOTSUPP;
|
||||
}
|
||||
RTM_EXPORT(pthread_attr_getguardsize);
|
||||
|
||||
/**
|
||||
* @brief This function sets inherit-scheduler attribute in thread attributes object.
|
||||
*
|
||||
* @note The function sets the inherit-scheduler attribute of the thread attributes object
|
||||
* referred to by attr to the value specified in inheritsched.
|
||||
* The inherit-scheduler attribute determines whether a thread created using the thread
|
||||
* attributes object attr will inherit its scheduling attributes from the calling thread
|
||||
* or whether it will take them from attr.
|
||||
*
|
||||
* @see pthread_attr_init(), pthread_attr_setschedpolicy(), pthread_attr_setschedparam()
|
||||
*
|
||||
* @param attr is a thread attributes object.
|
||||
*
|
||||
* @param inheritsched the inheritsched attribute determines how the other scheduling attributes of the created thread are to be set:
|
||||
* The policy can be ONE of the following values:
|
||||
*
|
||||
* PTHREAD_INHERIT_SCHED Specifies that the scheduling policy and associated attributes are
|
||||
* to be inherited from the creating thread, and the scheduling attributes
|
||||
* in this attr argument are to be ignored.
|
||||
*
|
||||
* PTHREAD_EXPLICIT_SCHED Specifies that the scheduling policy and associated attributes are to be
|
||||
* set to the corresponding values from this attribute object.
|
||||
*
|
||||
* @return Upon successful completion, these functions shall return a value of 0;
|
||||
*/
|
||||
int pthread_attr_setinheritsched(pthread_attr_t *attr, int inheritsched)
|
||||
{
|
||||
RT_ASSERT(attr != RT_NULL);
|
||||
|
||||
attr->inheritsched = inheritsched;
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(pthread_attr_setinheritsched);
|
||||
|
||||
/**
|
||||
* @brief This function get and set the inheritsched attribute in the attr argument.
|
||||
*
|
||||
* @note The function sets the inherit-scheduler attribute of the thread attributes object
|
||||
* referred to by attr to the value specified in inheritsched.
|
||||
* The inherit-scheduler attribute determines whether a thread created using the thread
|
||||
* attributes object attr will inherit its scheduling attributes from the calling thread
|
||||
* or whether it will take them from attr.
|
||||
*
|
||||
* @see pthread_attr_init(), pthread_attr_getschedpolicy(), pthread_attr_getschedparam()
|
||||
*
|
||||
* @param attr is a thread attributes object.
|
||||
*
|
||||
* @param inheritsched the inheritsched attribute determines how the other scheduling attributes of the created thread are to be set:
|
||||
* The inheritsched can be ONE of the following values:
|
||||
*
|
||||
* PTHREAD_INHERIT_SCHED Specifies that the scheduling policy and associated attributes are
|
||||
* to be inherited from the creating thread, and the scheduling attributes
|
||||
* in this attr argument are to be ignored.
|
||||
*
|
||||
* PTHREAD_EXPLICIT_SCHED Specifies that the scheduling policy and associated attributes are to be
|
||||
* set to the corresponding values from this attribute object.
|
||||
*
|
||||
* @return Upon successful completion, these functions shall return a value of 0;
|
||||
*/
|
||||
int pthread_attr_getinheritsched(const pthread_attr_t *attr, int *inheritsched)
|
||||
{
|
||||
RT_ASSERT(attr != RT_NULL);
|
||||
|
||||
*inheritsched = attr->inheritsched;
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(pthread_attr_getinheritsched);
|
||||
|
||||
/**
|
||||
* @brief This function set contentionscope attribute.
|
||||
*
|
||||
* @note The function are used to set the contentionscope attribute in the attr object.
|
||||
*
|
||||
* @param attr is a thread attributes object.
|
||||
*
|
||||
* @param scope is the value of contentionscope attribute.
|
||||
* The scope can be ONE of the following values:
|
||||
*
|
||||
* PTHREAD_SCOPE_SYSTEM signifying system scheduling contention scope.
|
||||
*
|
||||
* PTHREAD_SCOPE_PROCESS signifying process scheduling contention scope.
|
||||
*
|
||||
* @return Upon successful completion, these functions shall return a value of 0;
|
||||
*/
|
||||
int pthread_attr_setscope(pthread_attr_t *attr, int scope)
|
||||
{
|
||||
if (scope == PTHREAD_SCOPE_SYSTEM)
|
||||
return 0;
|
||||
if (scope == PTHREAD_SCOPE_PROCESS)
|
||||
return EOPNOTSUPP;
|
||||
|
||||
return EINVAL;
|
||||
}
|
||||
RTM_EXPORT(pthread_attr_setscope);
|
||||
|
||||
/**
|
||||
* @brief This function get contentionscope attribute.
|
||||
*
|
||||
* @note The function are used to get the contentionscope attribute in the attr object.
|
||||
*
|
||||
* @param attr is a thread attributes object.
|
||||
*
|
||||
* @param scope is the value of contentionscope attribute.
|
||||
* The scope can be ONE of the following values:
|
||||
*
|
||||
* PTHREAD_SCOPE_SYSTEM signifying system scheduling contention scope.
|
||||
*
|
||||
* PTHREAD_SCOPE_PROCESS signifying process scheduling contention scope.
|
||||
*
|
||||
* @return Upon successful completion, these functions shall return a value of 0;
|
||||
*/
|
||||
int pthread_attr_getscope(pthread_attr_t const *attr, int *scope)
|
||||
{
|
||||
return PTHREAD_SCOPE_SYSTEM;
|
||||
}
|
||||
RTM_EXPORT(pthread_attr_getscope);
|
||||
223
RT_Thread/components/libc/posix/pthreads/pthread_barrier.c
Normal file
223
RT_Thread/components/libc/posix/pthreads/pthread_barrier.c
Normal file
@ -0,0 +1,223 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2024 RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2010-10-26 Bernard the first version
|
||||
*/
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
int pthread_barrierattr_destroy(pthread_barrierattr_t *attr)
|
||||
{
|
||||
if (!attr)
|
||||
return EINVAL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(pthread_barrierattr_destroy);
|
||||
|
||||
int pthread_barrierattr_init(pthread_barrierattr_t *attr)
|
||||
{
|
||||
if (!attr)
|
||||
return EINVAL;
|
||||
*attr = PTHREAD_PROCESS_PRIVATE;
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(pthread_barrierattr_init);
|
||||
|
||||
int pthread_barrierattr_getpshared(const pthread_barrierattr_t *attr,
|
||||
int *pshared)
|
||||
{
|
||||
if (!attr)
|
||||
return EINVAL;
|
||||
*pshared = (int)*attr;
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(pthread_barrierattr_getpshared);
|
||||
|
||||
int pthread_barrierattr_setpshared(pthread_barrierattr_t *attr, int pshared)
|
||||
{
|
||||
if (!attr)
|
||||
return EINVAL;
|
||||
if (pshared == PTHREAD_PROCESS_PRIVATE)
|
||||
{
|
||||
*attr = PTHREAD_PROCESS_PRIVATE;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return EINVAL;
|
||||
}
|
||||
RTM_EXPORT(pthread_barrierattr_setpshared);
|
||||
|
||||
/**
|
||||
* @brief Destroys a barrier object.
|
||||
*
|
||||
* The `pthread_barrier_destroy` function releases any resources associated
|
||||
* with the specified barrier object. After a barrier has been destroyed,
|
||||
* it cannot be used again unless it is reinitialized using `pthread_barrier_init`.
|
||||
*
|
||||
* @param[in] barrier
|
||||
* A pointer to an initialized `pthread_barrier_t` object to be destroyed.
|
||||
*
|
||||
* @return
|
||||
* - `0` on success.
|
||||
* - `EINVAL` if the `barrier` is invalid or uninitialized.
|
||||
* - `EBUSY` if there are threads currently blocked on the barrier.
|
||||
*
|
||||
* @note
|
||||
* - Ensure that no threads are blocked on the barrier before calling this function.
|
||||
* - Attempting to destroy a barrier that is still in use results in undefined behavior.
|
||||
*
|
||||
* @warning
|
||||
* Destroying a barrier without ensuring it is no longer in use can lead to
|
||||
* resource leaks or undefined program behavior.
|
||||
*
|
||||
* @see pthread_barrier_init, pthread_barrier_wait
|
||||
*/
|
||||
int pthread_barrier_destroy(pthread_barrier_t *barrier)
|
||||
{
|
||||
rt_err_t result;
|
||||
|
||||
if (!barrier)
|
||||
return EINVAL;
|
||||
|
||||
/* Lock the internal mutex to safely check the barrier's state*/
|
||||
result = pthread_mutex_lock(&(barrier->mutex));
|
||||
if (result != 0)
|
||||
return result;
|
||||
|
||||
/* Check if any threads are currently waiting on the barrier*/
|
||||
if (barrier->count != 0)
|
||||
{
|
||||
pthread_mutex_unlock(&(barrier->mutex));
|
||||
return EBUSY; /* Threads are still waiting*/
|
||||
}
|
||||
|
||||
/* Free resources associated with the barrier*/
|
||||
result = pthread_mutex_unlock(&(barrier->mutex));
|
||||
if (result != 0)
|
||||
{
|
||||
return result; /* Return mutex unlock error*/
|
||||
}
|
||||
|
||||
result = pthread_mutex_destroy(&(barrier->mutex));
|
||||
if (result != 0)
|
||||
{
|
||||
return result; /* Return mutex destroy error*/
|
||||
}
|
||||
result = pthread_cond_destroy(&(barrier->cond));
|
||||
|
||||
return result;
|
||||
}
|
||||
RTM_EXPORT(pthread_barrier_destroy);
|
||||
|
||||
/**
|
||||
* @brief Initializes a barrier for synchronizing threads.
|
||||
*
|
||||
* The `pthread_barrier_init` function initializes a barrier object
|
||||
* that allows a specified number of threads to synchronize at a barrier point.
|
||||
* Each thread waits at the barrier until the required number of threads have called
|
||||
* `pthread_barrier_wait`.
|
||||
*
|
||||
* @param[out] barrier
|
||||
* A pointer to the `pthread_barrier_t` object to be initialized.
|
||||
* This object must not already be initialized.
|
||||
*
|
||||
* @param[in] attr
|
||||
* A pointer to a `pthread_barrierattr_t` object that specifies
|
||||
* attributes for the barrier (e.g., process-shared or process-private).
|
||||
* If NULL, the default attributes are used.
|
||||
*
|
||||
* @param[in] count
|
||||
* The number of threads that must call `pthread_barrier_wait`
|
||||
* before any of them successfully return from the barrier.
|
||||
*
|
||||
* @return
|
||||
* - `0` on success.
|
||||
* - `EINVAL` if the `count` is zero or `barrier` is invalid.
|
||||
*
|
||||
* @note The barrier must be destroyed using `pthread_barrier_destroy`
|
||||
* when it is no longer needed.
|
||||
*
|
||||
* @warning If `count` is set to zero, the behavior is undefined.
|
||||
*
|
||||
* @see pthread_barrier_wait, pthread_barrier_destroy
|
||||
*/
|
||||
int pthread_barrier_init(pthread_barrier_t *barrier,
|
||||
const pthread_barrierattr_t *attr,
|
||||
unsigned count)
|
||||
{
|
||||
if (!barrier)
|
||||
return EINVAL;
|
||||
if (attr && (*attr != PTHREAD_PROCESS_PRIVATE))
|
||||
return EINVAL;
|
||||
if (count == 0)
|
||||
return EINVAL;
|
||||
|
||||
barrier->count = count;
|
||||
pthread_cond_init(&(barrier->cond), NULL);
|
||||
pthread_mutex_init(&(barrier->mutex), NULL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(pthread_barrier_init);
|
||||
|
||||
/**
|
||||
* @brief Synchronizes threads at a barrier.
|
||||
*
|
||||
* The `pthread_barrier_wait` function blocks the calling thread at the specified
|
||||
* barrier until the required number of threads have reached the barrier. Once
|
||||
* the required number of threads have called this function, all threads are
|
||||
* unblocked and can proceed.
|
||||
*
|
||||
* @param[in] barrier
|
||||
* A pointer to an initialized `pthread_barrier_t` object representing the barrier
|
||||
* at which threads will synchronize.
|
||||
*
|
||||
* @return
|
||||
* - `0` for all threads except one.
|
||||
* - `EINVAL` - The `barrier` is invalid or uninitialized.
|
||||
*
|
||||
* @note
|
||||
* - All threads participating in the barrier must call `pthread_barrier_wait`
|
||||
* before any of them are released.
|
||||
*
|
||||
* @warning
|
||||
* Ensure that the number of threads specified during the barrier's initialization
|
||||
* matches the number of threads calling this function, otherwise the program
|
||||
* may hang indefinitely.
|
||||
*
|
||||
* @see pthread_barrier_init, pthread_barrier_destroy
|
||||
*/
|
||||
int pthread_barrier_wait(pthread_barrier_t *barrier)
|
||||
{
|
||||
rt_err_t result;
|
||||
if (!barrier)
|
||||
return EINVAL;
|
||||
|
||||
result = pthread_mutex_lock(&(barrier->mutex));
|
||||
if (result != 0)
|
||||
return EINVAL;
|
||||
|
||||
if (barrier->count == 0)
|
||||
result = EINVAL;
|
||||
else
|
||||
{
|
||||
barrier->count -= 1;
|
||||
if (barrier->count == 0) /* broadcast condition */
|
||||
pthread_cond_broadcast(&(barrier->cond));
|
||||
else
|
||||
pthread_cond_wait(&(barrier->cond), &(barrier->mutex));
|
||||
}
|
||||
|
||||
pthread_mutex_unlock(&(barrier->mutex));
|
||||
|
||||
return result;
|
||||
}
|
||||
RTM_EXPORT(pthread_barrier_wait);
|
||||
|
||||
555
RT_Thread/components/libc/posix/pthreads/pthread_cond.c
Normal file
555
RT_Thread/components/libc/posix/pthreads/pthread_cond.c
Normal file
@ -0,0 +1,555 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2024 RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2010-10-26 Bernard the first version
|
||||
* 2022-06-27 xiangxistu use atomic operation to protect pthread conditional variable
|
||||
*/
|
||||
|
||||
#include <rthw.h>
|
||||
#include <pthread.h>
|
||||
#include "pthread_internal.h"
|
||||
|
||||
int pthread_condattr_destroy(pthread_condattr_t *attr)
|
||||
{
|
||||
if (!attr)
|
||||
return EINVAL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(pthread_condattr_destroy);
|
||||
|
||||
int pthread_condattr_init(pthread_condattr_t *attr)
|
||||
{
|
||||
if (!attr)
|
||||
return EINVAL;
|
||||
*attr = PTHREAD_PROCESS_PRIVATE;
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(pthread_condattr_init);
|
||||
|
||||
int pthread_condattr_getclock(const pthread_condattr_t *attr,
|
||||
clockid_t *clock_id)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(pthread_condattr_getclock);
|
||||
|
||||
int pthread_condattr_setclock(pthread_condattr_t *attr,
|
||||
clockid_t clock_id)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(pthread_condattr_setclock);
|
||||
|
||||
int pthread_condattr_getpshared(const pthread_condattr_t *attr, int *pshared)
|
||||
{
|
||||
if (!attr || !pshared)
|
||||
return EINVAL;
|
||||
|
||||
*pshared = PTHREAD_PROCESS_PRIVATE;
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(pthread_condattr_getpshared);
|
||||
|
||||
int pthread_condattr_setpshared(pthread_condattr_t *attr, int pshared)
|
||||
{
|
||||
if ((pshared != PTHREAD_PROCESS_PRIVATE) &&
|
||||
(pshared != PTHREAD_PROCESS_SHARED))
|
||||
{
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
if (pshared != PTHREAD_PROCESS_PRIVATE)
|
||||
return ENOSYS;
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(pthread_condattr_setpshared);
|
||||
|
||||
/**
|
||||
* @brief Initializes a condition variable.
|
||||
*
|
||||
* This function initializes the condition variable pointed to by `cond` with the attributes
|
||||
* specified by `attr`. If `attr` is NULL, the condition variable is initialized with the
|
||||
* default attributes.
|
||||
*
|
||||
* @param cond A pointer to the condition variable to be initialized.
|
||||
* Must point to valid memory.
|
||||
* @param attr A pointer to the condition variable attributes object.
|
||||
* If NULL, default attributes are used.
|
||||
*
|
||||
* @return
|
||||
* - `0` on success.
|
||||
* - A non-zero error code on failure, including:
|
||||
* - `EINVAL`: Invalid attributes, invalid condition variable pointer, or semaphore init failed.
|
||||
*
|
||||
* @note
|
||||
* - The condition variable must not be used until it has been initialized.
|
||||
* - Each condition variable must be destroyed using `pthread_cond_destroy()`
|
||||
* once it is no longer needed.
|
||||
*
|
||||
* @see pthread_cond_destroy, pthread_cond_wait, pthread_cond_signal, pthread_cond_broadcast
|
||||
*/
|
||||
int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
|
||||
{
|
||||
rt_err_t result;
|
||||
char cond_name[RT_NAME_MAX];
|
||||
static rt_uint16_t cond_num = 0;
|
||||
|
||||
/* parameter check */
|
||||
if (cond == RT_NULL)
|
||||
return EINVAL;
|
||||
if ((attr != RT_NULL) && (*attr != PTHREAD_PROCESS_PRIVATE))
|
||||
return EINVAL;
|
||||
|
||||
rt_snprintf(cond_name, sizeof(cond_name), "cond%02d", cond_num++);
|
||||
|
||||
/* use default value */
|
||||
if (attr == RT_NULL)
|
||||
{
|
||||
cond->attr = PTHREAD_PROCESS_PRIVATE;
|
||||
}
|
||||
else
|
||||
{
|
||||
cond->attr = *attr;
|
||||
}
|
||||
|
||||
result = rt_sem_init(&cond->sem, cond_name, 0, RT_IPC_FLAG_FIFO);
|
||||
if (result != RT_EOK)
|
||||
{
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
/* detach the object from system object container */
|
||||
rt_object_detach(&(cond->sem.parent.parent));
|
||||
cond->sem.parent.parent.type = RT_Object_Class_Semaphore;
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(pthread_cond_init);
|
||||
|
||||
/**
|
||||
* @brief Destroys a condition variable.
|
||||
*
|
||||
* This function destroys the condition variable pointed to by `cond`. After a condition
|
||||
* variable is destroyed, it must not be used until it is reinitialized with
|
||||
* `pthread_cond_init`.
|
||||
*
|
||||
* @param cond A pointer to the condition variable to be destroyed.
|
||||
* Must point to a valid, previously initialized condition variable.
|
||||
*
|
||||
* @return
|
||||
* - `0` on success.
|
||||
* - A non-zero error code on failure, including:
|
||||
* - `EBUSY`: The condition variable is currently in use by other threads.
|
||||
* - `EINVAL`: The condition variable is invalid or uninitialized.
|
||||
*
|
||||
* @note
|
||||
* - The condition variable must not be destroyed while it is being used by other threads
|
||||
* (e.g., in `pthread_cond_wait` or `pthread_cond_timedwait`).
|
||||
* - Attempting to destroy a condition variable that has not been initialized results in
|
||||
* undefined behavior.
|
||||
*
|
||||
* @see pthread_cond_init, pthread_cond_wait, pthread_cond_signal, pthread_cond_broadcast
|
||||
*/
|
||||
int pthread_cond_destroy(pthread_cond_t *cond)
|
||||
{
|
||||
rt_err_t result;
|
||||
if (cond == RT_NULL)
|
||||
{
|
||||
return EINVAL;
|
||||
}
|
||||
/* which is not initialized */
|
||||
if (cond->attr == -1)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!rt_list_isempty(&cond->sem.parent.suspend_thread))
|
||||
{
|
||||
return EBUSY;
|
||||
}
|
||||
__retry:
|
||||
result = rt_sem_trytake(&(cond->sem));
|
||||
if (result == EBUSY)
|
||||
{
|
||||
pthread_cond_broadcast(cond);
|
||||
goto __retry;
|
||||
}
|
||||
|
||||
/* clean condition */
|
||||
rt_memset(cond, 0, sizeof(pthread_cond_t));
|
||||
cond->attr = -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(pthread_cond_destroy);
|
||||
|
||||
/**
|
||||
* @brief Unblocks all threads waiting on the specified condition variable.
|
||||
*
|
||||
* This function wakes up all threads that are currently blocked on the condition variable
|
||||
* pointed to by `cond`. The condition variable must be associated with a mutex, and
|
||||
* threads waiting on the condition variable should recheck the condition after being
|
||||
* unblocked.
|
||||
*
|
||||
* @param cond A pointer to the condition variable.
|
||||
* Must point to a valid, initialized condition variable.
|
||||
*
|
||||
* @return
|
||||
* - `0` on success.
|
||||
* - A non-zero error code on failure, including:
|
||||
* - `EINVAL`: The condition variable is invalid or uninitialized.
|
||||
*
|
||||
* @note
|
||||
* - Calling this function does not release the associated mutex.
|
||||
* - Waking up threads does not guarantee that any specific thread will acquire the
|
||||
* mutex immediately, as thread scheduling depends on the system.
|
||||
* - Typically used when the condition might allow multiple waiting threads to proceed.
|
||||
*
|
||||
* @see pthread_cond_signal, pthread_cond_wait, pthread_cond_init, pthread_cond_destroy
|
||||
*/
|
||||
int pthread_cond_broadcast(pthread_cond_t *cond)
|
||||
{
|
||||
rt_err_t result;
|
||||
|
||||
if (cond == RT_NULL)
|
||||
return EINVAL;
|
||||
if (cond->attr == -1)
|
||||
pthread_cond_init(cond, RT_NULL);
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* try to take condition semaphore */
|
||||
result = rt_sem_trytake(&(cond->sem));
|
||||
if (result == -RT_ETIMEOUT)
|
||||
{
|
||||
/* it's timeout, release this semaphore */
|
||||
rt_sem_release(&(cond->sem));
|
||||
}
|
||||
else if (result == RT_EOK)
|
||||
{
|
||||
/* has taken this semaphore, release it */
|
||||
rt_sem_release(&(cond->sem));
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
return EINVAL;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(pthread_cond_broadcast);
|
||||
|
||||
/**
|
||||
* @brief Wakes up one thread waiting on the specified condition variable.
|
||||
*
|
||||
* This function unblocks one thread that is currently waiting on the
|
||||
* condition variable `cond`. If multiple threads are waiting, the thread to wake
|
||||
* up is determined by the system's scheduling policies.
|
||||
*
|
||||
* @param cond A pointer to the condition variable to signal.
|
||||
* Must point to a valid and initialized condition variable.
|
||||
*
|
||||
* @return
|
||||
* - `0` on success.
|
||||
* - A non-zero error code on failure, including:
|
||||
* - `EINVAL`: The condition variable is invalid or uninitialized.
|
||||
*
|
||||
* @note
|
||||
* - This function does not release the associated mutex.
|
||||
* - If no threads are currently waiting on the condition variable, the call has no effect.
|
||||
* - The awakened thread will not run until it can reacquire the associated mutex and
|
||||
* re-evaluate the waiting condition.
|
||||
* - It is typically used when only one waiting thread should be allowed to proceed.
|
||||
*
|
||||
* @see pthread_cond_broadcast, pthread_cond_wait, pthread_cond_init, pthread_cond_destroy
|
||||
*/
|
||||
int pthread_cond_signal(pthread_cond_t *cond)
|
||||
{
|
||||
rt_base_t temp;
|
||||
rt_err_t result;
|
||||
|
||||
if (cond == RT_NULL)
|
||||
return EINVAL;
|
||||
if (cond->attr == -1)
|
||||
pthread_cond_init(cond, RT_NULL);
|
||||
|
||||
/* disable interrupt */
|
||||
temp = rt_hw_interrupt_disable();
|
||||
if (rt_list_isempty(&cond->sem.parent.suspend_thread))
|
||||
{
|
||||
/* enable interrupt */
|
||||
rt_hw_interrupt_enable(temp);
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* enable interrupt */
|
||||
rt_hw_interrupt_enable(temp);
|
||||
result = rt_sem_release(&(cond->sem));
|
||||
if (result == RT_EOK)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
RTM_EXPORT(pthread_cond_signal);
|
||||
|
||||
/**
|
||||
* @brief Waits on a condition variable with a timeout.
|
||||
*
|
||||
* This function causes the calling thread to block on the condition variable `cond`,
|
||||
* releasing the associated mutex `mutex`. The thread will remain blocked until
|
||||
* one of the following occurs:
|
||||
* - It is signaled or broadcast using `pthread_cond_signal` or `pthread_cond_broadcast`.
|
||||
* - The specified timeout expires.
|
||||
*
|
||||
* @param cond A pointer to the condition variable to wait on.
|
||||
* Must point to a valid, initialized condition variable.
|
||||
* @param mutex A pointer to the mutex associated with the condition variable.
|
||||
* Must be locked by the calling thread before invoking this function.
|
||||
* @param timeout The timeout duration in milliseconds. A value of `RT_WAITING_FOREVER`
|
||||
* indicates the thread will wait indefinitely.
|
||||
*
|
||||
* @return
|
||||
* - `RT_EOK` on successful wakeup (signaled or broadcast).
|
||||
* - `-RT_ETIMEOUT` if the timeout expires before the condition variable is signaled.
|
||||
* - `-RT_ERROR` if an error occurs (e.g., invalid parameters).
|
||||
*
|
||||
* @note
|
||||
* - The mutex is automatically released while the thread waits and re-acquired before
|
||||
* the function returns.
|
||||
* - If `timeout` is 0, the function behaves as a non-blocking check.
|
||||
* - Ensure the condition variable and mutex are properly initialized before use.
|
||||
*
|
||||
* @see pthread_cond_signal, pthread_cond_broadcast, pthread_mutex_lock, pthread_mutex_unlock
|
||||
*/
|
||||
rt_err_t _pthread_cond_timedwait(pthread_cond_t *cond,
|
||||
pthread_mutex_t *mutex,
|
||||
rt_int32_t timeout)
|
||||
{
|
||||
rt_err_t result = RT_EOK;
|
||||
rt_sem_t sem;
|
||||
rt_int32_t time;
|
||||
|
||||
sem = &(cond->sem);
|
||||
if (sem == RT_NULL)
|
||||
{
|
||||
return -RT_ERROR;
|
||||
}
|
||||
time = timeout;
|
||||
|
||||
if (!cond || !mutex)
|
||||
{
|
||||
return -RT_ERROR;
|
||||
}
|
||||
/* check whether initialized */
|
||||
if (cond->attr == -1)
|
||||
{
|
||||
pthread_cond_init(cond, RT_NULL);
|
||||
}
|
||||
|
||||
/* The mutex was not owned by the current thread at the time of the call. */
|
||||
if (mutex->lock.owner != rt_thread_self())
|
||||
{
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
{
|
||||
register rt_base_t temp;
|
||||
struct rt_thread *thread;
|
||||
|
||||
/* parameter check */
|
||||
RT_ASSERT(sem != RT_NULL);
|
||||
RT_ASSERT(rt_object_get_type(&sem->parent.parent) == RT_Object_Class_Semaphore);
|
||||
|
||||
/* disable interrupt */
|
||||
temp = rt_hw_interrupt_disable();
|
||||
|
||||
if (sem->value > 0)
|
||||
{
|
||||
/* semaphore is available */
|
||||
sem->value--;
|
||||
|
||||
/* enable interrupt */
|
||||
rt_hw_interrupt_enable(temp);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* no waiting, return with timeout */
|
||||
if (time == 0)
|
||||
{
|
||||
rt_hw_interrupt_enable(temp);
|
||||
|
||||
return -RT_ETIMEOUT;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* current context checking */
|
||||
RT_DEBUG_IN_THREAD_CONTEXT;
|
||||
|
||||
/* semaphore is unavailable, push to suspend list */
|
||||
/* get current thread */
|
||||
thread = rt_thread_self();
|
||||
|
||||
/* reset thread error number */
|
||||
thread->error = RT_EOK;
|
||||
|
||||
/* suspend thread */
|
||||
rt_thread_suspend(thread);
|
||||
|
||||
/* Only support FIFO */
|
||||
rt_list_insert_before(&(sem->parent.suspend_thread), &RT_THREAD_LIST_NODE(thread));
|
||||
|
||||
/**
|
||||
rt_ipc_list_suspend(&(sem->parent.suspend_thread),
|
||||
thread,
|
||||
sem->parent.parent.flag);
|
||||
*/
|
||||
|
||||
/* has waiting time, start thread timer */
|
||||
if (time > 0)
|
||||
{
|
||||
/* reset the timeout of thread timer and start it */
|
||||
rt_timer_control(&(thread->thread_timer),
|
||||
RT_TIMER_CTRL_SET_TIME,
|
||||
&time);
|
||||
rt_timer_start(&(thread->thread_timer));
|
||||
}
|
||||
|
||||
/* to avoid the lost of singal< cond->sem > */
|
||||
if (pthread_mutex_unlock(mutex) != 0)
|
||||
{
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/* enable interrupt */
|
||||
rt_hw_interrupt_enable(temp);
|
||||
|
||||
/* do schedule */
|
||||
rt_schedule();
|
||||
|
||||
result = thread->error;
|
||||
|
||||
/* lock mutex again */
|
||||
pthread_mutex_lock(mutex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
RTM_EXPORT(_pthread_cond_timedwait);
|
||||
|
||||
/**
|
||||
* @brief Waits on a condition variable.
|
||||
*
|
||||
* This function blocks the calling thread on the condition variable `cond` and releases
|
||||
* the associated mutex `mutex`. The thread remains blocked until it is signaled or
|
||||
* broadcast using `pthread_cond_signal` or `pthread_cond_broadcast`. When the thread
|
||||
* is awakened, it re-acquires the mutex and resumes execution.
|
||||
*
|
||||
* @param cond A pointer to the condition variable to wait on.
|
||||
* Must point to a valid, initialized condition variable.
|
||||
* @param mutex A pointer to the mutex associated with the condition variable.
|
||||
* Must be locked by the calling thread before invoking this function.
|
||||
*
|
||||
* @return
|
||||
* - `0` on success.
|
||||
* - A non-zero error code on failure, including:
|
||||
* - `EINVAL`: The condition variable or mutex is invalid or uninitialized.
|
||||
*
|
||||
* @note
|
||||
* - The mutex must be locked before calling this function.
|
||||
* - Upon returning, the mutex is locked again by the calling thread.
|
||||
* - Spurious wakeups may occur, so the thread should always recheck the waiting
|
||||
* condition upon wakeup.
|
||||
* - This function may block indefinitely unless the condition is signaled or broadcast.
|
||||
*
|
||||
* @see pthread_cond_signal, pthread_cond_broadcast, pthread_cond_timedwait, pthread_mutex_lock
|
||||
*/
|
||||
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
|
||||
{
|
||||
rt_err_t result;
|
||||
|
||||
__retry:
|
||||
result = _pthread_cond_timedwait(cond, mutex, RT_WAITING_FOREVER);
|
||||
if (result == RT_EOK)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
else if (result == -RT_EINTR)
|
||||
{
|
||||
/* https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_wait.html
|
||||
* These functions shall not return an error code of [EINTR].
|
||||
*/
|
||||
goto __retry;
|
||||
}
|
||||
|
||||
return EINVAL;
|
||||
}
|
||||
RTM_EXPORT(pthread_cond_wait);
|
||||
|
||||
/**
|
||||
* @brief Waits on a condition variable with a timeout.
|
||||
*
|
||||
* This function blocks the calling thread on the condition variable `cond`, releasing
|
||||
* the associated mutex `mutex`. The thread remains blocked until one of the following occurs:
|
||||
* - The condition variable is signaled or broadcast using `pthread_cond_signal` or `pthread_cond_broadcast`.
|
||||
* - The specified absolute timeout `abstime` is reached.
|
||||
* - A spurious wakeup occurs (requiring the thread to recheck the condition).
|
||||
*
|
||||
* @param cond A pointer to the condition variable to wait on.
|
||||
* Must point to a valid, initialized condition variable.
|
||||
* @param mutex A pointer to the mutex associated with the condition variable.
|
||||
* Must be locked by the calling thread before invoking this function.
|
||||
* @param abstime A pointer to a `struct timespec` specifying the absolute timeout (in seconds and nanoseconds
|
||||
* since the Epoch). If the time specified is already reached, the function immediately returns.
|
||||
*
|
||||
* @return
|
||||
* - `0` on successful wakeup (signaled or broadcast).
|
||||
* - `ETIMEDOUT` if the timeout expires before the condition variable is signaled.
|
||||
* - A non-zero error code on failure, including:
|
||||
* - `EINVAL`: The condition variable, mutex, or `abstime` is invalid.
|
||||
* - `EPERM`: The mutex is not owned by the calling thread.
|
||||
*
|
||||
* @note
|
||||
* - The mutex is released while the thread is waiting and re-acquired before the function returns.
|
||||
* - Spurious wakeups may occur, so the thread must always recheck the waiting condition upon wakeup.
|
||||
* - The timeout is specified in absolute time, not relative duration.
|
||||
* - Ensure the condition variable and mutex are properly initialized before use.
|
||||
*
|
||||
* @see pthread_cond_wait, pthread_cond_signal, pthread_cond_broadcast, pthread_mutex_lock
|
||||
*/
|
||||
int pthread_cond_timedwait(pthread_cond_t *cond,
|
||||
pthread_mutex_t *mutex,
|
||||
const struct timespec *abstime)
|
||||
{
|
||||
int timeout;
|
||||
rt_err_t result;
|
||||
|
||||
timeout = rt_timespec_to_tick(abstime);
|
||||
result = _pthread_cond_timedwait(cond, mutex, timeout);
|
||||
if (result == RT_EOK)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if (result == -RT_ETIMEOUT)
|
||||
{
|
||||
return ETIMEDOUT;
|
||||
}
|
||||
|
||||
return EINVAL;
|
||||
}
|
||||
RTM_EXPORT(pthread_cond_timedwait);
|
||||
66
RT_Thread/components/libc/posix/pthreads/pthread_internal.h
Normal file
66
RT_Thread/components/libc/posix/pthreads/pthread_internal.h
Normal file
@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2021, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2010-10-26 Bernard the first version
|
||||
*/
|
||||
|
||||
#ifndef __PTHREAD_INTERNAL_H__
|
||||
#define __PTHREAD_INTERNAL_H__
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <pthread.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
struct _pthread_cleanup
|
||||
{
|
||||
void (*cleanup_func)(void *parameter);
|
||||
void *parameter;
|
||||
|
||||
struct _pthread_cleanup *next;
|
||||
};
|
||||
typedef struct _pthread_cleanup _pthread_cleanup_t;
|
||||
|
||||
struct _pthread_key_data
|
||||
{
|
||||
int is_used;
|
||||
void (*destructor)(void *parameter);
|
||||
};
|
||||
typedef struct _pthread_key_data _pthread_key_data_t;
|
||||
|
||||
#ifndef PTHREAD_NUM_MAX
|
||||
#define PTHREAD_NUM_MAX 32
|
||||
#endif
|
||||
|
||||
#define PTHREAD_MAGIC 0x70746873
|
||||
struct _pthread_data
|
||||
{
|
||||
rt_uint32_t magic;
|
||||
pthread_attr_t attr;
|
||||
rt_thread_t tid;
|
||||
|
||||
void* (*thread_entry)(void *parameter);
|
||||
void *thread_parameter;
|
||||
|
||||
/* return value */
|
||||
void *return_value;
|
||||
|
||||
/* semaphore for joinable thread */
|
||||
rt_sem_t joinable_sem;
|
||||
|
||||
/* cancel state and type */
|
||||
rt_uint8_t cancelstate;
|
||||
volatile rt_uint8_t canceltype;
|
||||
volatile rt_uint8_t canceled;
|
||||
|
||||
_pthread_cleanup_t *cleanup;
|
||||
void** tls; /* thread-local storage area */
|
||||
};
|
||||
typedef struct _pthread_data _pthread_data_t;
|
||||
|
||||
_pthread_data_t *_pthread_get_data(pthread_t thread);
|
||||
|
||||
#endif
|
||||
584
RT_Thread/components/libc/posix/pthreads/pthread_mutex.c
Normal file
584
RT_Thread/components/libc/posix/pthreads/pthread_mutex.c
Normal file
@ -0,0 +1,584 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2024 RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2010-10-26 Bernard the first version
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include "pthread.h"
|
||||
|
||||
#define MUTEXATTR_SHARED_MASK 0x0010
|
||||
#define MUTEXATTR_TYPE_MASK 0x000f
|
||||
|
||||
const pthread_mutexattr_t pthread_default_mutexattr = PTHREAD_PROCESS_PRIVATE;
|
||||
|
||||
/**
|
||||
* @brief Initializes a mutex attributes object.
|
||||
*
|
||||
* This function initializes a mutex attributes object pointed to by `attr` with
|
||||
* default attribute values. Once initialized, the attributes object can be used
|
||||
* to customize the behavior of mutexes created using it.
|
||||
*
|
||||
* @param[out] attr Pointer to the mutex attributes object to be initialized.
|
||||
*
|
||||
* @return
|
||||
* - 0 on success.
|
||||
* - Non-zero error code on failure.
|
||||
*
|
||||
* @note
|
||||
* After initialization, the mutex attributes object must be destroyed with
|
||||
* `pthread_mutexattr_destroy()` when it is no longer needed.
|
||||
*
|
||||
* @warning
|
||||
* Using an uninitialized mutex attributes object may result in undefined behavior.
|
||||
*
|
||||
* @see pthread_mutexattr_destroy, pthread_mutex_init
|
||||
*/
|
||||
int pthread_mutexattr_init(pthread_mutexattr_t *attr)
|
||||
{
|
||||
if (attr)
|
||||
{
|
||||
*attr = pthread_default_mutexattr;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
return EINVAL;
|
||||
}
|
||||
RTM_EXPORT(pthread_mutexattr_init);
|
||||
|
||||
/**
|
||||
* @brief Destroys a mutex attributes object.
|
||||
*
|
||||
* This function releases any resources associated with the mutex attributes object
|
||||
* pointed to by `attr`. After the attributes object is destroyed, it should not
|
||||
* be used unless it is re-initialized with `pthread_mutexattr_init()`.
|
||||
*
|
||||
* @param[in,out] attr Pointer to the mutex attributes object to be destroyed.
|
||||
*
|
||||
* @return
|
||||
* - 0 on success.
|
||||
* - Non-zero error code on failure, including:
|
||||
* - `EINVAL`: The attributes object is invalid or uninitialized.
|
||||
*
|
||||
* @note
|
||||
* Destroying an uninitialized or already destroyed attributes object results in undefined behavior.
|
||||
*
|
||||
* @warning
|
||||
* Ensure that no mutexes are being initialized or created using this attributes object
|
||||
* at the time of its destruction.
|
||||
*
|
||||
* @see pthread_mutexattr_init, pthread_mutex_init
|
||||
*/
|
||||
int pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
|
||||
{
|
||||
if (attr)
|
||||
{
|
||||
*attr = -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
return EINVAL;
|
||||
}
|
||||
RTM_EXPORT(pthread_mutexattr_destroy);
|
||||
|
||||
/**
|
||||
* @brief Retrieves the type attribute of a mutex attributes object.
|
||||
*
|
||||
* This function retrieves the mutex type attribute from the attributes object
|
||||
* pointed to by `attr` and stores it in the integer pointed to by `type`.
|
||||
*
|
||||
* @param[in] attr Pointer to the mutex attributes object.
|
||||
* @param[out] type Pointer to an integer where the mutex type will be stored.
|
||||
* Possible values include:
|
||||
* - `PTHREAD_MUTEX_NORMAL`: Default mutex type.
|
||||
* - `PTHREAD_MUTEX_ERRORCHECK`: Mutex with error-checking.
|
||||
* - `PTHREAD_MUTEX_RECURSIVE`: Recursive mutex.
|
||||
*
|
||||
* @return
|
||||
* - 0 on success.
|
||||
* - Non-zero error code on failure, including:
|
||||
* - `EINVAL`: The attributes object or the `type` pointer is invalid.
|
||||
*
|
||||
* @note
|
||||
* Use this function to check the type of a mutex attributes object that has
|
||||
* already been initialized or configured.
|
||||
*
|
||||
* @see pthread_mutexattr_settype, pthread_mutexattr_init
|
||||
*/
|
||||
int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type)
|
||||
{
|
||||
if (attr && type)
|
||||
{
|
||||
int atype = (*attr & MUTEXATTR_TYPE_MASK);
|
||||
|
||||
if (atype >= PTHREAD_MUTEX_NORMAL && atype <= PTHREAD_MUTEX_ERRORCHECK)
|
||||
{
|
||||
*type = atype;
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return EINVAL;
|
||||
}
|
||||
RTM_EXPORT(pthread_mutexattr_gettype);
|
||||
|
||||
/**
|
||||
* @brief Sets the type attribute of a mutex attributes object.
|
||||
*
|
||||
* This function sets the type of the mutex to be initialized using the
|
||||
* attributes object pointed to by `attr`. The `type` can be one of the
|
||||
* following values:
|
||||
* - `PTHREAD_MUTEX_NORMAL`: Default mutex type. The mutex does not allow
|
||||
* a thread to unlock it if it was not locked by that thread (this results
|
||||
* in undefined behavior).
|
||||
* - `PTHREAD_MUTEX_ERRORCHECK`: Error-checking mutex type. A thread trying to
|
||||
* lock a mutex it already holds will return an error.
|
||||
* - `PTHREAD_MUTEX_RECURSIVE`: Recursive mutex type. The same thread can lock
|
||||
* the mutex multiple times without causing a deadlock, but it must unlock it
|
||||
* the same number of times.
|
||||
*
|
||||
* @param[in,out] attr Pointer to the mutex attributes object.
|
||||
* @param[in] type The type to set for the mutex. One of the following:
|
||||
* - `PTHREAD_MUTEX_NORMAL`
|
||||
* - `PTHREAD_MUTEX_ERRORCHECK`
|
||||
* - `PTHREAD_MUTEX_RECURSIVE`
|
||||
*
|
||||
* @return
|
||||
* - 0 on success.
|
||||
* - Non-zero error code on failure, including:
|
||||
* - `EINVAL`: The specified type is invalid.
|
||||
*
|
||||
* @note
|
||||
* The type must be set before the mutex attributes object is used to
|
||||
* initialize a mutex with `pthread_mutex_init()`.
|
||||
*
|
||||
* @warning
|
||||
* Attempting to set an invalid mutex type will result in an error.
|
||||
*
|
||||
* @see pthread_mutexattr_gettype, pthread_mutexattr_init, pthread_mutex_init
|
||||
*/
|
||||
int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
|
||||
{
|
||||
if (attr && type >= PTHREAD_MUTEX_NORMAL && type <= PTHREAD_MUTEX_ERRORCHECK)
|
||||
{
|
||||
*attr = (*attr & ~MUTEXATTR_TYPE_MASK) | type;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
return EINVAL;
|
||||
}
|
||||
RTM_EXPORT(pthread_mutexattr_settype);
|
||||
|
||||
/**
|
||||
* @brief Sets the shared attribute of a mutex attributes object.
|
||||
*
|
||||
* This function sets the `pshared` attribute of the mutex attributes object
|
||||
* pointed to by `attr`. The `pshared` attribute determines whether the mutex
|
||||
* is shared between threads of the same process or can be shared between
|
||||
* threads of different processes.
|
||||
*
|
||||
* @param[in,out] attr Pointer to the mutex attributes object.
|
||||
* @param[in] pshared The sharing behavior of the mutex. This can be one of the following:
|
||||
* - `PTHREAD_PROCESS_PRIVATE`: The mutex is only shared between threads
|
||||
* of the same process (this is the default behavior).
|
||||
* - `PTHREAD_PROCESS_SHARED`: The mutex can be shared between threads
|
||||
* of different processes (requires the mutex to be allocated in
|
||||
* shared memory).
|
||||
*
|
||||
* @return
|
||||
* - 0 on success.
|
||||
* - Non-zero error code on failure, including:
|
||||
* - `EINVAL`: Invalid `pshared` value or invalid attributes object.
|
||||
*
|
||||
* @note
|
||||
* The `pshared` attribute must be set before the mutex attributes object is
|
||||
* used to initialize a mutex with `pthread_mutex_init()`. For shared mutexes
|
||||
* (`PTHREAD_PROCESS_SHARED`), the mutex object must be allocated in shared memory.
|
||||
*
|
||||
* @warning
|
||||
* Attempting to set an invalid `pshared` value will result in an error.
|
||||
*
|
||||
* @see pthread_mutexattr_getpshared, pthread_mutexattr_init, pthread_mutex_init
|
||||
*/
|
||||
int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared)
|
||||
{
|
||||
if (!attr)
|
||||
return EINVAL;
|
||||
|
||||
switch (pshared)
|
||||
{
|
||||
case PTHREAD_PROCESS_PRIVATE:
|
||||
*attr &= ~MUTEXATTR_SHARED_MASK;
|
||||
return 0;
|
||||
|
||||
case PTHREAD_PROCESS_SHARED:
|
||||
*attr |= MUTEXATTR_SHARED_MASK;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return EINVAL;
|
||||
}
|
||||
RTM_EXPORT(pthread_mutexattr_setpshared);
|
||||
|
||||
/**
|
||||
* @brief Retrieves the shared attribute of a mutex attributes object.
|
||||
*
|
||||
* This function retrieves the `pshared` attribute from the mutex attributes
|
||||
* object pointed to by `attr` and stores it in the integer pointed to by `pshared`.
|
||||
* The `pshared` attribute indicates whether the mutex can be shared between threads
|
||||
* of different processes or only within the same process.
|
||||
*
|
||||
* @param[in] attr Pointer to the mutex attributes object.
|
||||
* @param[out] pshared Pointer to an integer where the shared attribute will be stored.
|
||||
* Possible values are:
|
||||
* - `PTHREAD_PROCESS_PRIVATE`: Mutex is shared only within the same process.
|
||||
* - `PTHREAD_PROCESS_SHARED`: Mutex can be shared between threads of different processes.
|
||||
*
|
||||
* @return
|
||||
* - 0 on success.
|
||||
* - Non-zero error code on failure, including:
|
||||
* - `EINVAL`: Invalid attributes object or the `pshared` pointer is NULL.
|
||||
*
|
||||
* @note
|
||||
* Use this function to check the shared attribute of an already initialized
|
||||
* mutex attributes object.
|
||||
*
|
||||
* @warning
|
||||
* Attempting to get the `pshared` attribute of an uninitialized or invalid
|
||||
* attributes object will result in an error.
|
||||
*
|
||||
* @see pthread_mutexattr_setpshared, pthread_mutexattr_init, pthread_mutex_init
|
||||
*/
|
||||
int pthread_mutexattr_getpshared(pthread_mutexattr_t *attr, int *pshared)
|
||||
{
|
||||
if (!attr || !pshared)
|
||||
return EINVAL;
|
||||
|
||||
*pshared = (*attr & MUTEXATTR_SHARED_MASK) ? PTHREAD_PROCESS_SHARED
|
||||
: PTHREAD_PROCESS_PRIVATE;
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(pthread_mutexattr_getpshared);
|
||||
|
||||
/**
|
||||
* @brief Initializes a mutex with optional attributes.
|
||||
*
|
||||
* This function initializes a mutex object pointed to by `mutex`. The mutex
|
||||
* can optionally be initialized with attributes specified by `attr`. If
|
||||
* `attr` is `NULL`, default attributes are used.
|
||||
*
|
||||
* @param[in,out] mutex Pointer to the mutex to be initialized.
|
||||
* @param[in] attr Pointer to the mutex attributes object. Pass `NULL` to use
|
||||
* default attributes.
|
||||
*
|
||||
* @return
|
||||
* - 0 on success.
|
||||
* - Non-zero error code on failure, including:
|
||||
* - `EINVAL`: Invalid parameters or result.
|
||||
*
|
||||
* @note
|
||||
* The mutex object must be destroyed using `pthread_mutex_destroy()` after it
|
||||
* is no longer needed to free associated resources.
|
||||
*
|
||||
* @warning
|
||||
* A mutex should not be re-initialized while it is already in use.
|
||||
*
|
||||
* @see pthread_mutex_destroy, pthread_mutex_lock, pthread_mutex_unlock
|
||||
*/
|
||||
int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
|
||||
{
|
||||
rt_err_t result;
|
||||
char name[RT_NAME_MAX];
|
||||
static rt_uint16_t pthread_mutex_number = 0;
|
||||
|
||||
if (!mutex)
|
||||
return EINVAL;
|
||||
|
||||
/* build mutex name */
|
||||
rt_snprintf(name, sizeof(name), "pmtx%02d", pthread_mutex_number ++);
|
||||
if (attr == RT_NULL)
|
||||
mutex->attr = pthread_default_mutexattr;
|
||||
else
|
||||
mutex->attr = *attr;
|
||||
|
||||
/* init mutex lock */
|
||||
result = rt_mutex_init(&(mutex->lock), name, RT_IPC_FLAG_PRIO);
|
||||
if (result != RT_EOK)
|
||||
return EINVAL;
|
||||
|
||||
/* detach the object from system object container */
|
||||
rt_object_detach(&(mutex->lock.parent.parent));
|
||||
mutex->lock.parent.parent.type = RT_Object_Class_Mutex;
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(pthread_mutex_init);
|
||||
|
||||
/**
|
||||
* @brief Destroys a mutex object.
|
||||
*
|
||||
* This function releases any resources associated with the mutex object
|
||||
* pointed to by `mutex`. After the mutex has been destroyed, it cannot
|
||||
* be used unless it is re-initialized with `pthread_mutex_init()`.
|
||||
*
|
||||
* @param[in,out] mutex Pointer to the mutex to be destroyed.
|
||||
*
|
||||
* @return
|
||||
* - 0 on success.
|
||||
* - Non-zero error code on failure, including:
|
||||
* - `EBUSY`: The mutex is currently locked or being used by another thread.
|
||||
* - `EINVAL`: The mutex is invalid or has not been initialized.
|
||||
*
|
||||
* @note
|
||||
* Before calling this function, ensure that the mutex is not locked or in use
|
||||
* by any thread. Destroying a locked mutex results in undefined behavior.
|
||||
*
|
||||
* @warning
|
||||
* Attempting to destroy a mutex that is still in use can cause resource leaks
|
||||
* or undefined behavior.
|
||||
*
|
||||
* @see pthread_mutex_init, pthread_mutex_lock, pthread_mutex_unlock
|
||||
*/
|
||||
int pthread_mutex_destroy(pthread_mutex_t *mutex)
|
||||
{
|
||||
if (!mutex || mutex->attr == -1)
|
||||
return EINVAL;
|
||||
|
||||
/* it's busy */
|
||||
if (mutex->lock.owner != RT_NULL)
|
||||
return EBUSY;
|
||||
|
||||
rt_memset(mutex, 0, sizeof(pthread_mutex_t));
|
||||
mutex->attr = -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(pthread_mutex_destroy);
|
||||
|
||||
/**
|
||||
* @brief Locks a mutex.
|
||||
*
|
||||
* This function locks the mutex object pointed to by `mutex`. If the mutex is
|
||||
* already locked by another thread, the calling thread will block until the
|
||||
* mutex becomes available.
|
||||
*
|
||||
* @param[in,out] mutex Pointer to the mutex to be locked.
|
||||
*
|
||||
* @return
|
||||
* - 0 on success.
|
||||
* - Non-zero error code on failure, including:
|
||||
* - `EDEADLK`: A deadlock condition was detected (e.g., the current thread
|
||||
* already holds the mutex in a recursive locking scenario).
|
||||
* - `EINVAL`: The mutex is invalid or uninitialized.
|
||||
*
|
||||
* @note
|
||||
* If the mutex is initialized with the `PTHREAD_MUTEX_RECURSIVE` attribute,
|
||||
* the same thread can lock the mutex multiple times without causing a deadlock.
|
||||
* However, the mutex must be unlocked an equal number of times before it
|
||||
* becomes available to other threads.
|
||||
*
|
||||
* @warning
|
||||
* Attempting to lock an uninitialized or already destroyed mutex results in
|
||||
* undefined behavior.
|
||||
*
|
||||
* @see pthread_mutex_unlock, pthread_mutex_trylock, pthread_mutex_init
|
||||
*/
|
||||
int pthread_mutex_lock(pthread_mutex_t *mutex)
|
||||
{
|
||||
int mtype;
|
||||
rt_err_t result;
|
||||
|
||||
if (!mutex)
|
||||
return EINVAL;
|
||||
|
||||
if (mutex->attr == -1)
|
||||
{
|
||||
/* init mutex */
|
||||
pthread_mutex_init(mutex, RT_NULL);
|
||||
}
|
||||
|
||||
mtype = mutex->attr & MUTEXATTR_TYPE_MASK;
|
||||
rt_enter_critical();
|
||||
if (mutex->lock.owner == rt_thread_self() &&
|
||||
mtype != PTHREAD_MUTEX_RECURSIVE)
|
||||
{
|
||||
rt_exit_critical();
|
||||
|
||||
return EDEADLK;
|
||||
}
|
||||
rt_exit_critical();
|
||||
|
||||
result = rt_mutex_take(&(mutex->lock), RT_WAITING_FOREVER);
|
||||
if (result == RT_EOK)
|
||||
return 0;
|
||||
|
||||
return EINVAL;
|
||||
}
|
||||
RTM_EXPORT(pthread_mutex_lock);
|
||||
|
||||
/**
|
||||
* @brief Unlocks a mutex.
|
||||
*
|
||||
* This function unlocks the mutex object pointed to by `mutex`. If other threads
|
||||
* are blocked waiting for the mutex, one of them will acquire the lock once it is
|
||||
* released. The calling thread must hold the lock on the mutex before calling
|
||||
* this function.
|
||||
*
|
||||
* @param[in,out] mutex Pointer to the mutex to be unlocked.
|
||||
*
|
||||
* @return
|
||||
* - 0 on success.
|
||||
* - Non-zero error code on failure, including:
|
||||
* - `EPERM`: The current thread does not hold the lock on the mutex.
|
||||
* - `EINVAL`: The mutex is invalid or uninitialized.
|
||||
*
|
||||
* @note
|
||||
* If the mutex was initialized with the `PTHREAD_MUTEX_RECURSIVE` attribute,
|
||||
* the mutex will only be unlocked after the calling thread unlocks it as many
|
||||
* times as it was locked.
|
||||
*
|
||||
* @warning
|
||||
* Attempting to unlock an uninitialized, destroyed, or unlocked mutex results
|
||||
* in undefined behavior.
|
||||
*
|
||||
* @see pthread_mutex_lock, pthread_mutex_trylock, pthread_mutex_init
|
||||
*/
|
||||
int pthread_mutex_unlock(pthread_mutex_t *mutex)
|
||||
{
|
||||
rt_err_t result;
|
||||
|
||||
if (!mutex)
|
||||
return EINVAL;
|
||||
if (mutex->attr == -1)
|
||||
{
|
||||
/* init mutex */
|
||||
pthread_mutex_init(mutex, RT_NULL);
|
||||
}
|
||||
|
||||
if (mutex->lock.owner != rt_thread_self())
|
||||
{
|
||||
int mtype;
|
||||
mtype = mutex->attr & MUTEXATTR_TYPE_MASK;
|
||||
|
||||
/* error check, return EPERM */
|
||||
if (mtype == PTHREAD_MUTEX_ERRORCHECK)
|
||||
return EPERM;
|
||||
|
||||
/* no thread waiting on this mutex */
|
||||
if (mutex->lock.owner == RT_NULL)
|
||||
return 0;
|
||||
}
|
||||
|
||||
result = rt_mutex_release(&(mutex->lock));
|
||||
if (result == RT_EOK)
|
||||
return 0;
|
||||
|
||||
return EINVAL;
|
||||
}
|
||||
RTM_EXPORT(pthread_mutex_unlock);
|
||||
|
||||
/**
|
||||
* @brief Attempts to lock a mutex without blocking.
|
||||
*
|
||||
* This function attempts to lock the mutex object pointed to by `mutex`. If the mutex
|
||||
* is already locked by another thread, the function returns immediately with an error
|
||||
* code instead of blocking.
|
||||
*
|
||||
* @param[in,out] mutex Pointer to the mutex to be locked.
|
||||
*
|
||||
* @return
|
||||
* - 0 on success (the mutex was successfully locked).
|
||||
* - Non-zero error code on failure, including:
|
||||
* - `EBUSY`: The mutex is already locked by another thread.
|
||||
* - `EINVAL`: The mutex is invalid or uninitialized.
|
||||
*
|
||||
* @note
|
||||
* This function is useful for implementing non-blocking mutex acquisition. If the mutex
|
||||
* was initialized with the `PTHREAD_MUTEX_RECURSIVE` attribute, the calling thread can
|
||||
* lock it multiple times, but must unlock it the same number of times.
|
||||
*
|
||||
* @warning
|
||||
* Attempting to trylock an uninitialized or destroyed mutex results in undefined behavior.
|
||||
*
|
||||
* @see pthread_mutex_lock, pthread_mutex_unlock, pthread_mutex_init
|
||||
*/
|
||||
int pthread_mutex_trylock(pthread_mutex_t *mutex)
|
||||
{
|
||||
rt_err_t result;
|
||||
int mtype;
|
||||
|
||||
if (!mutex)
|
||||
return EINVAL;
|
||||
if (mutex->attr == -1)
|
||||
{
|
||||
/* init mutex */
|
||||
pthread_mutex_init(mutex, RT_NULL);
|
||||
}
|
||||
|
||||
mtype = mutex->attr & MUTEXATTR_TYPE_MASK;
|
||||
rt_enter_critical();
|
||||
if (mutex->lock.owner == rt_thread_self() &&
|
||||
mtype != PTHREAD_MUTEX_RECURSIVE)
|
||||
{
|
||||
rt_exit_critical();
|
||||
|
||||
return EDEADLK;
|
||||
}
|
||||
rt_exit_critical();
|
||||
|
||||
result = rt_mutex_take(&(mutex->lock), 0);
|
||||
if (result == RT_EOK) return 0;
|
||||
|
||||
return EBUSY;
|
||||
}
|
||||
RTM_EXPORT(pthread_mutex_trylock);
|
||||
|
||||
int pthread_mutexattr_getprioceiling(const pthread_mutexattr_t *attr, int *prioceiling)
|
||||
{
|
||||
return EINVAL;
|
||||
}
|
||||
RTM_EXPORT(pthread_mutexattr_getprioceiling);
|
||||
|
||||
int pthread_mutexattr_setprioceiling(const pthread_mutexattr_t *attr, int prioceiling)
|
||||
{
|
||||
return EINVAL;
|
||||
}
|
||||
RTM_EXPORT(pthread_mutexattr_setprioceiling);
|
||||
|
||||
int pthread_mutexattr_getprotocol(const pthread_mutexattr_t *attr, int *protocol)
|
||||
{
|
||||
return EINVAL;
|
||||
}
|
||||
RTM_EXPORT(pthread_mutexattr_getprotocol);
|
||||
|
||||
int pthread_mutexattr_setprotocol(const pthread_mutexattr_t *attr, int protocol)
|
||||
{
|
||||
return EINVAL;
|
||||
}
|
||||
RTM_EXPORT(pthread_mutexattr_setprotocol);
|
||||
|
||||
int pthread_mutex_getprioceiling(const pthread_mutex_t *mutex, int *prioceiling)
|
||||
{
|
||||
return pthread_mutexattr_getprioceiling(&mutex->attr, prioceiling);
|
||||
}
|
||||
RTM_EXPORT(pthread_mutex_getprioceiling);
|
||||
|
||||
int pthread_mutex_setprioceiling(pthread_mutex_t *mutex, int prioceiling, int *old_ceiling)
|
||||
{
|
||||
*old_ceiling = pthread_mutexattr_getprioceiling(&mutex->attr, old_ceiling);
|
||||
if(*old_ceiling != 0)
|
||||
{
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
return pthread_mutexattr_setprioceiling(&mutex->attr, prioceiling);
|
||||
}
|
||||
RTM_EXPORT(pthread_mutex_setprioceiling);
|
||||
562
RT_Thread/components/libc/posix/pthreads/pthread_rwlock.c
Normal file
562
RT_Thread/components/libc/posix/pthreads/pthread_rwlock.c
Normal file
@ -0,0 +1,562 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2024 RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2010-10-26 Bernard the first version
|
||||
*/
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
int pthread_rwlockattr_init(pthread_rwlockattr_t *attr)
|
||||
{
|
||||
if (!attr)
|
||||
return EINVAL;
|
||||
*attr = PTHREAD_PROCESS_PRIVATE;
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(pthread_rwlockattr_init);
|
||||
|
||||
int pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr)
|
||||
{
|
||||
if (!attr)
|
||||
return EINVAL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(pthread_rwlockattr_destroy);
|
||||
|
||||
int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t *attr,
|
||||
int *pshared)
|
||||
{
|
||||
if (!attr || !pshared)
|
||||
return EINVAL;
|
||||
|
||||
*pshared = PTHREAD_PROCESS_PRIVATE;
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(pthread_rwlockattr_getpshared);
|
||||
|
||||
int pthread_rwlockattr_setpshared(pthread_rwlockattr_t *attr, int pshared)
|
||||
{
|
||||
if (!attr || pshared != PTHREAD_PROCESS_PRIVATE)
|
||||
return EINVAL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(pthread_rwlockattr_setpshared);
|
||||
|
||||
/**
|
||||
* @brief Initializes a read-write lock.
|
||||
*
|
||||
* This function initializes the read-write lock object pointed to by `rwlock` with the
|
||||
* attributes specified by `attr`. If `attr` is `NULL`, the default attributes are used.
|
||||
* A read-write lock allows multiple threads to read or a single thread to write, but not both simultaneously.
|
||||
*
|
||||
* @param rwlock A pointer to the read-write lock object to be initialized.
|
||||
* Must point to valid memory.
|
||||
* @param attr A pointer to the attributes for the read-write lock.
|
||||
* If `NULL`, default attributes are applied.
|
||||
*
|
||||
* @return
|
||||
* - `0` on success.
|
||||
* - A non-zero error code on failure, including:
|
||||
* - `EINVAL`: Invalid attributes.
|
||||
*
|
||||
* @note
|
||||
* - The read-write lock must be destroyed using `pthread_rwlock_destroy()` when it is no longer needed.
|
||||
* - 'rw_mutex' is used for protecting rwlock data.
|
||||
* 'rw_condreaders' is a condition variable for controlling readers.
|
||||
* 'rw_condwriters' is a condition variable for controlling writers.
|
||||
*
|
||||
* @see pthread_rwlock_destroy, pthread_rwlock_rdlock, pthread_rwlock_wrlock, pthread_rwlock_unlock
|
||||
*/
|
||||
int pthread_rwlock_init(pthread_rwlock_t *rwlock,
|
||||
const pthread_rwlockattr_t *attr)
|
||||
{
|
||||
if (!rwlock)
|
||||
return EINVAL;
|
||||
|
||||
rwlock->attr = PTHREAD_PROCESS_PRIVATE;
|
||||
pthread_mutex_init(&(rwlock->rw_mutex), NULL);
|
||||
pthread_cond_init(&(rwlock->rw_condreaders), NULL);
|
||||
pthread_cond_init(&(rwlock->rw_condwriters), NULL);
|
||||
|
||||
rwlock->rw_nwaitwriters = 0;
|
||||
rwlock->rw_nwaitreaders = 0;
|
||||
rwlock->rw_refcount = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(pthread_rwlock_init);
|
||||
|
||||
/**
|
||||
* @brief Destroys a read-write lock.
|
||||
*
|
||||
* This function destroys the read-write lock object pointed to by `rwlock`. After
|
||||
* the lock is destroyed, it cannot be used until it is reinitialized with
|
||||
* `pthread_rwlock_init`. Any threads currently blocked on the lock are affected by the destruction.
|
||||
*
|
||||
* @param rwlock A pointer to the read-write lock object to be destroyed.
|
||||
* Must point to a valid, initialized read-write lock.
|
||||
*
|
||||
* @return
|
||||
* - `0` on success.
|
||||
* - A non-zero error code on failure, including:
|
||||
* - `EINVAL`: The `rwlock` is invalid or uninitialized.
|
||||
* - `EBUSY`: The lock is currently in use by a thread, and cannot be destroyed.
|
||||
*
|
||||
* @note
|
||||
* - The read-write lock must not be in use (i.e., no threads should be blocked on it)
|
||||
* when `pthread_rwlock_destroy` is called.
|
||||
* - Calling this function on an uninitialized or destroyed lock will result in undefined behavior.
|
||||
* - Ensure that all threads have unlocked the lock before attempting to destroy it.
|
||||
*
|
||||
* @see pthread_rwlock_init, pthread_rwlock_rdlock, pthread_rwlock_wrlock, pthread_rwlock_unlock
|
||||
*/
|
||||
int pthread_rwlock_destroy (pthread_rwlock_t *rwlock)
|
||||
{
|
||||
int result;
|
||||
|
||||
if (!rwlock)
|
||||
return EINVAL;
|
||||
if (rwlock->attr == -1)
|
||||
return 0; /* rwlock is not initialized */
|
||||
|
||||
if ( (result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
|
||||
return(result);
|
||||
|
||||
if (rwlock->rw_refcount != 0 ||
|
||||
rwlock->rw_nwaitreaders != 0 ||
|
||||
rwlock->rw_nwaitwriters != 0)
|
||||
{
|
||||
result = EBUSY;
|
||||
|
||||
return result;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* check whether busy */
|
||||
result = rt_sem_trytake(&(rwlock->rw_condreaders.sem));
|
||||
if (result == RT_EOK)
|
||||
{
|
||||
result = rt_sem_trytake(&(rwlock->rw_condwriters.sem));
|
||||
if (result == RT_EOK)
|
||||
{
|
||||
rt_sem_release(&(rwlock->rw_condreaders.sem));
|
||||
rt_sem_release(&(rwlock->rw_condwriters.sem));
|
||||
|
||||
pthread_cond_destroy(&rwlock->rw_condreaders);
|
||||
pthread_cond_destroy(&rwlock->rw_condwriters);
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_sem_release(&(rwlock->rw_condreaders.sem));
|
||||
result = EBUSY;
|
||||
}
|
||||
}
|
||||
else
|
||||
result = EBUSY;
|
||||
}
|
||||
|
||||
pthread_mutex_unlock(&rwlock->rw_mutex);
|
||||
if (result == 0)
|
||||
pthread_mutex_destroy(&rwlock->rw_mutex);
|
||||
|
||||
return result;
|
||||
}
|
||||
RTM_EXPORT(pthread_rwlock_destroy);
|
||||
|
||||
/**
|
||||
* @brief Acquire a read lock on a read-write lock.
|
||||
*
|
||||
* This function locks the specified read-write lock for reading. If the lock
|
||||
* is already held by one or more threads for reading, the calling thread
|
||||
* can acquire the lock as well (shared access). However, if the lock is
|
||||
* held by another writer thread, or other writer thread has been waiting
|
||||
* for the lock, the calling thread will block until the write lock is released.
|
||||
*
|
||||
* @param rwlock A pointer to the read-write lock to be locked.
|
||||
*
|
||||
* @return - 0 on success.
|
||||
* - EINVAL if the rwlock is invalid.
|
||||
* - EDEADLK if a deadlock condition is detected (optional; implementation-dependent).
|
||||
*
|
||||
* @note A thread that has acquired a read lock must eventually release it
|
||||
* using `pthread_rwlock_unlock`. Multiple read locks can be held
|
||||
* simultaneously, but a write lock excludes all other locks.
|
||||
*
|
||||
* @see pthread_rwlock_unlock
|
||||
* @see pthread_rwlock_wrlock
|
||||
* @see pthread_rwlock_tryrdlock
|
||||
*/
|
||||
int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock)
|
||||
{
|
||||
int result;
|
||||
|
||||
if (!rwlock)
|
||||
return EINVAL;
|
||||
if (rwlock->attr == -1)
|
||||
pthread_rwlock_init(rwlock, NULL);
|
||||
|
||||
if ((result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
|
||||
return(result);
|
||||
|
||||
/* give preference to waiting writers */
|
||||
while (rwlock->rw_refcount < 0 || rwlock->rw_nwaitwriters > 0)
|
||||
{
|
||||
rwlock->rw_nwaitreaders++;
|
||||
/* rw_mutex will be released when waiting for rw_condreaders */
|
||||
result = pthread_cond_wait(&rwlock->rw_condreaders, &rwlock->rw_mutex);
|
||||
/* rw_mutex should have been taken again when returned from waiting */
|
||||
rwlock->rw_nwaitreaders--;
|
||||
if (result != 0) /* wait error */
|
||||
break;
|
||||
}
|
||||
|
||||
/* another reader has a read lock */
|
||||
if (result == 0)
|
||||
rwlock->rw_refcount++;
|
||||
|
||||
pthread_mutex_unlock(&rwlock->rw_mutex);
|
||||
|
||||
return (result);
|
||||
}
|
||||
RTM_EXPORT(pthread_rwlock_rdlock);
|
||||
|
||||
/**
|
||||
* @brief Try to acquire a read lock on a read-write lock without blocking.
|
||||
*
|
||||
* This function attempts to acquire a read lock on the specified read-write lock.
|
||||
* If the lock is already held for writing, the function will return immediately
|
||||
* without blocking the calling thread. If the lock is available for reading,
|
||||
* it will be acquired successfully.
|
||||
*
|
||||
* @param rwlock A pointer to the read-write lock to attempt to lock.
|
||||
*
|
||||
* @return - 0 on success, indicating the read lock was acquired.
|
||||
* - EBUSY if the lock is currently held for writing by another thread.
|
||||
* - EINVAL if the rwlock is invalid.
|
||||
*
|
||||
* @note This function is non-blocking and returns immediately if the lock
|
||||
* cannot be acquired. After successfully acquiring the read lock,
|
||||
* the thread must release it using `pthread_rwlock_unlock`.
|
||||
*
|
||||
* @see pthread_rwlock_unlock
|
||||
* @see pthread_rwlock_rdlock
|
||||
* @see pthread_rwlock_trywrlock
|
||||
*/
|
||||
int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock)
|
||||
{
|
||||
int result;
|
||||
|
||||
if (!rwlock)
|
||||
return EINVAL;
|
||||
if (rwlock->attr == -1)
|
||||
pthread_rwlock_init(rwlock, NULL);
|
||||
|
||||
if ((result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
|
||||
return(result);
|
||||
|
||||
if (rwlock->rw_refcount < 0 || rwlock->rw_nwaitwriters > 0)
|
||||
result = EBUSY; /* held by a writer or waiting writers */
|
||||
else
|
||||
rwlock->rw_refcount++; /* increment count of reader locks */
|
||||
|
||||
pthread_mutex_unlock(&rwlock->rw_mutex);
|
||||
|
||||
return(result);
|
||||
}
|
||||
RTM_EXPORT(pthread_rwlock_tryrdlock);
|
||||
|
||||
/**
|
||||
* @brief Acquire a read lock on a read-write lock with a timeout.
|
||||
*
|
||||
* This function attempts to lock the specified read-write lock for reading,
|
||||
* blocking until the lock becomes available or the specified timeout expires.
|
||||
* If the lock is held for writing by another thread, the calling thread will
|
||||
* block, but only up to the time specified by `abstime`.
|
||||
*
|
||||
* @param rwlock A pointer to the read-write lock to be locked.
|
||||
* @param abstime A pointer to a `timespec` structure specifying the
|
||||
* absolute timeout (in seconds and nanoseconds since the
|
||||
* Epoch, 1970-01-01 00:00:00 UTC).
|
||||
*
|
||||
* @return - 0 on success, indicating the read lock was acquired.
|
||||
* - ETIMEDOUT if the timeout expired before the lock could be acquired.
|
||||
* - EINVAL if the `rwlock` is invalid or `abstime` contains invalid values.
|
||||
* - EDEADLK if a deadlock condition is detected (optional; implementation-dependent).
|
||||
*
|
||||
* @note The timeout is specified as an absolute time (not relative). After
|
||||
* acquiring the read lock, the thread must release it using
|
||||
* `pthread_rwlock_unlock`.
|
||||
*
|
||||
* @warning If the system clock is changed (e.g., via manual adjustment or
|
||||
* NTP synchronization), the timeout behavior may be affected.
|
||||
*
|
||||
* @see pthread_rwlock_unlock
|
||||
* @see pthread_rwlock_rdlock
|
||||
* @see pthread_rwlock_tryrdlock
|
||||
*/
|
||||
int pthread_rwlock_timedrdlock(pthread_rwlock_t *rwlock,
|
||||
const struct timespec *abstime)
|
||||
{
|
||||
int result;
|
||||
|
||||
if (!rwlock)
|
||||
return EINVAL;
|
||||
if (rwlock->attr == -1)
|
||||
pthread_rwlock_init(rwlock, NULL);
|
||||
|
||||
if ( (result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
|
||||
return(result);
|
||||
|
||||
/* give preference to waiting writers */
|
||||
while (rwlock->rw_refcount < 0 || rwlock->rw_nwaitwriters > 0)
|
||||
{
|
||||
rwlock->rw_nwaitreaders++;
|
||||
/* rw_mutex will be released when waiting for rw_condreaders */
|
||||
result = pthread_cond_timedwait(&rwlock->rw_condreaders, &rwlock->rw_mutex, abstime);
|
||||
/* rw_mutex should have been taken again when returned from waiting */
|
||||
rwlock->rw_nwaitreaders--;
|
||||
if (result != 0)
|
||||
break;
|
||||
}
|
||||
|
||||
/* another reader has a read lock */
|
||||
if (result == 0)
|
||||
rwlock->rw_refcount++;
|
||||
|
||||
pthread_mutex_unlock(&rwlock->rw_mutex);
|
||||
|
||||
return (result);
|
||||
}
|
||||
RTM_EXPORT(pthread_rwlock_timedrdlock);
|
||||
|
||||
/**
|
||||
* @brief Acquire a write lock on a read-write lock with a timeout.
|
||||
*
|
||||
* This function attempts to acquire a write lock on the specified read-write lock,
|
||||
* blocking until the lock becomes available or the specified timeout expires.
|
||||
* If the lock is already held for reading or writing by another thread, the
|
||||
* calling thread will block, but only up to the time specified by `abstime`.
|
||||
*
|
||||
* @param rwlock A pointer to the read-write lock to be locked.
|
||||
* @param abstime A pointer to a `timespec` structure specifying the
|
||||
* absolute timeout (in seconds and nanoseconds since the
|
||||
* Epoch, 1970-01-01 00:00:00 UTC).
|
||||
*
|
||||
* @return
|
||||
* - 0 on success, indicating the write lock was acquired.
|
||||
* - EINVAL if the `rwlock` is invalid or `abstime` contains invalid values.
|
||||
* - EDEADLK if a deadlock condition is detected (optional; implementation-dependent).
|
||||
*
|
||||
* @note The timeout is specified as an absolute time (not relative). After
|
||||
* acquiring the write lock, the thread must release it using
|
||||
* `pthread_rwlock_unlock`.
|
||||
*
|
||||
* @warning If the system clock is adjusted (e.g., manually or via NTP synchronization),
|
||||
* the timeout behavior may be affected.
|
||||
*
|
||||
* @see pthread_rwlock_unlock
|
||||
* @see pthread_rwlock_wrlock
|
||||
* @see pthread_rwlock_trywrlock
|
||||
*/
|
||||
int pthread_rwlock_timedwrlock(pthread_rwlock_t *rwlock,
|
||||
const struct timespec *abstime)
|
||||
{
|
||||
int result;
|
||||
|
||||
if (!rwlock)
|
||||
return EINVAL;
|
||||
if (rwlock->attr == -1)
|
||||
pthread_rwlock_init(rwlock, NULL);
|
||||
|
||||
if ((result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
|
||||
return(result);
|
||||
|
||||
while (rwlock->rw_refcount != 0)
|
||||
{
|
||||
rwlock->rw_nwaitwriters++;
|
||||
/* rw_mutex will be released when waiting for rw_condwriters */
|
||||
result = pthread_cond_timedwait(&rwlock->rw_condwriters, &rwlock->rw_mutex, abstime);
|
||||
/* rw_mutex should have been taken again when returned from waiting */
|
||||
rwlock->rw_nwaitwriters--;
|
||||
|
||||
if (result != 0)
|
||||
break;
|
||||
}
|
||||
|
||||
if (result == 0)
|
||||
rwlock->rw_refcount = -1;
|
||||
|
||||
pthread_mutex_unlock(&rwlock->rw_mutex);
|
||||
|
||||
return(result);
|
||||
}
|
||||
RTM_EXPORT(pthread_rwlock_timedwrlock);
|
||||
|
||||
/**
|
||||
* @brief Try to acquire a write lock on a read-write lock without blocking.
|
||||
*
|
||||
* This function attempts to acquire a write lock on the specified read-write lock.
|
||||
* If the lock is already held for reading or writing by another thread, the function
|
||||
* will return immediately without blocking the calling thread. If the lock is
|
||||
* available, it will be acquired successfully.
|
||||
*
|
||||
* @param rwlock A pointer to the read-write lock to attempt to lock.
|
||||
*
|
||||
* @return
|
||||
* - 0 on success, indicating the write lock was acquired.
|
||||
* - EBUSY if the lock is currently held by another thread (read or write lock).
|
||||
* - EINVAL if the `rwlock` is invalid.
|
||||
*
|
||||
* @note This function is non-blocking and returns immediately if the lock cannot
|
||||
* be acquired. After successfully acquiring the write lock, the thread must
|
||||
* release it using `pthread_rwlock_unlock`.
|
||||
*
|
||||
* @see pthread_rwlock_unlock
|
||||
* @see pthread_rwlock_wrlock
|
||||
* @see pthread_rwlock_timedwrlock
|
||||
*/
|
||||
int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock)
|
||||
{
|
||||
int result;
|
||||
|
||||
if (!rwlock)
|
||||
return EINVAL;
|
||||
if (rwlock->attr == -1)
|
||||
pthread_rwlock_init(rwlock, NULL);
|
||||
|
||||
if ((result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
|
||||
return(result);
|
||||
|
||||
if (rwlock->rw_refcount != 0)
|
||||
result = EBUSY; /* held by either writer or reader(s) */
|
||||
else
|
||||
rwlock->rw_refcount = -1; /* available, indicate a writer has it */
|
||||
|
||||
pthread_mutex_unlock(&rwlock->rw_mutex);
|
||||
|
||||
return(result);
|
||||
}
|
||||
RTM_EXPORT(pthread_rwlock_trywrlock);
|
||||
|
||||
/**
|
||||
* @brief Release a read or write lock on a read-write lock.
|
||||
*
|
||||
* This function unlocks the specified read-write lock, releasing either a read
|
||||
* lock or a write lock held by the calling thread. If the calling thread does
|
||||
* not hold the lock, the behavior is undefined.
|
||||
*
|
||||
* @param rwlock A pointer to the read-write lock to be unlocked.
|
||||
*
|
||||
* @return
|
||||
* - 0 on success.
|
||||
* - EINVAL if the `rwlock` is invalid.
|
||||
*
|
||||
* @note
|
||||
* - This function must only be called by the thread that successfully acquired
|
||||
* the lock.
|
||||
*
|
||||
* @see pthread_rwlock_rdlock
|
||||
* @see pthread_rwlock_wrlock
|
||||
* @see pthread_rwlock_tryrdlock
|
||||
* @see pthread_rwlock_trywrlock
|
||||
*/
|
||||
int pthread_rwlock_unlock(pthread_rwlock_t *rwlock)
|
||||
{
|
||||
int result;
|
||||
|
||||
if (!rwlock)
|
||||
return EINVAL;
|
||||
if (rwlock->attr == -1)
|
||||
pthread_rwlock_init(rwlock, NULL);
|
||||
|
||||
if ( (result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
|
||||
return(result);
|
||||
|
||||
if (rwlock->rw_refcount > 0)
|
||||
rwlock->rw_refcount--; /* releasing a reader */
|
||||
else if (rwlock->rw_refcount == -1)
|
||||
rwlock->rw_refcount = 0; /* releasing a writer */
|
||||
|
||||
/* give preference to waiting writers over waiting readers */
|
||||
if (rwlock->rw_nwaitwriters > 0)
|
||||
{
|
||||
if (rwlock->rw_refcount == 0)
|
||||
result = pthread_cond_signal(&rwlock->rw_condwriters);
|
||||
}
|
||||
else if (rwlock->rw_nwaitreaders > 0)
|
||||
{
|
||||
result = pthread_cond_broadcast(&rwlock->rw_condreaders);
|
||||
}
|
||||
|
||||
pthread_mutex_unlock(&rwlock->rw_mutex);
|
||||
|
||||
return(result);
|
||||
}
|
||||
RTM_EXPORT(pthread_rwlock_unlock);
|
||||
|
||||
/**
|
||||
* @brief Acquire a write lock on a read-write lock.
|
||||
*
|
||||
* This function locks the specified read-write lock for writing. If the lock
|
||||
* is already held by another thread for reading or writing, the calling thread
|
||||
* blocks until the lock becomes available.
|
||||
*
|
||||
* @param rwlock A pointer to the read-write lock to be locked.
|
||||
*
|
||||
* @return
|
||||
* - 0 on success, indicating the write lock was acquired.
|
||||
* - EINVAL if the `rwlock` is invalid.
|
||||
* - EDEADLK if a deadlock condition is detected (optional; implementation-dependent).
|
||||
*
|
||||
* @note
|
||||
* - A write lock is exclusive, meaning no other thread can acquire a read or
|
||||
* write lock while a write lock is held.
|
||||
* - The thread that successfully acquires the write lock must release it using
|
||||
* `pthread_rwlock_unlock`.
|
||||
*
|
||||
* @see pthread_rwlock_unlock
|
||||
* @see pthread_rwlock_trywrlock
|
||||
* @see pthread_rwlock_timedwrlock
|
||||
* @see pthread_rwlock_rdlock
|
||||
*/
|
||||
int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock)
|
||||
{
|
||||
int result;
|
||||
|
||||
if (!rwlock)
|
||||
return EINVAL;
|
||||
if (rwlock->attr == -1)
|
||||
pthread_rwlock_init(rwlock, NULL);
|
||||
|
||||
if ((result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
|
||||
return(result);
|
||||
|
||||
while (rwlock->rw_refcount != 0)
|
||||
{
|
||||
rwlock->rw_nwaitwriters++;
|
||||
/* rw_mutex will be released when waiting for rw_condwriters */
|
||||
result = pthread_cond_wait(&rwlock->rw_condwriters, &rwlock->rw_mutex);
|
||||
/* rw_mutex should have been taken again when returned from waiting */
|
||||
rwlock->rw_nwaitwriters--;
|
||||
|
||||
if (result != 0)
|
||||
break;
|
||||
}
|
||||
|
||||
if (result == 0)
|
||||
rwlock->rw_refcount = -1;
|
||||
|
||||
pthread_mutex_unlock(&rwlock->rw_mutex);
|
||||
|
||||
return(result);
|
||||
}
|
||||
RTM_EXPORT(pthread_rwlock_wrlock);
|
||||
|
||||
69
RT_Thread/components/libc/posix/pthreads/pthread_spin.c
Normal file
69
RT_Thread/components/libc/posix/pthreads/pthread_spin.c
Normal file
@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2021, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2010-10-26 Bernard the first version
|
||||
*/
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
int pthread_spin_init (pthread_spinlock_t *lock, int pshared)
|
||||
{
|
||||
if (!lock)
|
||||
return EINVAL;
|
||||
|
||||
lock->lock = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pthread_spin_destroy (pthread_spinlock_t *lock)
|
||||
{
|
||||
if (!lock)
|
||||
return EINVAL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pthread_spin_lock (pthread_spinlock_t *lock)
|
||||
{
|
||||
if (!lock)
|
||||
return EINVAL;
|
||||
|
||||
while (!(lock->lock))
|
||||
{
|
||||
lock->lock = 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pthread_spin_trylock (pthread_spinlock_t *lock)
|
||||
{
|
||||
if (!lock)
|
||||
return EINVAL;
|
||||
|
||||
if (!(lock->lock))
|
||||
{
|
||||
lock->lock = 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
return EBUSY;
|
||||
}
|
||||
|
||||
int pthread_spin_unlock (pthread_spinlock_t *lock)
|
||||
{
|
||||
if (!lock)
|
||||
return EINVAL;
|
||||
if (!(lock->lock))
|
||||
return EPERM;
|
||||
|
||||
lock->lock = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
218
RT_Thread/components/libc/posix/pthreads/pthread_tls.c
Normal file
218
RT_Thread/components/libc/posix/pthreads/pthread_tls.c
Normal file
@ -0,0 +1,218 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2024 RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2010-10-26 Bernard the first version
|
||||
*/
|
||||
|
||||
#include <pthread.h>
|
||||
#include "pthread_internal.h"
|
||||
|
||||
_pthread_key_data_t _thread_keys[PTHREAD_KEY_MAX];
|
||||
|
||||
/* initialize key area */
|
||||
static int pthread_key_system_init(void)
|
||||
{
|
||||
rt_memset(&_thread_keys[0], 0, sizeof(_thread_keys));
|
||||
return 0;
|
||||
}
|
||||
INIT_COMPONENT_EXPORT(pthread_key_system_init);
|
||||
|
||||
/**
|
||||
* @brief Retrieves the value associated with a thread-specific data key for the calling thread.
|
||||
*
|
||||
* This function returns the value that was previously set for the specified key
|
||||
* in the calling thread using `pthread_setspecific`. Each thread has its own independent
|
||||
* value for the same key.
|
||||
*
|
||||
* @param[in] key
|
||||
* The thread-specific data key, created using `pthread_key_create`.
|
||||
*
|
||||
* @return
|
||||
* - The value associated with the key for the calling thread, or `NULL` if no value
|
||||
* has been set for the key in this thread.
|
||||
*
|
||||
* @note
|
||||
* - If no value has been set for the key in the calling thread, `pthread_getspecific`
|
||||
* returns `NULL`. This does not indicate an error unless `NULL` was explicitly set as the value.
|
||||
* - The value retrieved is specific to the calling thread and may differ for other threads.
|
||||
*
|
||||
* @attention
|
||||
* - If the key has been deleted using `pthread_key_delete`, the behavior of this
|
||||
* function is undefined.
|
||||
*
|
||||
* @see pthread_key_create(), pthread_setspecific(), pthread_key_delete()
|
||||
*/
|
||||
void *pthread_getspecific(pthread_key_t key)
|
||||
{
|
||||
struct _pthread_data* ptd;
|
||||
|
||||
if (rt_thread_self() == NULL) return NULL;
|
||||
|
||||
/* get pthread data from user data of thread */
|
||||
ptd = (_pthread_data_t *)rt_thread_self()->pthread_data;
|
||||
RT_ASSERT(ptd != NULL);
|
||||
|
||||
if (ptd->tls == NULL)
|
||||
return NULL;
|
||||
|
||||
if ((key < PTHREAD_KEY_MAX) && (_thread_keys[key].is_used))
|
||||
return ptd->tls[key];
|
||||
|
||||
return NULL;
|
||||
}
|
||||
RTM_EXPORT(pthread_getspecific);
|
||||
|
||||
/**
|
||||
* @brief Associates a value with a thread-specific data key for the calling thread.
|
||||
*
|
||||
* This function sets a thread-specific value for the given key. Each thread has its
|
||||
* own independent value associated with the same key, and this value is not shared with
|
||||
* other threads.
|
||||
*
|
||||
* @param[in] key
|
||||
* The thread-specific data key, created using `pthread_key_create`.
|
||||
* @param[in] value
|
||||
* The value to associate with the key for the calling thread. The value can be
|
||||
* a pointer to any data or `NULL`.
|
||||
*
|
||||
* @return
|
||||
* - `0`: The value was successfully set.
|
||||
* - `EINVAL`: The specified key is invalid or not initialized.
|
||||
*
|
||||
* @note
|
||||
* - Setting a new value for a key in a thread overwrites any previously set value
|
||||
* for that key in the same thread.
|
||||
* - The value set using `pthread_setspecific` is accessible via `pthread_getspecific`
|
||||
* for the same thread.
|
||||
* - The association between the key and value is valid until the thread terminates,
|
||||
* the key is deleted using `pthread_key_delete`, or a new value is set with this function.
|
||||
*
|
||||
* @attention
|
||||
* - The value is specific to the calling thread; other threads will not be affected
|
||||
* and will continue to maintain their own independent values for the same key.
|
||||
*
|
||||
* @see pthread_key_create(), pthread_getspecific(), pthread_key_delete()
|
||||
*/
|
||||
int pthread_setspecific(pthread_key_t key, const void *value)
|
||||
{
|
||||
struct _pthread_data* ptd;
|
||||
|
||||
if (rt_thread_self() == NULL) return EINVAL;
|
||||
|
||||
/* get pthread data from user data of thread */
|
||||
ptd = (_pthread_data_t *)rt_thread_self()->pthread_data;
|
||||
RT_ASSERT(ptd != NULL);
|
||||
|
||||
/* check tls area */
|
||||
if (ptd->tls == NULL)
|
||||
{
|
||||
ptd->tls = (void**)rt_calloc(PTHREAD_KEY_MAX, sizeof(void*));
|
||||
}
|
||||
|
||||
if ((key < PTHREAD_KEY_MAX) && _thread_keys[key].is_used)
|
||||
{
|
||||
ptd->tls[key] = (void *)value;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
return EINVAL;
|
||||
}
|
||||
RTM_EXPORT(pthread_setspecific);
|
||||
|
||||
/**
|
||||
* @brief Creates a thread-specific data key.
|
||||
*
|
||||
* This function allocates a unique key for thread-specific data (TSD).
|
||||
* Each thread can use this key to access its own specific data associated with it.
|
||||
*
|
||||
* @param[out] key
|
||||
* A pointer to a variable where the newly created key will be stored.
|
||||
* On success, `*key` will hold the newly allocated key.
|
||||
* @param[in] destructor
|
||||
* An optional destructor function pointer. This function will be called to
|
||||
* clean up thread-specific data associated with the key when a thread terminates.
|
||||
* Pass `NULL` if no cleanup is required.
|
||||
*
|
||||
* @return
|
||||
* - `0`: The key was successfully created.
|
||||
* - `EAGAIN`: The system has reached the maximum number of available keys, and no new key can be allocated.
|
||||
*
|
||||
* @note
|
||||
* - Each thread can use `pthread_setspecific` and `pthread_getspecific` to set and retrieve the thread-specific data associated with the key.
|
||||
* - The destructor function will be invoked automatically by the system when the thread terminates, if not explicitly called.
|
||||
*
|
||||
* @attention
|
||||
* - If the `destructor` function is invoked and it sets thread-specific data for the same key,
|
||||
* the destructor may be called multiple times, up to a limit defined by the system
|
||||
* (typically `PTHREAD_DESTRUCTOR_ITERATIONS`).
|
||||
*
|
||||
* @see pthread_setspecific(), pthread_getspecific(), pthread_key_delete()
|
||||
*/
|
||||
int pthread_key_create(pthread_key_t *key, void (*destructor)(void*))
|
||||
{
|
||||
rt_uint32_t index;
|
||||
|
||||
rt_enter_critical();
|
||||
for (index = 0; index < PTHREAD_KEY_MAX; index ++)
|
||||
{
|
||||
if (_thread_keys[index].is_used == 0)
|
||||
{
|
||||
_thread_keys[index].is_used = 1;
|
||||
_thread_keys[index].destructor = destructor;
|
||||
|
||||
*key = index;
|
||||
|
||||
rt_exit_critical();
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
rt_exit_critical();
|
||||
|
||||
return EAGAIN;
|
||||
}
|
||||
RTM_EXPORT(pthread_key_create);
|
||||
|
||||
/**
|
||||
* @brief Deletes a thread-specific data key.
|
||||
*
|
||||
* This function deletes a previously created thread-specific data key.
|
||||
* The key will no longer be valid for use in `pthread_setspecific` or `pthread_getspecific`.
|
||||
*
|
||||
* @param[in] key
|
||||
* The thread-specific data key to delete. The key must have been created with
|
||||
* `pthread_key_create`.
|
||||
*
|
||||
* @return
|
||||
* - `0`: The key was successfully deleted.
|
||||
* - `EINVAL`: The specified key is invalid or has not been initialized.
|
||||
*
|
||||
* @note
|
||||
* - Deleting a key does not automatically free or clean up the thread-specific data
|
||||
* associated with the key. It is the programmer's responsibility to ensure that
|
||||
* associated resources are properly released, if necessary.
|
||||
* - After deletion, using the key in `pthread_setspecific` or `pthread_getspecific`
|
||||
* will result in undefined behavior.
|
||||
*
|
||||
* @see pthread_key_create(), pthread_setspecific(), pthread_getspecific()
|
||||
*/
|
||||
int pthread_key_delete(pthread_key_t key)
|
||||
{
|
||||
if (key >= PTHREAD_KEY_MAX)
|
||||
return EINVAL;
|
||||
|
||||
rt_enter_critical();
|
||||
_thread_keys[key].is_used = 0;
|
||||
_thread_keys[key].destructor = 0;
|
||||
rt_exit_critical();
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(pthread_key_delete);
|
||||
|
||||
57
RT_Thread/components/libc/posix/pthreads/sched.c
Normal file
57
RT_Thread/components/libc/posix/pthreads/sched.c
Normal file
@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2021, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
*/
|
||||
|
||||
#include <sched.h>
|
||||
|
||||
int sched_yield(void)
|
||||
{
|
||||
rt_thread_yield();
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(sched_yield);
|
||||
|
||||
int sched_get_priority_min(int policy)
|
||||
{
|
||||
if (policy != SCHED_FIFO && policy != SCHED_RR)
|
||||
return EINVAL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(sched_get_priority_min);
|
||||
|
||||
int sched_get_priority_max(int policy)
|
||||
{
|
||||
if (policy != SCHED_FIFO && policy != SCHED_RR)
|
||||
return EINVAL;
|
||||
|
||||
return RT_THREAD_PRIORITY_MAX - 1;
|
||||
}
|
||||
RTM_EXPORT(sched_get_priority_max);
|
||||
|
||||
int sched_setscheduler(pid_t pid, int policy)
|
||||
{
|
||||
return EOPNOTSUPP;
|
||||
}
|
||||
RTM_EXPORT(sched_setscheduler);
|
||||
|
||||
int sched_rr_get_interval(pid_t pid, struct timespec *tp)
|
||||
{
|
||||
if(pid != 0)
|
||||
{
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
rt_set_errno(-EINVAL);
|
||||
|
||||
/* course model, don't support */
|
||||
// TODO
|
||||
return -1;
|
||||
}
|
||||
RTM_EXPORT(sched_rr_get_interval);
|
||||
41
RT_Thread/components/libc/posix/pthreads/sched.h
Normal file
41
RT_Thread/components/libc/posix/pthreads/sched.h
Normal file
@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2021, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
*/
|
||||
|
||||
#ifndef __SCHED_H__
|
||||
#define __SCHED_H__
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <pthread.h>
|
||||
|
||||
/* Thread scheduling policies */
|
||||
enum
|
||||
{
|
||||
SCHED_OTHER = 0,
|
||||
SCHED_FIFO,
|
||||
SCHED_RR,
|
||||
SCHED_MIN = SCHED_OTHER,
|
||||
SCHED_MAX = SCHED_RR
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
int sched_yield(void);
|
||||
int sched_get_priority_min(int policy);
|
||||
int sched_get_priority_max(int policy);
|
||||
int sched_rr_get_interval(pid_t pid, struct timespec *tp);
|
||||
int sched_setscheduler(pid_t pid, int policy);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user