From 6cea271f436d98c39c00e680c71fb1b14733648c Mon Sep 17 00:00:00 2001 From: Bart Van Assche Date: Sun, 15 Oct 2023 07:36:33 -0700 Subject: [PATCH] drd: Add support for C11 thrd_create() --- NEWS | 1 + configure.ac | 20 ++++++++++++++++++++ drd/drd_pthread_intercepts.c | 3 ++- drd/tests/Makefile.am | 8 ++++++++ drd/tests/thrd_create.cpp | 11 +++++++++++ drd/tests/thrd_create.stderr.exp | 4 ++++ drd/tests/thrd_create.vgtest | 2 ++ 7 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 drd/tests/thrd_create.cpp create mode 100644 drd/tests/thrd_create.stderr.exp create mode 100644 drd/tests/thrd_create.vgtest diff --git a/NEWS b/NEWS index ad30282704..b2a385d8df 100644 --- a/NEWS +++ b/NEWS @@ -86,6 +86,7 @@ are not entered into bugzilla tend to get forgotten about or ignored. 473870 FreeBSD 14 applications fail early at startup 473944 Handle mold linker split RW PT_LOAD segments correctly 474332 aligned_alloc under Valgrind returns nullptr when alignment is not a multiple of sizeof(void *) +475650 DRD does not work with C11 threads n-i-bz Allow arguments with spaces in .valgrindrc files n-i-bz FreeBSD fixed reading of Valgrind tools own debuginfo diff --git a/configure.ac b/configure.ac index d1b82c05d2..95dbc70201 100755 --- a/configure.ac +++ b/configure.ac @@ -2080,6 +2080,26 @@ AC_LANG(C) AM_CONDITIONAL(CXX_CAN_INCLUDE_THREAD_HEADER, test x$ac_cxx_can_include_thread_header = xyes) +AC_MSG_CHECKING([that the C++ compiler supports thrd_create()]) +AC_LANG(C++) +safe_CXXFLAGS=$CXXFLAGS +CXXFLAGS=-std=c++11 + +AC_COMPILE_IFELSE([AC_LANG_PROGRAM([ +#include +], [[thrd_t thr; return thrd_create(&thr, [](void *arg){return 0;}, NULL);]])], +[ +ac_cxx_have_thrd_create=yes +AC_MSG_RESULT([yes]) +], [ +ac_cxx_have_thrd_create=no +AC_MSG_RESULT([no]) +]) +CXXFLAGS=$safe_CXXFLAGS +AC_LANG(C) + +AM_CONDITIONAL(CXX_HAVE_THRD_CREATE, test x$ac_cxx_have_thrd_create = xyes) + # Check whether compiler can process #include without errors AC_MSG_CHECKING([that C++ compiler can include header file]) diff --git a/drd/drd_pthread_intercepts.c b/drd/drd_pthread_intercepts.c index 1e6313e8ec..b1b4762d63 100644 --- a/drd/drd_pthread_intercepts.c +++ b/drd/drd_pthread_intercepts.c @@ -604,7 +604,8 @@ int pthread_create_intercept(pthread_t* thread, const pthread_attr_t* attr, * this means that the new thread will be started as a joinable thread. */ thread_args.detachstate = PTHREAD_CREATE_JOINABLE; - if (attr) + /* The C11 thrd_create() implementation passes -1 as 'attr' argument. */ + if (attr && (uintptr_t)attr + 1 != 0) { if (pthread_attr_getdetachstate(attr, &thread_args.detachstate) != 0) assert(0); diff --git a/drd/tests/Makefile.am b/drd/tests/Makefile.am index 5482cf7be4..1b0beb4dae 100755 --- a/drd/tests/Makefile.am +++ b/drd/tests/Makefile.am @@ -365,6 +365,8 @@ EXTRA_DIST = \ tc23_bogus_condwait.vgtest \ tc24_nonzero_sem.stderr.exp \ tc24_nonzero_sem.vgtest \ + thrd_create.stderr.exp \ + thrd_create.vgtest \ thread_name.stderr.exp \ thread_name.vgtest \ thread_name_xml.stderr.exp \ @@ -513,6 +515,10 @@ if HAVE_SWAPCONTEXT check_PROGRAMS += swapcontext endif +if CXX_HAVE_THRD_CREATE +check_PROGRAMS += thrd_create +endif + if !VGCONF_OS_IS_DARWIN check_PROGRAMS += sem_wait endif @@ -653,3 +659,5 @@ endif timed_mutex_SOURCES = timed_mutex.cpp timed_mutex_CXXFLAGS = $(AM_CXXFLAGS) -std=c++0x + +thrd_create_SOURCES = thrd_create.cpp diff --git a/drd/tests/thrd_create.cpp b/drd/tests/thrd_create.cpp new file mode 100644 index 0000000000..b0f3eeea86 --- /dev/null +++ b/drd/tests/thrd_create.cpp @@ -0,0 +1,11 @@ +#include +#include + +int main() +{ + thrd_t thr; + thrd_create(&thr, [](void *arg){std::cerr << "Hello, world!\n"; return 0;}, + NULL); + thrd_join(thr, NULL); + return 0; +} diff --git a/drd/tests/thrd_create.stderr.exp b/drd/tests/thrd_create.stderr.exp new file mode 100644 index 0000000000..f5fb4374a1 --- /dev/null +++ b/drd/tests/thrd_create.stderr.exp @@ -0,0 +1,4 @@ + +Hello, world! + +ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) diff --git a/drd/tests/thrd_create.vgtest b/drd/tests/thrd_create.vgtest new file mode 100644 index 0000000000..12b373e461 --- /dev/null +++ b/drd/tests/thrd_create.vgtest @@ -0,0 +1,2 @@ +prereq: test -e thrd_create && ./supported_libpthread +prog: thrd_create -- 2.43.5