Adding a new thread model to GCC
Wed Apr 13 09:18:00 GMT 2016
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
1) the CONDITION_VARIABLE can only be used with SRWLOCK and CRITICAL_SECTION and no user-defined mutexes can be supportd.
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.
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.
Also, this library has not been fully tested. If someone has interest in exploring how it works or testing it, I might be able to help.
More information about the Gcc