summaryrefslogtreecommitdiff
path: root/src/threading/mutex.h
diff options
context:
space:
mode:
authorShadowNinja <shadowninja@minetest.net>2017-01-28 17:02:43 -0500
committerShadowNinja <shadowninja@minetest.net>2017-01-28 18:52:07 -0500
commitc93f7f5ceaf255bc9314be022b7bebb55b1d0605 (patch)
tree34b08b1680b9223587ed30043a02d64465467f3c /src/threading/mutex.h
parent79d752ba4f6f0197627cc20f99b2540f63b6dc88 (diff)
downloadminetest-c93f7f5ceaf255bc9314be022b7bebb55b1d0605.tar.gz
minetest-c93f7f5ceaf255bc9314be022b7bebb55b1d0605.tar.bz2
minetest-c93f7f5ceaf255bc9314be022b7bebb55b1d0605.zip
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.
Diffstat (limited to 'src/threading/mutex.h')
-rw-r--r--src/threading/mutex.h2
1 files changed, 2 insertions, 0 deletions
diff --git a/src/threading/mutex.h b/src/threading/mutex.h
index fb5c029fc..6feb23c25 100644
--- a/src/threading/mutex.h
+++ b/src/threading/mutex.h
@@ -56,6 +56,8 @@ public:
void lock();
void unlock();
+ bool try_lock();
+
protected:
Mutex(bool recursive);
void init_mutex(bool recursive);