diff options
| author | ligr <[email protected]> | 2024-11-21 11:16:21 +0800 |
|---|---|---|
| committer | Meco Man <[email protected]> | 2024-11-21 19:22:44 -0500 |
| commit | bb0a1ea45350986d14e985b1ba0de9c1ec7a650d (patch) | |
| tree | ba4ff67b7c1ed5d90b836b18dadb6b881b36c897 /components/libc | |
| parent | 0bac8c4f90ebefc16decc8b1c4e96c9802a79a9c (diff) | |
[components/libc/posix]fix the problem that it doesn't check if barrier still in use and dosen't destory the mutex either.
Diffstat (limited to 'components/libc')
| -rw-r--r-- | components/libc/posix/pthreads/pthread_barrier.c | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/components/libc/posix/pthreads/pthread_barrier.c b/components/libc/posix/pthreads/pthread_barrier.c index f2c0e2d1c3..8802aaedbd 100644 --- a/components/libc/posix/pthreads/pthread_barrier.c +++ b/components/libc/posix/pthreads/pthread_barrier.c @@ -86,6 +86,30 @@ int pthread_barrier_destroy(pthread_barrier_t *barrier) 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; |
