FYI, Eric posted this today to the GCC patches list.  This may be of
great interest to many who would prefer native threads instead of the
winpthreads posix style interface.

Great work, Eric!  I look forward to trying this out!

---------- Forwarded message ---------
From: Eric Botcazou <ebotca...@adacore.com>
Date: Fri, Jun 28, 2019 at 6:51 AM
Subject: [patch] Reimplement GNU threads library on native Windows
To: <gcc-patc...@gcc.gnu.org>
Cc: <libstd...@gcc.gnu.org>


Hi,

this reimplements the GNU threads library on native Windows (except for the
Objective-C specific subset) using direct Win32 API calls, in lieu of the
implementation based on semaphores.  This base implementations requires
Windows XP/Server 2003, which is the default minimal setting of MinGW-W64.
This also adds the support required for the C++11 threads, using again direct
Win32 API calls; this additional layer requires Windows Vista/Server 2008 and
is enabled only if _GTHREADS_USE_COND is defined to 1.

This also changes libstdc++ to setting _GTHREADS_USE_COND to 1 when the switch
--enable-libstdcxx-threads is passed, which means that C++11 threads are still
disabled by default on native Windows and that you need to explicitly pass the
switch to enable them.  The 30_threads chapter of the testsuite is clean.

Tested on i686-pc-mingw32 and x86_64-pc-mingw32, OK for the mainline?


2019-06-28  Eric Botcazou  <ebotca...@adacore.com>

libgcc/
        * config.host (i[34567]86-*-mingw*): Add thread fragment after EH one
        as well as new i386/t-slibgcc-mingw fragment.
        (x86_64-*-mingw*): Likewise.
        * config/i386/gthr-win32.h: If _GTHREADS_USE_COND is 1, define both
        __GTHREAD_HAS_COND & __GTHREADS_CXX0X to 1 and _WIN32_WINNT to 0x0600.
        Error out if _GTHREAD_USE_MUTEX_TIMEDLOCK is 1.
        Include stdlib.h instead of errno.h and do not include _mingw.h.
        (CONST_CAST2): Add specific definition for C++.
        (ATTRIBUTE_UNUSED): New macro.
        (__UNUSED_PARAM): Delete.
        Define WIN32_LEAN_AND_MEAN before including windows.h.
        (__gthread_objc_data_tls): Use TLS_OUT_OF_INDEXES instead of (DWORD)-1.
        (__gthread_objc_init_thread_system): Likewise.
        (__gthread_objc_thread_get_data): Minor tweak.
        (__gthread_objc_condition_allocate): Use ATTRIBUTE_UNUSED.
        (__gthread_objc_condition_deallocate): Likewise.
        (__gthread_objc_condition_wait): Likewise.
        (__gthread_objc_condition_broadcast): Likewise.
        (__gthread_objc_condition_signal): Likewise.
        Include sys/time.h.
        (__gthr_win32_DWORD): New typedef.
        (__gthr_win32_HANDLE): Likewise.
        (__gthr_win32_CRITICAL_SECTION): Likewise.
        (__gthr_win32_CONDITION_VARIABLE): Likewise.
        (__gthread_t): Adjust.
        (__gthread_key_t): Likewise.
        (__gthread_mutex_t): Likewise.
        (__gthread_recursive_mutex_t): Likewise.
        (__gthread_cond_t): New typedef.
        (__gthread_time_t): Likewise.
        (__GTHREAD_MUTEX_INIT_DEFAULT): Delete.
        (__GTHREAD_RECURSIVE_MUTEX_INIT_DEFAULT): Likewise.
        (__GTHREAD_COND_INIT_FUNCTION): Define.
        (__GTHREAD_TIME_INIT): Likewise.
        (__gthr_i486_lock_cmp_xchg): Delete.
        (__gthr_win32_create): Declare.
        (__gthr_win32_join): Likewise.
        (__gthr_win32_self): Likewise.
        (__gthr_win32_detach): Likewise.
        (__gthr_win32_equal): Likewise.
        (__gthr_win32_yield): Likewise.
        (__gthr_win32_mutex_destroy): Likewise.
        (__gthr_win32_cond_init_function): Likewise if _GTHREADS_USE_COND is 1.
        (__gthr_win32_cond_broadcast): Likewise.
        (__gthr_win32_cond_signal): Likewise.
        (__gthr_win32_cond_wait): Likewise.
        (__gthr_win32_cond_timedwait): Likewise.
        (__gthr_win32_recursive_mutex_init_function): Delete.
        (__gthr_win32_recursive_mutex_lock): Likewise.
        (__gthr_win32_recursive_mutex_unlock): Likewise.
        (__gthr_win32_recursive_mutex_destroy): Likewise.
        (__gthread_create): New inline function.
        (__gthread_join): Likewise.
        (__gthread_self): Likewise.
        (__gthread_detach): Likewise.
        (__gthread_equal): Likewise.
        (__gthread_yield): Likewise.
        (__gthread_cond_init_function): Likewise if _GTHREADS_USE_COND is 1.
        (__gthread_cond_broadcast): Likewise.
        (__gthread_cond_signal): Likewise.
        (__gthread_cond_wait): Likewise.
        (__gthread_cond_timedwait): Likewise.
        (__GTHREAD_WIN32_INLINE): New macro.
        (__GTHREAD_WIN32_COND_INLINE): Likewise.
        (__GTHREAD_WIN32_ACTIVE_P): Likewise.
        Define WIN32_LEAN_AND_MEAN before including windows.h.
        (__gthread_once): Minor tweaks.
        (__gthread_key_create): Use ATTRIBUTE_UNUSED and TLS_OUT_OF_INDEXES.
        (__gthread_key_delete): Minor tweak.
        (__gthread_getspecific): Likewise.
        (__gthread_setspecific): Likewise.
        (__gthread_mutex_init_function): Reimplement.
        (__gthread_mutex_destroy): Likewise.
        (__gthread_mutex_lock): Likewise.
        (__gthread_mutex_trylock): Likewise.
        (__gthread_mutex_unlock): Likewise.
        (__gthr_win32_abs_to_rel_time): Declare.
        (__gthread_recursive_mutex_init_function): Reimplement.
        (__gthread_recursive_mutex_destroy): Likewise.
        (__gthread_recursive_mutex_lock): Likewise.
        (__gthread_recursive_mutex_trylock): Likewise.
        (__gthread_recursive_mutex_unlock): Likewise.
        (__gthread_cond_destroy): New inline function.
        (__gthread_cond_wait_recursive): Likewise.
        * config/i386/gthr-win32.c: Delete everything.
        Include gthr-win32.h to get the out-of-line version of inline routines.
        Add compile-time checks for the local version of the Win32 types.
        * config/i386/gthr-win32-cond.c: New file.
        * config/i386/gthr-win32-thread.c: Likewise.
        * config/i386/t-gthr-win32: Add config/i386/gthr-win32-thread.c to the
        EH part, config/i386/gthr-win32-cond.c and config/i386/gthr-win32.c to
        the static version of libgcc.
        * config/i386/t-mingw-pthread: Add config/i386/gthr-win32-thread.c to
        the EH part of libgcc.
        * config/i386/t-slibgcc-mingw: New file.
        * config/i386/libgcc-mingw.ver: Likewise.
libstdc++-v3/
        * acinclude.m4 (GLIBCXX_ENABLE_LIBSTDCXX_TIME): Set ac_has_sched_yield
        and ac_has_win32_sleep to yes for MinGW.  Change HAVE_WIN32_SLEEP into
        _GLIBCXX_USE_WIN32_SLEEP.
        (GLIBCXX_CHECK_GTHREADS): Add _WIN32_THREADS to compilation flags for
        Win32 threads and force _GTHREAD_USE_MUTEX_TIMEDLOCK to 0 for them.
        Add _GTHREADS_USE_COND to compilation flags if yes was configured and
        define it to 1 on success.
        * config.h.in: Regenerate.
        * configure: Likewise.
        * config/os/mingw32-w64/os_defines.h (_GLIBCXX_USE_GET_NPROCS_WIN32):
        Define to 1.
        * config/os/mingw32/os_defines.h
(_GLIBCXX_THREAD_ATEXIT_WIN32): Likewise.
        (_GLIBCXX_USE_GET_NPROCS_WIN32): Likewise.
        * src/c++11/thread.cc (get_nprocs): Provide Win32 implementation if
        _GLIBCXX_USE_GET_NPROCS_WIN32 is defined.  Replace HAVE_WIN32_SLEEP
        with USE_WIN32_SLEEP.
        * testsuite/19_diagnostics/headers/system_error/errc_std_c++0x.cc: Add
        missing conditional compilation.
        * testsuite/lib/libstdc++.exp (check_v3_target_sleep): Add support for
        _GLIBCXX_USE_WIN32_SLEEP.
        (check_v3_target_nprocs): Likewise for _GLIBCXX_USE_GET_NPROCS_WIN32.
gcc/testsuite/
        * lib/target-supports.exp (check_effective_target_pthread): Add
        #include <pthread.h> directive to the test.

--
Eric Botcazou

_______________________________________________
Mingw-w64-public mailing list
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to