all: Make header guard names consistent
[ffmpeg.git] / compat / os2threads.h
index b816bff..7c0fe13 100644 (file)
@@ -23,8 +23,8 @@
  * os2threads to pthreads wrapper
  */
 
-#ifndef AVCODEC_OS2PTHREADS_H
-#define AVCODEC_OS2PTHREADS_H
+#ifndef COMPAT_OS2THREADS_H
+#define COMPAT_OS2THREADS_H
 
 #define INCL_DOS
 #include <os2.h>
 #undef __STRICT_ANSI__          /* for _beginthread() */
 #include <stdlib.h>
 
+#include <sys/fmutex.h>
+
+#include "libavutil/mem.h"
+
 typedef TID  pthread_t;
 typedef void pthread_attr_t;
 
@@ -45,6 +49,13 @@ typedef struct {
 
 typedef void pthread_condattr_t;
 
+typedef struct {
+    volatile int done;
+    _fmutex mtx;
+} pthread_once_t;
+
+#define PTHREAD_ONCE_INIT {0, _FMUTEX_INITIALIZER}
+
 struct thread_arg {
     void *(*start_routine)(void *);
     void *arg;
@@ -64,6 +75,8 @@ static av_always_inline int pthread_create(pthread_t *thread, const pthread_attr
     struct thread_arg *thread_arg;
 
     thread_arg = av_mallocz(sizeof(struct thread_arg));
+    if (!thread_arg)
+        return ENOMEM;
 
     thread_arg->start_routine = start_routine;
     thread_arg->arg = arg;
@@ -159,4 +172,22 @@ static av_always_inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mute
     return 0;
 }
 
-#endif /* AVCODEC_OS2PTHREADS_H */
+static av_always_inline int pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
+{
+    if (!once_control->done)
+    {
+        _fmutex_request(&once_control->mtx, 0);
+
+        if (!once_control->done)
+        {
+            init_routine();
+
+            once_control->done = 1;
+        }
+
+        _fmutex_release(&once_control->mtx);
+    }
+
+    return 0;
+}
+#endif /* COMPAT_OS2THREADS_H */