Commit Graph

46 Commits (master)

Author SHA1 Message Date
sfan5 663c936428 Fix synchronization issue at thread start
If a newly started thread immediately exits then m_running would
immediately be set to false again and the caller would be stuck
waiting for m_running to become true forever.
Since a mutex for synchronizing startup already exists we can
simply move the while loop into it.

see also: #5134 which introduced m_start_finished_mutex
2022-05-02 20:54:55 +02:00
Sebastien Marie 3fb1f45301
Remove Thread::kill() and related unittest (#10317)
Closes: #6065
2020-09-10 12:19:18 +02:00
David CARLIER 0683bea283
Add NetBSD cpu affinity support code (#10378) 2020-09-09 19:12:58 +02:00
David CARLIER b262184acf
NetBSD build fix proposal. (#10308)
Fixing thread naming call and let the class setting RANDOM_MIN/RANDOM_MAX.
2020-08-25 20:50:41 +02:00
David CARLIER cf5547227d
Complete Haiku platform support. (#10311)
Fixing linkage/libraries missing issue.
Implements missing platform specifics.
2020-08-23 15:39:32 +02:00
sfan5 49365b25d9 Optimize semaphore wait with zero timeout on POSIX 2019-11-14 20:20:14 +01:00
Leonid Bobrov 339341ba4e DragonFly BSD is somewhat identical to FreeBSD (#8159) 2019-02-03 09:53:54 +01:00
Loic Blot 5070ca2111
Fix 5 issues reported by PVS studio
* src/sky.cpp  146     warn    V519 The 'suncolor_f.r' variable is assigned values twice successively. Perhaps this is a mistake. Check lines: 142, 146.
* src/sky.cpp  147     warn    V519 The 'suncolor_f.g' variable is assigned values twice successively. Perhaps this is a mistake. Check lines: 143, 147.
* src/sky.cpp  148     warn    V519 The 'suncolor_f.b' variable is assigned values twice successively. Perhaps this is a mistake. Check lines: 144, 148.
* src/threading/thread.cpp     63      err     V730 Not all members of a class are initialized inside the constructor. Consider inspecting: m_thread_obj.
* src/server.cpp       3243    err     V595 The 'log' pointer was utilized before it was verified against nullptr. Check lines: 3243, 3258.
2018-04-04 08:40:31 +02:00
Loïc Blot 32ae492657 Thread: fix a crash on Windows due to data race condition on Thread::m_start_finished_mutex (#6515) 2017-10-10 12:27:08 +02:00
Loïc Blot 88b436e6a9 Code modernization: subfolders (#6283)
* Code modernization: subfolders

Modernize various code on subfolders client, network, script, threading, unittests, util

* empty function
* default constructor/destructor
* for range-based loops
* use emplace_back instead of push_back
* C++ STL header style
* Make connection.cpp readable in a pointed place + typo
2017-08-19 22:23:47 +02:00
Loïc Blot 921151d97a C++ modernize: Pragma once (#6264)
* Migrate cpp headers to pragma once
2017-08-17 22:19:39 +02:00
adrido d7343b6c93 Fix msvc annoyances (#5963)
* MSVC: Fix '/std:c++11' is not a valid compiler option

* MSVC/MINGW: Define 'WIN32_LEAN_AND_MEAN' for the whole project

In some obscure cases 'Windows.h" got includet before that definition, which leaded to compilation warnings+errors

* MSVC: '/arch:SSE' is only available for x86

* MSVC: Fix float conversation

* MSVC/MINGW: use winthreads on Windows

* MSVC: 'USE_CMAKE_CONFIG' might be already definied by CMake build system

* MSVC: Use all available cpu cores for compiling

* Add missing include ctime and use std::time_t
2017-06-27 11:54:40 +02:00
Vincent Glize 4a5e8ad343 C++11 cleanup on constructors (#6000)
* C++11 cleanup on constructors dir script
2017-06-19 23:54:58 +02:00
ShadowNinja 6c5e5e2023 Remove threads.h and replace its definitions with their C++11 equivalents (#5957)
This also changes threadProc's signature, since C++11 supports arbitrary
thread function signatures.
2017-06-11 09:43:05 +02:00
Loïc Blot 5bd33a1586 C++11 patchset 5: use std::threads and remove old compat layer (#5928)
* C++11 patchset 5: use std::threads and remove old compat layer

* use pragma once in modified headers
* use C++11 function delete for object copy
2017-06-08 10:40:11 +02:00
Loïc Blot d4c0f91275 Use C++11 mutexes only (remove compat code) (#5922)
* Fix event LINT & remove default constructor/destructors
* remove compat code & modernize autolock header
2017-06-06 16:29:28 +02:00
Loïc Blot b3dfe5332c C++11 patchset 3: remove Atomic/GenericAtomic and use std::atomic (#5906) 2017-06-06 14:34:14 +02:00
Zeno- 1bf9b25fb4 Fix uninitalised variable in event.cpp (#5764) 2017-05-15 20:10:29 +10:00
Loic Blot 4af99b75cf
Pass clang-format on 14 trivial header files fixes
Also remove them from whitelist
2017-04-07 23:22:00 +02:00
ShadowNinja 3eecc6ff44 Fix AIX threading build 2017-01-28 18:52:07 -05:00
ShadowNinja c93f7f5cea Fix synchronization issue at thread start
If a newly spawned thread called getThreadId or getThreadHandle before
the spawning thread finished saving the thread handle, then the
handle/id would be used uninitialized.  This would cause the threading
tests to fail since isCurrentThread would return false, and if Minetest
is built with C++11 support the std::thread object pointer would be
dereferenced while ininitialized, causing a segmentation fault.

This fixes the issue by using a mutex to force the spawned thread to
wait for the spawning thread to finish initializing the thread object.

An alternative way to handle this would be to also set the thread
handle/id in the started thread but this wouldn't work for C++11
builds because there's no way to get the partially constructed object.
2017-01-28 18:52:07 -05:00
sfan5 0a16e53b40 Fix C++11 Windows build of threading code
The initial problem was that mutex_auto_lock.h tries to use std::unique_lock<std::mutex>
	despite mutex.h not using C++11's std::mutex on Windows. The problem here is the mismatch
	between C++11 usage conditions of the two headers. This commit moves the decision logic
	to threads.h and makes sure mutex.h, mutex_auto_lock.h and event.h all use the same features.
2016-10-06 22:37:30 +02:00
Rogier-5 1dfd977ec4 Fix & make linux conditionals uniform (#4278)
The source used a hodge-podge of different combinations of different
macros to check for linux: 'linux', '__linux', '__linux__'.

As '__linux__' is standard (Posix), and the others are not, the source
now uniformly uses __linux__. If either linux or __linux are defined,
it is made sure that __linux__ is defined as well.
2016-07-04 21:00:57 +02:00
Craig Robbins c92cff5d52 Fix Windows build
Fixes the issue introduced by c1a0ebb (Fix use of uninitialised variable
in class Event) causing Windows builds to fail
2016-05-02 15:01:17 +10:00
Craig Robbins c1a0ebb725 Fix use of uninitialised variable in class Event 2016-05-01 17:32:00 +10:00
ShadowNinja a53357d291 Fix POSIX C++11 build
I broke this in 46fd114e9a.
2016-04-30 13:41:04 -04:00
Craig Robbins ae75073944 Fix prepreprocessor error in thread.h (related to C++11 threads) 2016-04-30 12:29:52 +10:00
ShadowNinja 46fd114e9a Fix race on thread creation
This often broke the threading tests on OSX.
2016-04-28 13:21:46 -04:00
est31 e50c784e2c Fix C++11 compilability
Previous commits broke it... :(
2016-01-23 05:45:29 +01:00
BlockMen aed10765f2 Fix events on Windows 2015-12-11 23:08:14 +01:00
ShadowNinja 696148e298 Fix Event implementation
On non-windows platforms this just used a semaphore,
which meant that multiple calls to signal() would
result in wait() returning multiple times.
2015-12-07 13:53:56 -05:00
kwolekr 6f95bb1ef3 Fix misc. MinGW and Valgrind warnings 2015-11-08 16:34:56 -05:00
est31 8f03995604 Time: use locks again
The Atomic implementation was only partially correct, and was very complex.
Use locks for sake of simplicity, following KISS principle.
Only remaining atomic operation use is time of day speed, because that
really is only read + written.

Also fixes a bug with m_time_conversion_skew only being decremented, never
incremented (Regresion from previous commit).

atomic.h changes:
	* Add GenericAtomic<T> class for non-integral types like floats.

	* Remove some last remainders from atomic.h of the volatile use.
2015-11-04 03:44:09 +01:00
est31 abc354a5d0 Atomic: cleanup and add more operations
Cleanup:
	* Remove volatile keyword, it is of no use at all. [1]
	* Remove the enable_if stuff. It had no use either.
	  The most likely explanation why the enable_if stuff was there is that it
	  was used as something like a STATIC_ASSERT to verify that sizeof(T) is not larger
	  than sizeof(void *). This check however is not just misplaced in a place where we
	  already use a lock, it isn't needed at all, as gcc will just generate a call to
	  to the runtime if it compiles for platforms that don't support atomic instructions.
	  The runtime will then most likely use locks.

Code style fixes:
	* Prefix name of the mutex
	* Line everything up nicely, where it makes things look nice
	* Filling \ continuations with spaces is code style rule

Added operations on the atomic var:
	* Compare and swap
	* Swap

The second point of the cleanup also fixes the Android build of the next commit.

[1]: http://stackoverflow.com/q/2484980
2015-11-03 19:07:39 +01:00
est31 eabfe091b5 Rename and move basicmacros.h to util/basic_macros.h 2015-11-02 04:57:55 +01:00
kwolekr 52e5b513ed Fix Lua scripting synchronization
For several years now, the lua script lock has been completely broken.
This commit fixes the main issue (creation of a temporary rather than
scoped object), and fixes a subsequent deadlock issue caused by
nested script API calls by adding support for recursive mutexes.
2015-11-01 11:32:05 -05:00
kwolekr fdede60034 Fix C++11 compatibility 2015-10-31 02:38:23 -04:00
kwolekr c56d7fe0eb Add DISABLE_CLASS_COPY macro (and use it)
Use this macro to disallow copying of an object using the assignment
operator or copy constructor.  This catches otherwise silent-but-deadly
mistakes such as "ServerMap map = env->getMap();" at compile time.

If so desired, it is still possible to copy a class, but it now requires
an explicit call to memcpy or std::copy.
2015-10-27 22:05:08 -04:00
kwolekr 964be640cb Fix some threading things and add additional thread unittests
- Fix thread name reset on start()
- Fully reset thread state on kill()
- Add unittests to check for correct object states under various circumstances
2015-10-24 02:31:23 -04:00
Rui 06e5d0f5c5 Fix == to = 2015-10-17 17:20:58 +01:00
kwolekr 6ba9d6545d Fix missing include on AIX 2015-10-17 01:16:17 -04:00
kwolekr 765a834cd0 Refactor Thread class to improve readability and portability
- Fix some incompatibilities with obscure platforms (AIX and WinCE)
- Clean up Thread class interface
- Add m_ prefix to private member variables
- Simplify platform-dependent logic, reducing preprocessor
  conditional clauses and improving readibility
- Add Thread class documentation
2015-10-16 23:43:29 -04:00
ShadowNinja 2139d7d45f Refactor logging
- Add warning log level
- Change debug_log_level setting to enumeration string
- Map Irrlicht log events to MT log events
- Encapsulate log_* functions and global variables into a class, Logger
- Unify dstream with standard logging mechanism
- Unify core.debug() with standard core.log() script API
2015-10-14 01:03:54 -04:00
ShadowNinja 20405edb06 Fix MinGW 32-bit build 2015-10-03 13:20:43 -04:00
Pavel Puchkin 588f0c1fb0 Fix building on OSX, broken since "Clean up threading"
Commit

e4bff8be94 - Clean up threading

by @ShadowNinja has broken the OSX build.

Including things inside a namespace isn't good.

Also fixes #3124.
2015-09-06 05:34:02 +02:00
ShadowNinja e4bff8be94 Clean up threading
* Rename everything.
    * Strip J prefix.
    * Change UpperCamelCase functions to lowerCamelCase.
  * Remove global (!) semaphore count mutex on OSX.
  * Remove semaphore count getter (unused, unsafe, depended on internal
    API functions on Windows, and used a hack on OSX).
  * Add `Atomic<type>`.
  * Make `Thread` handle thread names.
  * Add support for C++11 multi-threading.
  * Combine pthread and win32 sources.
  * Remove `ThreadStarted` (unused, unneeded).
  * Move some includes from the headers to the sources.
  * Move all of `Event` into its header (allows inlining with no new includes).
  * Make `Event` use `Semaphore` (except on Windows).
  * Move some porting functions into `Thread`.
  * Integrate logging with `Thread`.
  * Add threading test.
2015-08-23 22:04:06 -04:00