From 6bf07c64aa262cfb8a11a29ea70794546853c36c Mon Sep 17 00:00:00 2001 From: Brad Smith Date: Fri, 21 Feb 2025 05:08:32 -0500 Subject: [PATCH] openbsd: add initial support - thread id - thread naming - CPU feature detection --- configure.ac | 1 + src/Makefile.am | 6 +++ src/openbsd/cpu.c | 95 ++++++++++++++++++++++++++++++++++++++++++++ src/openbsd/thread.c | 46 +++++++++++++++++++++ 4 files changed, 148 insertions(+) create mode 100644 src/openbsd/cpu.c create mode 100644 src/openbsd/thread.c diff --git a/configure.ac b/configure.ac index 5369b5c299..a94bab3105 100644 --- a/configure.ac +++ b/configure.ac @@ -494,6 +494,7 @@ AS_IF([test "${SYS}" = "mingw32"],[ ]) AM_CONDITIONAL([HAVE_FREEBSD], [test "${SYS}" = "freebsd"]) +AM_CONDITIONAL([HAVE_OPENBSD], [test "${SYS}" = "openbsd"]) AM_CONDITIONAL([HAVE_LINUX], [test "${SYS}" = "linux"]) AM_CONDITIONAL([HAVE_ANDROID], [test "${VENDOR}" = "android"]) AM_CONDITIONAL([HAVE_OS2], [test "${SYS}" = "os2"]) diff --git a/src/Makefile.am b/src/Makefile.am index c05fa9c194..94f0d6e96b 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -492,6 +492,12 @@ libvlccore_la_SOURCES += \ freebsd/thread.c endif +if HAVE_OPENBSD +libvlccore_la_SOURCES += \ + openbsd/cpu.c \ + openbsd/thread.c +endif + if HAVE_LINUX libvlccore_la_SOURCES += \ linux/cpu.c \ diff --git a/src/openbsd/cpu.c b/src/openbsd/cpu.c new file mode 100644 index 0000000000..d45ff42e58 --- /dev/null +++ b/src/openbsd/cpu.c @@ -0,0 +1,95 @@ +/***************************************************************************** + * openbsd/cpu.c: CPU detection code for OpenBSD + ***************************************************************************** + * Copyright (C) 2012 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 + +#include +#include + +#if defined (__aarch64__) +unsigned vlc_CPU_raw(void) +{ + unsigned int flags = 0; + unsigned long hwcap = 0; + //unsigned long hwcap2 = 0; // TODO: SVE2 + + elf_aux_info(AT_HWCAP, &hwcap, sizeof(hwcap)); + // elf_aux_info(AT_HWCAP, &hwcap, sizeof(hwcap2)); // TODO: SVE2 + + /* HWCAP_FP (HAVE_FPU) is statically assumed. */ + if (hwcap & HWCAP_ASIMD) + flags |= VLC_CPU_ARM_NEON; + if (hwcap & HWCAP_SVE) + flags |= VLC_CPU_ARM_SVE; + + return flags; +} + +#elif defined (__arm__) +unsigned vlc_CPU_raw(void) +{ + unsigned int flags = 0; + unsigned long hwcap = 0; + + elf_aux_info(AT_HWCAP, &hwcap, sizeof(hwcap)); + + /* TLS implies ARMv6, Thumb-EE and VFP imply ARMv7 */ + if (hwcap & (HWCAP_TLS|HWCAP_THUMBEE|HWCAP_VFP)) + flags |= VLC_CPU_ARMv6; /* SIMD */ + if (hwcap & HWCAP_NEON) + flags |= VLC_CPU_ARM_NEON; /* Advanced SIMD */ + + return flags; +} + +#elif defined (__powerpc__) /* both 32- and 64-bit */ +unsigned vlc_CPU_raw(void) +{ + unsigned int flags = 0; + unsigned long hwcap = 0; + + elf_aux_info(AT_HWCAP, &hwcap, sizeof(hwcap)); + + if (hwcap & PPC_FEATURE_HAS_ALTIVEC) + flags |= VLC_CPU_ALTIVEC; + + return flags; +} + +#elif defined (__riscv) +# define HWCAP_RV(letter) (1LU << ((letter) - 'A')) + +unsigned vlc_CPU_raw(void) +{ + unsigned int flags = 0; + unsigned long hwcap = 0; + + elf_aux_info(AT_HWCAP, &hwcap, sizeof(hwcap)); + + if (hwcap & HWCAP_RV('V')) + flags |= VLC_CPU_RV_V; + + return flags; +} +#endif diff --git a/src/openbsd/thread.c b/src/openbsd/thread.c new file mode 100644 index 0000000000..d0b8a74173 --- /dev/null +++ b/src/openbsd/thread.c @@ -0,0 +1,46 @@ +/***************************************************************************** + * 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 +#include +#include + +#include +#include +#include + +unsigned long vlc_thread_id(void) +{ + static _Thread_local pid_t tid = -1; + + if (unlikely(tid == -1)) + tid = getthrid(); + + return tid; +} + +void (vlc_thread_set_name)(const char *name) +{ + pthread_set_name_np(pthread_self(), name); +}