Monty and I had an argument recently regarding pthread_yield() and
std::this_thread::yield() usage.
I do want to make this discussion public, so that we can pick either
approach, get it documented and make sure everybody is aware of
reasoning and decision. Probably even add a bb check that wouldn't
allow unwanted function calls to pass.
Lets try to keep this discussion focused on std::this_thread::yield()
and avoid discussing other C++11 features.
Since I'm biased towards std::this_thread::yield() I'd let Monty
elaborate his concerns and explain why he prefers pthread_yield().
But generally it comes to compatibility and portability.
My position is: pthread_yield() should be removed in favour of
std::this_thread::yield(). Benefits:
1. Must be available to all current MariaDB versions. C++11 is a must
since 2018.
2. Compatibility and portability, which we don't have to care about.
3. I have no performance concerns after analysing how it is implemented
by clang and gcc.
4. Goes inline with "MDEV-35461 - Remove redundant checks for standard
library functions"
5. Standard things looks more community-friendly, e.g. why should MSVC
developers care about POSIX threads?
Currently pthread_yield() can be defined to:
1. SwitchToThread() - Win32 (Wlad, 2009)
MSVC seems to support std::this_thread::yield() since 2015:
https://learn.microsoft.com/en-us/cpp/standard-library/thread-functions?view=msvc-1402. thr_yield() - Solaris (Serg, 2009)
Oracle Developer Studio 12.6 supports std::this_thread::yield() since 2017:
https://docs.oracle.com/cd/E77782_01/html/E77789/bkabe.html#OSSCPgnyio3. pthread_yield_np() - OS X (Serg, 2009)
XCode is based on clang, which supports std::this_thread::yield() since 2013.
sched_yield() - default (Serg, 2009)
pthread_yield() - deprecated since 2021 (Serg, 2009)
Possible concerns:
1. Compatibility with recent platforms.
C++11 is widely available, thread models seem to be well supported.
InnoDB uses std::this_thread_yield() since 2021.
2. Compatibility with older (ancient?) platforms.
We're way off into C++11 since 2018.
Making recent MariaDB compile on such platforms is probably going
to be a heavy effort.
If we do really want to have these platforms supported, they must
be present in buildbot.
3. Performance impact of std::this_thread::yield().
See below. In many cases it is just a wrapper around functions
like sched_yield.
clang:
libcxx/include/__thread/this_thread.h:inline _LIBCPP_HIDE_FROM_ABI void yield() _NOEXCEPT { __libcpp_thread_yield(); }
libcxx/src/support/win32/thread_win32.cpp:void __libcpp_thread_yield() { SwitchToThread(); }
libcxx/include/__thread/support/pthread.h:inline _LIBCPP_HIDE_FROM_ABI void __libcpp_thread_yield() { sched_yield(); }
gcc additionally supports VxWorks:
yield() noexcept
{
#if defined _GLIBCXX_HAS_GTHREADS && defined _GLIBCXX_USE_SCHED_YIELD
__gthread_yield();
#endif
}
int
__gthread_yield (void)
{
return taskDelay (0);
}
__GTHREAD_INLINE int
__gthread_yield (void)
{
return __gthrw_(sched_yield) ();
}