[vlc-devel] [PATCH 2/2] OpenBSD: add thread ID and atomic wait functions
Rémi Denis-Courmont
remi at remlab.net
Sun Feb 16 18:50:32 CET 2020
(This is completely untested.)
---
configure.ac | 1 +
src/Makefile.am | 5 +++
src/openbsd/thread.c | 75 ++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 81 insertions(+)
create mode 100644 src/openbsd/thread.c
diff --git a/configure.ac b/configure.ac
index bc1874cb97..b3667f933a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -372,6 +372,7 @@ AS_IF([test "${SYS}" = "mingw32"],[
AM_CONDITIONAL([HAVE_FREEBSD], [test "${SYS}" = "freebsd"])
AM_CONDITIONAL([HAVE_LINUX], [test "${SYS}" = "linux"])
+AM_CONDITIONAL([HAVE_OPENBSD], [test "${SYS}" = "openbsd"])
AM_CONDITIONAL([HAVE_OS2], [test "${SYS}" = "os2"])
AM_CONDITIONAL([HAVE_DARWIN], [test "${SYS}" = "darwin"])
diff --git a/src/Makefile.am b/src/Makefile.am
index 4f351d00ae..f2adf61095 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -469,6 +469,11 @@ libvlccore_la_SOURCES += \
endif
endif
+if HAVE_OPENBSD
+libvlccore_la_SOURCES += \
+ openbsd/thread.c
+endif
+
if !HAVE_WIN32
if !HAVE_OS2
if !HAVE_NACL
diff --git a/src/openbsd/thread.c b/src/openbsd/thread.c
new file mode 100644
index 0000000000..350a7a06f6
--- /dev/null
+++ b/src/openbsd/thread.c
@@ -0,0 +1,75 @@
+/*****************************************************************************
+ * openbsd/thread.c: OpenBSD specifics for threading
+ *****************************************************************************
+ * Copyright (C) 2020 Rémi Denis-Courmont
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <errno.h>
+#include <limits.h>
+#include <sys/types.h>
+#include <pthread.h>
+#include <unistd.h>
+#include <sys/time.h>
+#include <sys/futex.h>
+
+#include <vlc_common.h>
+
+unsigned long vlc_thread_id(void)
+{
+ return getthrid();
+}
+
+static int vlc_futex_wake(void *addr, int nr)
+{
+ return futex(addr, FUTEX_WAKE, nr, NULL, NULL);
+}
+
+static int vlc_futex_wait(void *addr, unsigned val, const struct timespec *to)
+{
+ int ret, type;
+
+ pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &type);
+ ret = futex(addr, FUTEX_WAIT, val, to, NULL);
+ pthread_setcanceltype(type, NULL);
+ return ret;
+}
+
+void vlc_atomic_notify_one(void *addr)
+{
+ vlc_futex_wake(addr, 1);
+}
+
+void vlc_atomic_notify_all(void *addr)
+{
+ vlc_futex_wake(addr, INT_MAX);
+}
+
+void vlc_atomic_wait(void *addr, unsigned val)
+{
+ vlc_futex_wait(addr, val, NULL);
+}
+
+bool vlc_atomic_timedwait(void *addr, unsigned val, vlc_tick_t delay)
+{
+ struct timespec ts = timespec_from_vlc_tick(delay);
+
+ return vlc_futex_wait(addr, val, &ts) != ETIMEDOUT;
+}
--
2.25.0
More information about the vlc-devel
mailing list