Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/videolan/dav1d.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHugo Beauzée-Luyssen <hugo@videolan.org>2018-09-20 14:21:08 +0300
committerRonald S. Bultje <rsbultje@gmail.com>2018-09-22 14:59:24 +0300
commit707a32a57df226e3364dcc4373fc32ae7a69af91 (patch)
tree7f2705182960064fe70088ff1c39fe9d356e2ff1
parente2892ffa2dd1e893d0229c5fcbe0bbbee8e11c20 (diff)
Add & use a thread compatibility layer
-rw-r--r--meson.build9
-rw-r--r--src/cdf.c2
-rw-r--r--src/internal.h2
-rw-r--r--src/picture.c1
-rw-r--r--src/picture.h2
-rw-r--r--src/thread.h63
-rw-r--r--src/thread_data.h2
-rw-r--r--src/thread_task.c2
-rw-r--r--src/win32/thread.c128
9 files changed, 205 insertions, 6 deletions
diff --git a/meson.build b/meson.build
index 66e2f7d..863a920 100644
--- a/meson.build
+++ b/meson.build
@@ -34,7 +34,10 @@ cc = meson.get_compiler('c')
if not meson.is_cross_build()
thread_dependency = dependency('threads')
else
- thread_dependency = cc.find_library('pthread')
+ thread_dependency = cc.find_library('pthread', required: false)
+endif
+if thread_dependency.found()
+ cdata.set('HAVE_PTHREAD_H', 1)
endif
dav1d_inc_dirs = include_directories(['include', 'include/dav1d'])
@@ -193,6 +196,10 @@ libdav1d_sources = files(
'src/qm.c',
)
+if host_machine.system() == 'windows'
+ libdav1d_sources += files('src/win32/thread.c')
+endif
+
libdav1d = library('dav1d',
libdav1d_sources, rev_target,
version: '0.0.1',
diff --git a/src/cdf.c b/src/cdf.c
index 71bc237..b0fdcaa 100644
--- a/src/cdf.c
+++ b/src/cdf.c
@@ -28,9 +28,9 @@
#include "config.h"
#include <assert.h>
-#include <pthread.h>
#include <string.h>
+#include "src/thread.h"
#include "common/intops.h"
#include "src/cdf.h"
diff --git a/src/internal.h b/src/internal.h
index ffb2b64..b538ead 100644
--- a/src/internal.h
+++ b/src/internal.h
@@ -28,7 +28,6 @@
#ifndef __DAV1D_SRC_INTERNAL_H__
#define __DAV1D_SRC_INTERNAL_H__
-#include <pthread.h>
#include <stdatomic.h>
#include "dav1d/data.h"
@@ -54,6 +53,7 @@ typedef struct Dav1dTileContext Dav1dTileContext;
#include "src/picture.h"
#include "src/recon.h"
#include "src/ref_mvs.h"
+#include "src/thread.h"
typedef struct Dav1dDSPContext {
Dav1dIntraPredDSPContext ipred;
diff --git a/src/picture.c b/src/picture.c
index dccfd40..cb3daba 100644
--- a/src/picture.c
+++ b/src/picture.c
@@ -39,6 +39,7 @@
#include "src/picture.h"
#include "src/ref.h"
+#include "src/thread.h"
static int picture_alloc_with_edges(Dav1dPicture *const p,
const int w, const int h,
diff --git a/src/picture.h b/src/picture.h
index 051d4cf..d071cfd 100644
--- a/src/picture.h
+++ b/src/picture.h
@@ -28,9 +28,9 @@
#ifndef __DAV1D_SRC_PICTURE_H__
#define __DAV1D_SRC_PICTURE_H__
-#include <pthread.h>
#include <stdatomic.h>
+#include "src/thread.h"
#include "dav1d/picture.h"
#include "src/thread_data.h"
diff --git a/src/thread.h b/src/thread.h
new file mode 100644
index 0000000..40ffb74
--- /dev/null
+++ b/src/thread.h
@@ -0,0 +1,63 @@
+/*
+ * Copyright © 2018, VideoLAN and dav1d authors
+ * Copyright © 2018, Two Orioles, LLC
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __DAV1D_THREAD_H__
+# define __DAV1D_THREAD_H__
+
+#if defined(_WIN32) && !defined(HAVE_PTHREAD_H)
+
+#include <windows.h>
+
+typedef CRITICAL_SECTION pthread_mutex_t;
+typedef CONDITION_VARIABLE pthread_cond_t;
+typedef void *pthread_t;
+typedef void *pthread_mutexattr_t;
+typedef void *pthread_condattr_t;
+typedef void *pthread_attr_t;
+
+void pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t* attr);
+void pthread_mutex_destroy(pthread_mutex_t* mutex);
+void pthread_mutex_lock(pthread_mutex_t* mutex);
+void pthread_mutex_unlock(pthread_mutex_t* mutex);
+
+void pthread_cond_init(pthread_cond_t* cond, const pthread_condattr_t* attr);
+void pthread_cond_destroy(pthread_cond_t* cond);
+void pthread_cond_wait(pthread_cond_t* cond, pthread_mutex_t* mutex);
+void pthread_cond_signal(pthread_cond_t* cond);
+void pthread_cond_broadcast(pthread_cond_t* cond);
+
+int pthread_create(pthread_t* thread, const pthread_attr_t* attr,
+ void*(*proc)(void*), void* param);
+void pthread_join(pthread_t thread, void** res);
+
+#else
+
+#include <pthread.h>
+
+#endif
+
+#endif // __DAV1D_THREAD_H__
diff --git a/src/thread_data.h b/src/thread_data.h
index 8736189..7dae029 100644
--- a/src/thread_data.h
+++ b/src/thread_data.h
@@ -28,6 +28,8 @@
#ifndef __DAV1D_SRC_THREAD_DATA_H__
#define __DAV1D_SRC_THREAD_DATA_H__
+#include "src/thread.h"
+
struct thread_data {
pthread_t thread;
pthread_cond_t cond;
diff --git a/src/thread_task.c b/src/thread_task.c
index 3ef739f..dc276c4 100644
--- a/src/thread_task.c
+++ b/src/thread_task.c
@@ -53,7 +53,6 @@ void *dav1d_frame_task(void *const data) {
decode_frame(f);
}
- pthread_exit(NULL);
return NULL;
}
@@ -132,6 +131,5 @@ void *dav1d_tile_task(void *const data) {
}
}
- pthread_exit(NULL);
return NULL;
}
diff --git a/src/win32/thread.c b/src/win32/thread.c
new file mode 100644
index 0000000..c73dba1
--- /dev/null
+++ b/src/win32/thread.c
@@ -0,0 +1,128 @@
+/*
+ * Copyright © 2018, VideoLAN and dav1d authors
+ * Copyright © 2018, Two Orioles, LLC
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+
+#if defined(_WIN32) && !defined(HAVE_PTHREAD_H)
+
+#include <windows.h>
+#include <process.h>
+#include <errno.h>
+
+#include "config.h"
+#include "src/thread.h"
+
+void pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t* attr)
+{
+ (void)attr;
+ InitializeCriticalSection(mutex);
+}
+
+void pthread_mutex_destroy(pthread_mutex_t* mutex)
+{
+ DeleteCriticalSection(mutex);
+}
+
+void pthread_mutex_lock(pthread_mutex_t* mutex)
+{
+ EnterCriticalSection(mutex);
+}
+
+void pthread_mutex_unlock(pthread_mutex_t* mutex)
+{
+ LeaveCriticalSection(mutex);
+}
+
+void pthread_cond_init(pthread_cond_t* cond, const pthread_condattr_t* attr)
+{
+ (void)attr;
+ InitializeConditionVariable(cond);
+}
+
+void pthread_cond_destroy(pthread_cond_t* cond)
+{
+ (void)cond;
+}
+
+void pthread_cond_wait(pthread_cond_t* cond, pthread_mutex_t* mutex)
+{
+ SleepConditionVariableCS(cond, mutex, INFINITE);
+}
+
+void pthread_cond_signal(pthread_cond_t* cond)
+{
+ WakeConditionVariable(cond);
+}
+
+void pthread_cond_broadcast(pthread_cond_t* cond)
+{
+ WakeAllConditionVariable(cond);
+}
+
+typedef struct dav1d_win32_thread_t {
+ HANDLE h;
+ void* param;
+ void*(*proc)(void*);
+ void* res;
+} dav1d_win32_thread_t;
+
+static unsigned __stdcall dav1d_thread_entrypoint(void* data)
+{
+ dav1d_win32_thread_t* t = data;
+ t->res = t->proc( t->param );
+ return 0;
+}
+
+int pthread_create(pthread_t* thread, const pthread_attr_t* attr, void*(*proc)(void*), void* param)
+{
+ dav1d_win32_thread_t* th = *thread = malloc(sizeof(*th));
+ if (th == NULL)
+ return ENOMEM;
+ th->proc = proc;
+ th->param = param;
+ uintptr_t h = _beginthreadex(NULL, 0, dav1d_thread_entrypoint, th, 0, NULL);
+ if ( h == 0 ) {
+ int err = errno;
+ free(th);
+ *thread = NULL;
+ return err;
+ }
+ th->h = (HANDLE)h;
+ return 0;
+}
+
+void pthread_join(pthread_t thread, void** res)
+{
+ dav1d_win32_thread_t* th = thread;
+ WaitForSingleObject(th->h, INFINITE);
+
+ if (res != NULL)
+ *res = th->res;
+ free(th);
+}
+
+#endif