From bb8ca29e027e0e485f8fb645844bf90c72a5d879 Mon Sep 17 00:00:00 2001 From: chao an Date: Tue, 24 Oct 2023 22:16:00 +0800 Subject: [PATCH] sched/pthread/barrierwait: replace syscall(2) to kernel api syscall(2) cannot be called from kernel space Signed-off-by: chao an --- sched/pthread/pthread_barrierwait.c | 62 ++++++++++++++--------------- 1 file changed, 29 insertions(+), 33 deletions(-) diff --git a/sched/pthread/pthread_barrierwait.c b/sched/pthread/pthread_barrierwait.c index add1748b56c61..b3e5b47238bf5 100644 --- a/sched/pthread/pthread_barrierwait.c +++ b/sched/pthread/pthread_barrierwait.c @@ -25,8 +25,8 @@ #include #include +#include #include -#include #include #include @@ -80,11 +80,11 @@ int pthread_barrier_wait(FAR pthread_barrier_t *barrier) { - int semcount; - int ret = OK; irqstate_t flags; + int semcount; + int ret; - if (!barrier) + if (barrier == NULL) { return EINVAL; } @@ -95,11 +95,11 @@ int pthread_barrier_wait(FAR pthread_barrier_t *barrier) /* Find out how many threads are already waiting at the barrier */ - ret = sem_getvalue(&barrier->sem, &semcount); + ret = nxsem_get_value(&barrier->sem, &semcount); if (ret != OK) { leave_critical_section(flags); - return get_errno(); + return -ret; } /* If the number of waiters would be equal to the count, then we are done */ @@ -110,8 +110,8 @@ int pthread_barrier_wait(FAR pthread_barrier_t *barrier) while (semcount < 0) { - sem_post(&barrier->sem); - sem_getvalue(&barrier->sem, &semcount); + nxsem_post(&barrier->sem); + nxsem_get_value(&barrier->sem, &semcount); } /* Then return PTHREAD_BARRIER_SERIAL_THREAD to the final thread */ @@ -119,32 +119,28 @@ int pthread_barrier_wait(FAR pthread_barrier_t *barrier) leave_critical_section(flags); return PTHREAD_BARRIER_SERIAL_THREAD; } - else - { - /* Otherwise, this thread must wait as well */ - while (sem_wait(&barrier->sem) != OK) - { - /* If the thread is awakened by a signal, just continue to wait */ - - int errornumber = get_errno(); - if (errornumber != EINTR) - { - /* If it is awakened by some other error, then there is a - * problem - */ - - leave_critical_section(flags); - return errornumber; - } - } + /* Otherwise, this thread must wait as well */ - /* We will only get here when we are one of the N-1 threads that were - * waiting for the final thread at the barrier. We just need to return - * zero. - */ + while ((ret = nxsem_wait(&barrier->sem)) != OK) + { + /* If the thread is awakened by a signal, just continue to wait */ - leave_critical_section(flags); - return 0; - } + if (ret != -EINTR) + { + /* If it is awakened by some other error, then there is a + * problem + */ + + break; + } + } + + /* We will only get here when we are one of the N-1 threads that were + * waiting for the final thread at the barrier. We just need to return + * zero. + */ + + leave_critical_section(flags); + return -ret; }