Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add C11 threads.h implementation in addition to pthread implementation for multithreaded encoding. #822

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ AM_PROG_CC_C_O
AC_C_INLINE
AC_C_TYPEOF

AC_CHECK_HEADERS([stdint.h stdbool.h inttypes.h byteswap.h sys/auxv.h sys/param.h sys/ioctl.h sys/time.h termios.h x86intrin.h cpuid.h arm_neon.h])
AC_CHECK_HEADERS([stdint.h stdbool.h inttypes.h byteswap.h sys/auxv.h sys/param.h sys/ioctl.h sys/time.h termios.h x86intrin.h cpuid.h arm_neon.h threads.h])

if test "x$ac_cv_header_stdint_h" != xyes -o "x$ac_cv_header_stdbool_h" != xyes; then
AC_MSG_ERROR("Header stdint.h and/or stdbool.h not found")
Expand Down Expand Up @@ -379,15 +379,21 @@ HAVE_PTHREAD=no
if test "x$enable_multithreading" != "xno" ; then
AX_PTHREAD([
HAVE_PTHREAD=yes
AC_DEFINE(HAVE_PTHREAD,1,[Define if pthread is enabled])
AC_DEFINE([HAVE_PTHREAD],1,[Define if multithreading is enable and pthread is available])
LIBS="$PTHREAD_LIBS $LIBS"
CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
CXXFLAGS="$CXXFLAGS $PTHREAD_CFLAGS"
CC="$PTHREAD_CC"
CXX="$PTHREAD_CXX"
],[HAVE_PTHREAD=no])
if test "x${HAVE_PTHREAD}" = "xno"; then
AC_MSG_WARN("pthread support, needed for multithreading, is not found")
if test "x${HAVE_PTHREAD}" = "xno" -a "x$ac_cv_header_threads_h" = "xyes"; then
HAVE_C11THREADS=yes
AC_DEFINE([HAVE_C11THREADS],1,[Define if multithreading is enable and <threads.h> is available])
else
HAVE_C11THREADS=no
fi
if test "x${HAVE_PTHREAD}" = "xno" -a "x${HAVE_C11THREADS}" = "xno"; then
AC_MSG_WARN("pthread or C11 threads support, needed for multithreading, are not found")
fi
fi

Expand Down Expand Up @@ -616,7 +622,13 @@ fi
echo " Compiler is Clang : ....................... ${xiph_cv_c_compiler_clang}"
echo " Asm optimizations : ....................... ${asm_optimisation}"
echo " Ogg/FLAC support : ........................ ${have_ogg}"
echo " Multithreading : ........................ ${HAVE_PTHREAD}"
if test "x${HAVE_C11THREADS}" = "xyes" ; then
echo " Multithreading : ........................ C11 threads"
elif test "x${HAVE_PTHREAD}" = "xyes" ; then
echo " Multithreading : ........................ pthread"
else
echo " Multithreading : ........................ no"
fi
echo " Stack protector : ........................ ${enable_stack_smash_protection}"
echo " Fuzzing support (Clang only) : ............ ${have_oss_fuzzers}"
echo
1 change: 1 addition & 0 deletions include/share/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ SUBDIRS = grabbag
EXTRA_DIST = \
alloc.h \
compat.h \
compat_threads.h \
endswap.h \
getopt.h \
grabbag.h \
Expand Down
124 changes: 124 additions & 0 deletions include/share/compat_threads.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/* libFLAC - Free Lossless Audio Codec library
* Copyright (C) 2025 Jörn Heusipp
* Copyright (C) 2025 Xiph.Org Foundation
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - 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.
*
* - Neither the name of the Xiph.org Foundation nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* 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 FOUNDATION 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 FLAC__SHARE__COMPAT_THREADS_H
#define FLAC__SHARE__COMPAT_THREADS_H

#if defined(HAVE_C11THREADS) || defined(HAVE_PTHREAD)
#define FLAC__USE_THREADS
#endif

#ifdef FLAC__USE_THREADS
#if defined(HAVE_C11THREADS)
#include <threads.h>
#elif defined(HAVE_PTHREAD)
#include <errno.h>
#include <stddef.h>
#include <pthread.h>
#endif
#endif


#ifdef FLAC__USE_THREADS

#if defined(HAVE_C11THREADS)

#define FLAC__thrd_success thrd_success
#define FLAC__thrd_busy thrd_busy
#define FLAC__thrd_nomem thrd_nomem

#define FLAC__thrd_t thrd_t
#define FLAC__thrd_create(thread, func, arg) thrd_create(thread, func, arg)
#define FLAC__thrd_join(thread, result) thrd_join(thread, result)

#define FLAC__mtx_plain mtx_plain
#define FLAC__mtx_t mtx_t
#define FLAC__mtx_init(mutex, type) mtx_init(mutex, type)
#define FLAC__mtx_trylock(mutex) mtx_trylock(mutex)
#define FLAC__mtx_lock(mutex) mtx_lock(mutex)
#define FLAC__mtx_unlock(mutex) mtx_unlock(mutex)
#define FLAC__mtx_destroy(mutex) mtx_destroy(mutex)

#define FLAC__cnd_t cnd_t
#define FLAC__cnd_init(cv) cnd_init(cv)
#define FLAC__cnd_broadcast(cv) cnd_broadcast(cv)
#define FLAC__cnd_signal(cv) cnd_signal(cv)
#define FLAC__cnd_wait(cv, mutex) cnd_wait(cv, mutex)
#define FLAC__cnd_destroy(cv) cnd_destroy(cv)

#define FLAC__thread_return_type int
#define FLAC__thread_default_return_value 0

#elif defined(HAVE_PTHREAD)

/* This is not meant to be a full implementation of C11 threads on top of
* pthreads.
* Just enough is implemented to provide the interfaces that libFLAC uses
* currently.
* A full implementation would certainly be possible, but there already are
* various other open source projects dedicated to providing pthread <-> C11
* threads wrappers both ways. FLAC wants to avoid the additional dependency
* here, and provides this reduced wrapper itself.
*/

#define FLAC__thrd_success 0
#define FLAC__thrd_busy EBUSY
#define FLAC__thrd_nomem ENOMEM

#define FLAC__thrd_t pthread_t
#define FLAC__thrd_create(thread, func, arg) pthread_create(thread, NULL, func, arg)
#define FLAC__thrd_join(thread, result) pthread_join(thread, result)

#define FLAC__mtx_plain NULL
#define FLAC__mtx_t pthread_mutex_t
#define FLAC__mtx_init(mutex, type) ((pthread_mutex_init(mutex, type) == 0) ? FLAC__thrd_success : FLAC__thrd_nomem)
#define FLAC__mtx_trylock(mutex) ((pthread_mutex_trylock(mutex) == 0) ? FLAC__thrd_success : FLAC__thrd_busy)
#define FLAC__mtx_lock(mutex) pthread_mutex_lock(mutex)
#define FLAC__mtx_unlock(mutex) pthread_mutex_unlock(mutex)
#define FLAC__mtx_destroy(mutex) pthread_mutex_destroy(mutex)

#define FLAC__cnd_t pthread_cond_t
#define FLAC__cnd_init(cv) ((pthread_cond_init(cv, NULL) == 0) ? FLAC__thrd_success : FLAC__thrd_nomem)
#define FLAC__cnd_broadcast(cv) pthread_cond_broadcast(cv)
#define FLAC__cnd_signal(cv) pthread_cond_signal(cv)
#define FLAC__cnd_wait(cv, mutex) pthread_cond_wait(cv, mutex)
#define FLAC__cnd_destroy(cv) pthread_cond_destroy(cv)

#define FLAC__thread_return_type void *
#define FLAC__thread_default_return_value NULL

#endif

#endif


#endif /* FLAC__SHARE__COMPAT_THREADS_H */
Loading