[FFmpeg-devel] [PATCH] os2threads: Add pthread_once emulation

Dave Yeo daveryeo at telus.net
Sun Oct 11 04:32:35 CEST 2015


Based on code by Yuri Dario, http://svn.netlabs.org/repos/ports/pthread/trunk

Signed-off-by: Dave Yeo <daveryeo at telus.net>
---
 compat/os2threads.h | 45 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)

diff --git a/compat/os2threads.h b/compat/os2threads.h
index 5b6ca55..7f2c925 100644
--- a/compat/os2threads.h
+++ b/compat/os2threads.h
@@ -31,9 +31,18 @@
 
 #undef __STRICT_ANSI__          /* for _beginthread() */
 #include <stdlib.h>
+#include <sys/builtin.h>
 
 #include "libavutil/mem.h"
 
+/*
+ * Boolean values to make us independent of system includes.
+ */
+enum {
+  PTW32_FALSE = 0,
+  PTW32_TRUE = (! PTW32_FALSE)
+};
+
 typedef TID  pthread_t;
 typedef void pthread_attr_t;
 
@@ -47,6 +56,19 @@ typedef struct {
 
 typedef void pthread_condattr_t;
 
+typedef struct pthread_once_t_ pthread_once_t;
+
+#define PTHREAD_ONCE_INIT {0}
+
+struct pthread_once_t_
+{
+  unsigned                      done;        /* indicates if user function has been executed */
+  pthread_mutex_t lock;
+  int                           reserved1;
+  int                           reserved2;
+};
+
+
 struct thread_arg {
     void *(*start_routine)(void *);
     void *arg;
@@ -163,4 +185,27 @@ static av_always_inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mute
     return 0;
 }
 
+static av_always_inline int pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
+{
+	if (once_control == NULL || init_routine == NULL)
+	{
+		return EINVAL;
+	}
+
+	if (__atomic_cmpxchg32((unsigned*)&once_control->done, 0, 0)) /* MBR fence */
+	{
+		pthread_mutex_lock(&once_control->lock);
+		
+		if (!once_control->done)
+		{
+			(*init_routine)();
+			once_control->done = PTW32_TRUE;
+		}
+		
+		pthread_mutex_unlock(&once_control->lock);
+	}
+
+	return 0;
+}
+
 #endif /* AVCODEC_OS2PTHREADS_H */
-- 
2.0.0


More information about the ffmpeg-devel mailing list