[FFmpeg-devel] [PATCH 1/3] compat/os2threads: Improve pthread_cond_xxx() functions

KO Myung-Hun komh78 at gmail.com
Sun Feb 14 04:59:54 CET 2016


1. Manipulate waiting count in pthread_cond_wait()
2. Use builtin atomic functions to manipulate waiting count
---
 compat/os2threads.h | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)

diff --git a/compat/os2threads.h b/compat/os2threads.h
index 7c0fe13..b461f83 100644
--- a/compat/os2threads.h
+++ b/compat/os2threads.h
@@ -32,6 +32,7 @@
 #undef __STRICT_ANSI__          /* for _beginthread() */
 #include <stdlib.h>
 
+#include <sys/builtin.h>
 #include <sys/fmutex.h>
 
 #include "libavutil/mem.h"
@@ -44,7 +45,7 @@ typedef void pthread_mutexattr_t;
 
 typedef struct {
     HEV  event_sem;
-    int  wait_count;
+    volatile unsigned  wait_count;
 } pthread_cond_t;
 
 typedef void pthread_condattr_t;
@@ -139,21 +140,17 @@ static av_always_inline int pthread_cond_destroy(pthread_cond_t *cond)
 
 static av_always_inline int pthread_cond_signal(pthread_cond_t *cond)
 {
-    if (cond->wait_count > 0) {
+    if (!__atomic_cmpxchg32(&cond->wait_count, 0, 0))
         DosPostEventSem(cond->event_sem);
 
-        cond->wait_count--;
-    }
-
     return 0;
 }
 
 static av_always_inline int pthread_cond_broadcast(pthread_cond_t *cond)
 {
-    while (cond->wait_count > 0) {
-        DosPostEventSem(cond->event_sem);
-
-        cond->wait_count--;
+    while (!__atomic_cmpxchg32(&cond->wait_count, 0, 0)) {
+        if (DosPostEventSem(cond->event_sem))
+            DosSleep(1);
     }
 
     return 0;
@@ -161,12 +158,14 @@ static av_always_inline int pthread_cond_broadcast(pthread_cond_t *cond)
 
 static av_always_inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
 {
-    cond->wait_count++;
+    __atomic_increment(&cond->wait_count);
 
     pthread_mutex_unlock(mutex);
 
     DosWaitEventSem(cond->event_sem, SEM_INDEFINITE_WAIT);
 
+    __atomic_decrement(&cond->wait_count);
+
     pthread_mutex_lock(mutex);
 
     return 0;
-- 
2.7.0



More information about the ffmpeg-devel mailing list