This is the mail archive of the
mailing list for the GCC project.
Re: Adding a new thread model to GCC
- From: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- To: lh_mouse <lh_mouse at 126 dot com>
- Cc: gcc <gcc at gcc dot gnu dot org>, mingw-w64-public <mingw-w64-public at lists dot sourceforge dot net>
- Date: Wed, 13 Apr 2016 11:30:36 +0100
- Subject: Re: Adding a new thread model to GCC
- Authentication-results: sourceware.org; auth=none
- References: <2f3897eb dot bbc0e dot 1540ee9aa0b dot Coremail dot lh_mouse at 126 dot com>
On 13 April 2016 at 10:17, lh_mouse wrote:
> Hi all,
> The 'win32' thread model of gcc has been there since long long ago, being compatible with very old Windows versions, also having a number of drawbacks:
> 0) its implementation is very inefficient, and
> 1) its mutexes and condition variables require dynamic initialization and are unusable in C++11 as std::mutex requires a `constexpr` constructor, and
> 2) allocating a number of rarely used mutexes or condition variables would eventually make the system run out of kernel objects.
> As a solution for 1) and 2), Microsoft introduced keyed events, details of which can be found here:
> In one word: the Windows SRWLOCK and CONDITION_VARIABLE have been introduced since Vista and are working well without any dynamic initialization, just like FUTEX.
> But there are still some major drawbacks:
> 0) the SRWLOCK can be used as an exclusive mutex but it does not have an equivalent function to pthread_mutex_timedwait, and
Some of our supported targets don't support pthread_mutex_timedwait
either (at least Mac OS X and HP-UX) so we have code in libstdc++ to
support mutexes but not timed mutexes. That might also work for your
> 1) the CONDITION_VARIABLE can only be used with SRWLOCK and CRITICAL_SECTION and no user-defined mutexes can be supportd.
That's OK, std::condition_variable only needs to work with std::mutex,
and then std::condition_variable_any (which works with user-defined
mutexes) only requires std::mutex and std::condition_variable. The
code to use user-defined mutexes is portable C++11, so doesn't need
any platform-specific code.
> As a solution, I come up with my own library: https://github.com/lhmouse/mcfgthread
> With 4 threads contending my mutex implementation turns to be 10 times faster than the gthr-win32 implementation on my Xeon E3-1230 v3 processor.
> I have also done some research on gthr.h and successfully created a wrapper for __gthread_* functhins, which can be found in mcfgthread/src/env/gthread.h.
Great! I haven't looked at the code, but I've been hoping someone
would do something like this to improve the std::mutex and
std::condition_variable implementations for modern Windows systems.
> I am currently looking for ways to integrate this library into gcc as a new thread model, let's say 'nt6' for example, so GCC can be configured with --enable-threads=nt6.
> But there is so little documentation about that. If someone know how to add a new thread model in GCC, I would appreciate that.
See line 1589 in gcc/configure.ac, where the allowed thread models are
defined. Your gthreads wrapper header should be added as
libgcc/config/i386/gthr-nt6.h and you might also need some changes to
libgcc/configure.ac (see how it handles the existing gthr-win32.h
You will need to use autoconf to regenerate the configure files after
editing the configure.ac files (or for now you can just edit the
configure files by hand, but eventually changes must go into the
configure.ac files). I can probably help with that, although I can't
build or test anything on Windows.
Once you've added the new thread model as a gthr-xxx.h file in libgcc
there will also be some work needed in libstdc++ to detect and
correctly use the new model (e.g. to detect that the timedwait
functions are not supported, so that timed mutexes will be disabled).
We might want to discuss that latter part on the libstdc++ list.