This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[v3] use NSDMI in C++11 mutex types


Now that G++ supports non-static data member initializers, I want to
use it for initializing the __gthread_mutex_t members of std::mutex
and friends:

__native_type  _M_mutex = __GTHREAD_MUTEX_INIT;

This is more portable than our current code, which does:

 constexpr mutex() noexcept : _M_mutex(__GTHREAD_MUTEX_INIT) { }

This is not valid if  __gthread_mutex_t is an array type and the
initializer is e.g. {x,y}

Using a NSDMI is OK, because that's exactly the syntax that
PTHREAD_MUTEX_INIT is designed for:

pthread_mutex_t m = PTHREAD_MUTEX_INIT;

(This patch doesn't include it, but std::condition_variable's use of
__GTHREAD_COND_INIT could also use a NSDMI.)

The attached patch not only does that, but also removes duplication by
creating a __mutex_base class, inherited by std::mutex and
std::timed_mutex, and a __recursive_mutex_base, inherited by
std::recursive_mutex and std::recursive_timed_mutex. The
initialization and destruction of the native mutex types is done in
the base classes.  As required, std::mutex and std::recursive_mutex
are still standard layout types with this change.

Does anyone have any comments or objections to going in this
direction?  If the new base classes aren't OK the NSDMI syntax could
still be used, just without refactoring to remove the code
duplication.

Attachment: mutex_nsdmi.txt
Description: Text document


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]