瀏覽代碼

[components/libc/posix]fix the problem that it doesn't check if barrier still in use and dosen't destory the mutex either.

ligr 5 月之前
父節點
當前提交
bb0a1ea453
共有 1 個文件被更改,包括 24 次插入0 次删除
  1. 24 0
      components/libc/posix/pthreads/pthread_barrier.c

+ 24 - 0
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;