Skip to content

Commit

Permalink
include/threads.h: Replace thrd_ defines by actual function definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
ajuckler authored and xiaoxiang781216 committed Dec 19, 2024
1 parent ecdff65 commit 1f2946b
Showing 1 changed file with 35 additions and 8 deletions.
43 changes: 35 additions & 8 deletions include/threads.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <pthread.h>
#include <time.h>
#include <errno.h>
#include <stdnoreturn.h>

/****************************************************************************
* Pre-processor Definitions
Expand Down Expand Up @@ -107,22 +108,35 @@ typedef CODE void (*tss_dtor_t)(FAR void *);
* int thrd_create(FAR thrd_t *thr, thrd_start_t func, FAR void *arg);
*/

#define thrd_create(thr,func,arg) \
pthread_create(thr,NULL,(pthread_startroutine_t)(func),arg)
static inline int thrd_create(FAR thrd_t *thr,
thrd_start_t func,
FAR void *arg)
{
return pthread_create(thr,
NULL,
(pthread_startroutine_t)func,
arg);
}

/* thrd_equal: checks if two identifiers refer to the same thread
*
* int thrd_equal(thrd_t lhs, thrd_t rhs);
*/

#define thrd_equal(lhs,rhs) ((lhs) == (rhs))
static inline int thrd_equal(thrd_t lhs, thrd_t rhs)
{
return (lhs == rhs) ? 1 : 0;
}

/* thrd_current: obtains the current thread identifier
*
* thrd_t thrd_current(void);
*/

#define thrd_current() ((thrd_t)_SCHED_GETTID())
static inline thrd_t thrd_current(void)
{
return pthread_self();
}

/* thrd_sleep: suspends execution of the calling thread for the given
* period of time
Expand All @@ -131,28 +145,41 @@ typedef CODE void (*tss_dtor_t)(FAR void *);
* FAR struct timespec *remaining);
*/

#define thrd_sleep(rqtp,rmtp) nanosleep(rqtp,rmtp)
static inline int thrd_sleep(FAR const struct timespec *time_point,
FAR struct timespec *remaining)
{
return nanosleep(time_point, remaining);
}

/* thrd_yield: yields the current time slice
*
* void thrd_yield(void);
*/

#define thrd_yield() pthread_yield()
static inline void thrd_yield(void)
{
pthread_yield();
}

/* thrd_exit: terminates the calling thread
*
* _Noreturn void thrd_exit(int res);
*/

#define thrd_exit(res) pthread_exit((pthread_addr_t)(res))
static inline noreturn void thrd_exit(int res)
{
pthread_exit((pthread_addr_t)res);
}

/* thrd_detach: detaches a thread
*
* int thrd_detach(thrd_t thr);
*/

#define thrd_detach(thr) pthread_detach(thr)
static inline int thrd_detach(thrd_t thr)
{
return pthread_detach(thr);
}

/* thrd_join: blocks until a thread terminates
*
Expand Down

0 comments on commit 1f2946b

Please sign in to comment.