Skip to content

Commit

Permalink
ALL: Include and use a copy of mingw-std-threads
Browse files Browse the repository at this point in the history
This allows us to use threads and mutexes from C++11 on older MinGW
platforms.
  • Loading branch information
vkremianskii authored and DrMcCoy committed Mar 16, 2019
1 parent 0cfd5b7 commit 0652789
Show file tree
Hide file tree
Showing 41 changed files with 3,226 additions and 34 deletions.
9 changes: 9 additions & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,15 @@ UTF-8 CPP
external/utf8cpp/utf8.h or <https://www.boost.org/users/license.html>
for details.

mingw-std-threads
*****************
xoreos uses and includes the header-only mingw-std-threads library
(<https://github.com/meganz/mingw-std-threads/>) as an alternative to
libwinpthread for C++11 std::thread in MinGW builds. mingw-std-threads
is licensed under the terms of the 2-clause BSD license. For the full
text, see external/mingw-std-threads/LICENSE or
<https://github.com/meganz/mingw-std-threads/blob/master/LICENSE>.

GLM
***
xoreos uses the header-only C++ mathematics library for graphics software
Expand Down
24 changes: 24 additions & 0 deletions external/mingw-std-threads/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Copyright (c) 2016, Mega Limited
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

41 changes: 41 additions & 0 deletions external/mingw-std-threads/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
mingw-std-threads
=================

Implementation of standard C++11 threading classes, which are currently still missing on MinGW GCC.

Target Windows version
----------------------
This implementation should work with Windows XP (regardless of service pack), or newer.
The library automatically detects the version of Windows that is being targeted (at compile time), and selects an implementation that takes advantage of available Windows features.
In MinGW GCC, the target Windows version may optionally be selected by the command-line option `-D _WIN32_WINNT=...`.
Use `0x0600` for Windows Vista, or `0x0601` for Windows 7.
See "[Modifying `WINVER` and `_WIN32_WINNT`](https://docs.microsoft.com/en-us/cpp/porting/modifying-winver-and-win32-winnt)" for more details.

Usage
-----

This is a header-only library. To use, just include the corresponding `mingw.xxx.h file`, where `xxx` would be the name of the standard header that you would normally include.

For example, `#include "mingw.thread.h"` replaces `#include <thread>`.

Compatibility
-------------

This code has been tested to work with MinGW-w64 5.3.0, but should work with any other MinGW version that has the `std` threading classes missing, has C++11 support for lambda functions, variadic templates, and has working mutex helper classes in `<mutex>`.

Switching from the win32-pthread based implementation
-----------------------------------------------------
It seems that recent versions of MinGW-w64 include a Win32 port of pthreads, and have the `std::thread`, `std::mutex`, etc. classes implemented and working based on that compatibility
layer.
That is a somewhat heavier implementation, as it relies on an abstraction layer, so you may still want to use this implementation for efficiency purposes.
Unfortunately you can't use this library standalone and independent of the system `<mutex>` headers, as it relies on those headers for `std::unique_lock` and other non-trivial utility classes.
In that case you will need to edit the `c++-config.h` file of your MinGW setup and comment out the definition of _GLIBCXX_HAS_GTHREADS.
This will cause the system headers not to define the actual `thread`, `mutex`, etc. classes, but still define the necessary utility classes.

Why MinGW has no threading classes
----------------------------------
It seems that for cross-platform threading implementation, the GCC standard library relies on the gthreads/pthreads library.
If this library is not available, as is the case with MinGW, the classes `std::thread`, `std::mutex`, `std::condition_variable` are not defined.
However, various usable helper classes are still defined in the system headers.
Hence, this implementation does not re-define them, and instead includes those headers.

0 comments on commit 0652789

Please sign in to comment.