From c3567a477601612ef3a60139a8b4d6bf798bea19 Mon Sep 17 00:00:00 2001 From: binary1248 Date: Sun, 2 Apr 2017 02:43:40 +0200 Subject: [PATCH] Removed SFML threading facilities. --- examples/voip/Server.cpp | 7 +- include/SFML/Audio/Music.hpp | 4 +- include/SFML/Audio/SoundRecorder.hpp | 4 +- include/SFML/Audio/SoundStream.hpp | 26 +- include/SFML/System.hpp | 5 - include/SFML/System/Lock.hpp | 139 --------- include/SFML/System/Mutex.hpp | 149 --------- include/SFML/System/Thread.hpp | 283 ------------------ include/SFML/System/Thread.inl | 87 ------ include/SFML/System/ThreadLocal.hpp | 104 ------- include/SFML/System/ThreadLocalPtr.hpp | 158 ---------- include/SFML/System/ThreadLocalPtr.inl | 77 ----- src/SFML/Audio/AlResource.cpp | 9 +- src/SFML/Audio/Music.cpp | 5 +- src/SFML/Audio/SoundRecorder.cpp | 19 +- src/SFML/Audio/SoundStream.cpp | 40 +-- src/SFML/Graphics/Shader.cpp | 2 - src/SFML/Graphics/Texture.cpp | 6 +- src/SFML/Main/MainAndroid.cpp | 32 +- src/SFML/System/Android/Activity.hpp | 4 +- src/SFML/System/Android/ResourceStream.cpp | 4 +- src/SFML/System/CMakeLists.txt | 23 -- src/SFML/System/Lock.cpp | 48 --- src/SFML/System/Mutex.cpp | 63 ---- src/SFML/System/Thread.cpp | 83 ----- src/SFML/System/ThreadLocal.cpp | 64 ---- src/SFML/System/Unix/MutexImpl.cpp | 69 ----- src/SFML/System/Unix/MutexImpl.hpp | 83 ----- src/SFML/System/Unix/ThreadImpl.cpp | 94 ------ src/SFML/System/Unix/ThreadImpl.hpp | 93 ------ src/SFML/System/Unix/ThreadLocalImpl.cpp | 65 ---- src/SFML/System/Unix/ThreadLocalImpl.hpp | 87 ------ src/SFML/System/Win32/ClockImpl.cpp | 9 +- src/SFML/System/Win32/MutexImpl.cpp | 64 ---- src/SFML/System/Win32/MutexImpl.hpp | 83 ----- src/SFML/System/Win32/ThreadImpl.cpp | 93 ------ src/SFML/System/Win32/ThreadImpl.hpp | 109 ------- src/SFML/System/Win32/ThreadLocalImpl.cpp | 64 ---- src/SFML/System/Win32/ThreadLocalImpl.hpp | 87 ------ src/SFML/Window/Android/InputImpl.cpp | 12 +- src/SFML/Window/Android/VideoModeImpl.cpp | 4 +- src/SFML/Window/Android/WindowImplAndroid.cpp | 12 +- src/SFML/Window/Context.cpp | 3 +- src/SFML/Window/EglContext.cpp | 7 +- src/SFML/Window/GlContext.cpp | 32 +- src/SFML/Window/Unix/Display.cpp | 9 +- src/SFML/Window/Unix/GlxContext.cpp | 11 +- src/SFML/Window/Unix/WindowImplX11.cpp | 11 +- src/SFML/Window/Win32/WglContext.cpp | 13 +- 49 files changed, 143 insertions(+), 2416 deletions(-) delete mode 100644 include/SFML/System/Lock.hpp delete mode 100644 include/SFML/System/Mutex.hpp delete mode 100644 include/SFML/System/Thread.hpp delete mode 100644 include/SFML/System/Thread.inl delete mode 100644 include/SFML/System/ThreadLocal.hpp delete mode 100644 include/SFML/System/ThreadLocalPtr.hpp delete mode 100644 include/SFML/System/ThreadLocalPtr.inl delete mode 100644 src/SFML/System/Lock.cpp delete mode 100644 src/SFML/System/Mutex.cpp delete mode 100644 src/SFML/System/Thread.cpp delete mode 100644 src/SFML/System/ThreadLocal.cpp delete mode 100644 src/SFML/System/Unix/MutexImpl.cpp delete mode 100644 src/SFML/System/Unix/MutexImpl.hpp delete mode 100644 src/SFML/System/Unix/ThreadImpl.cpp delete mode 100644 src/SFML/System/Unix/ThreadImpl.hpp delete mode 100644 src/SFML/System/Unix/ThreadLocalImpl.cpp delete mode 100644 src/SFML/System/Unix/ThreadLocalImpl.hpp delete mode 100644 src/SFML/System/Win32/MutexImpl.cpp delete mode 100644 src/SFML/System/Win32/MutexImpl.hpp delete mode 100644 src/SFML/System/Win32/ThreadImpl.cpp delete mode 100644 src/SFML/System/Win32/ThreadImpl.hpp delete mode 100644 src/SFML/System/Win32/ThreadLocalImpl.cpp delete mode 100644 src/SFML/System/Win32/ThreadLocalImpl.hpp diff --git a/examples/voip/Server.cpp b/examples/voip/Server.cpp index 0875bf95..4730cb83 100644 --- a/examples/voip/Server.cpp +++ b/examples/voip/Server.cpp @@ -7,6 +7,7 @@ #include #include #include +#include const sf::Uint8 audioData = 1; @@ -83,7 +84,7 @@ private: // Copy samples into a local buffer to avoid synchronization problems // (don't forget that we run in two separate threads) { - sf::Lock lock(m_mutex); + std::lock_guard lock(m_mutex); m_tempBuffer.assign(m_samples.begin() + m_offset, m_samples.end()); } @@ -132,7 +133,7 @@ private: // Don't forget that the other thread can access the sample array at any time // (so we protect any operation on it with the mutex) { - sf::Lock lock(m_mutex); + std::lock_guard lock(m_mutex); std::copy(samples, samples + sampleCount, std::back_inserter(m_samples)); } } @@ -156,7 +157,7 @@ private: //////////////////////////////////////////////////////////// sf::TcpListener m_listener; sf::TcpSocket m_client; - sf::Mutex m_mutex; + std::mutex m_mutex; std::vector m_samples; std::vector m_tempBuffer; std::size_t m_offset; diff --git a/include/SFML/Audio/Music.hpp b/include/SFML/Audio/Music.hpp index 5e272655..cf2fe0cd 100644 --- a/include/SFML/Audio/Music.hpp +++ b/include/SFML/Audio/Music.hpp @@ -31,10 +31,10 @@ #include #include #include -#include #include #include #include +#include namespace sf @@ -170,7 +170,7 @@ private: //////////////////////////////////////////////////////////// InputSoundFile m_file; ///< The streamed music file std::vector m_samples; ///< Temporary buffer of samples - Mutex m_mutex; ///< Mutex protecting the data + std::mutex m_mutex; ///< Mutex protecting the data }; } // namespace sf diff --git a/include/SFML/Audio/SoundRecorder.hpp b/include/SFML/Audio/SoundRecorder.hpp index 33c80b1f..eddc9ab0 100644 --- a/include/SFML/Audio/SoundRecorder.hpp +++ b/include/SFML/Audio/SoundRecorder.hpp @@ -30,10 +30,10 @@ //////////////////////////////////////////////////////////// #include #include -#include #include #include #include +#include namespace sf @@ -285,7 +285,7 @@ private: //////////////////////////////////////////////////////////// // Member data //////////////////////////////////////////////////////////// - Thread m_thread; ///< Thread running the background recording task + std::thread m_thread; ///< Thread running the background recording task std::vector m_samples; ///< Buffer to store captured samples unsigned int m_sampleRate; ///< Sample rate Time m_processingInterval; ///< Time period between calls to onProcessSamples diff --git a/include/SFML/Audio/SoundStream.hpp b/include/SFML/Audio/SoundStream.hpp index bd621727..70a094a2 100644 --- a/include/SFML/Audio/SoundStream.hpp +++ b/include/SFML/Audio/SoundStream.hpp @@ -30,10 +30,10 @@ //////////////////////////////////////////////////////////// #include #include -#include #include -#include #include +#include +#include namespace sf @@ -289,17 +289,17 @@ private: //////////////////////////////////////////////////////////// // Member data //////////////////////////////////////////////////////////// - Thread m_thread; ///< Thread running the background tasks - mutable Mutex m_threadMutex; ///< Thread mutex - Status m_threadStartState; ///< State the thread starts in (Playing, Paused, Stopped) - bool m_isStreaming; ///< Streaming state (true = playing, false = stopped) - unsigned int m_buffers[BufferCount]; ///< Sound buffers used to store temporary audio data - unsigned int m_channelCount; ///< Number of channels (1 = mono, 2 = stereo, ...) - unsigned int m_sampleRate; ///< Frequency (samples / second) - Uint32 m_format; ///< Format of the internal sound buffers - bool m_loop; ///< Loop flag (true to loop, false to play once) - Uint64 m_samplesProcessed; ///< Number of buffers processed since beginning of the stream - bool m_endBuffers[BufferCount]; ///< Each buffer is marked as "end buffer" or not, for proper duration calculation + std::thread m_thread; ///< Thread running the background tasks + mutable std::mutex m_threadMutex; ///< Thread mutex + Status m_threadStartState; ///< State the thread starts in (Playing, Paused, Stopped) + bool m_isStreaming; ///< Streaming state (true = playing, false = stopped) + unsigned int m_buffers[BufferCount]; ///< Sound buffers used to store temporary audio data + unsigned int m_channelCount; ///< Number of channels (1 = mono, 2 = stereo, ...) + unsigned int m_sampleRate; ///< Frequency (samples / second) + Uint32 m_format; ///< Format of the internal sound buffers + bool m_loop; ///< Loop flag (true to loop, false to play once) + Uint64 m_samplesProcessed; ///< Number of buffers processed since beginning of the stream + bool m_endBuffers[BufferCount]; ///< Each buffer is marked as "end buffer" or not, for proper duration calculation }; } // namespace sf diff --git a/include/SFML/System.hpp b/include/SFML/System.hpp index 5f18f766..98cf8486 100644 --- a/include/SFML/System.hpp +++ b/include/SFML/System.hpp @@ -34,15 +34,10 @@ #include #include #include -#include #include -#include #include #include #include -#include -#include -#include #include #include #include diff --git a/include/SFML/System/Lock.hpp b/include/SFML/System/Lock.hpp deleted file mode 100644 index eb420a8e..00000000 --- a/include/SFML/System/Lock.hpp +++ /dev/null @@ -1,139 +0,0 @@ -//////////////////////////////////////////////////////////// -// -// SFML - Simple and Fast Multimedia Library -// Copyright (C) 2007-2017 Laurent Gomila (laurent@sfml-dev.org) -// -// This software is provided 'as-is', without any express or implied warranty. -// In no event will the authors be held liable for any damages arising from the use of this software. -// -// Permission is granted to anyone to use this software for any purpose, -// including commercial applications, and to alter it and redistribute it freely, -// subject to the following restrictions: -// -// 1. The origin of this software must not be misrepresented; -// you must not claim that you wrote the original software. -// If you use this software in a product, an acknowledgment -// in the product documentation would be appreciated but is not required. -// -// 2. Altered source versions must be plainly marked as such, -// and must not be misrepresented as being the original software. -// -// 3. This notice may not be removed or altered from any source distribution. -// -//////////////////////////////////////////////////////////// - -#ifndef SFML_LOCK_HPP -#define SFML_LOCK_HPP - -//////////////////////////////////////////////////////////// -// Headers -//////////////////////////////////////////////////////////// -#include -#include - - -namespace sf -{ -class Mutex; - -//////////////////////////////////////////////////////////// -/// \brief Automatic wrapper for locking and unlocking mutexes -/// -//////////////////////////////////////////////////////////// -class SFML_SYSTEM_API Lock : NonCopyable -{ -public: - - //////////////////////////////////////////////////////////// - /// \brief Construct the lock with a target mutex - /// - /// The mutex passed to sf::Lock is automatically locked. - /// - /// \param mutex Mutex to lock - /// - //////////////////////////////////////////////////////////// - explicit Lock(Mutex& mutex); - - //////////////////////////////////////////////////////////// - /// \brief Destructor - /// - /// The destructor of sf::Lock automatically unlocks its mutex. - /// - //////////////////////////////////////////////////////////// - ~Lock(); - -private: - - //////////////////////////////////////////////////////////// - // Member data - //////////////////////////////////////////////////////////// - Mutex& m_mutex; ///< Mutex to lock / unlock -}; - -} // namespace sf - - -#endif // SFML_LOCK_HPP - - -//////////////////////////////////////////////////////////// -/// \class sf::Lock -/// \ingroup system -/// -/// sf::Lock is a RAII wrapper for sf::Mutex. By unlocking -/// it in its destructor, it ensures that the mutex will -/// always be released when the current scope (most likely -/// a function) ends. -/// This is even more important when an exception or an early -/// return statement can interrupt the execution flow of the -/// function. -/// -/// For maximum robustness, sf::Lock should always be used -/// to lock/unlock a mutex. -/// -/// Usage example: -/// \code -/// sf::Mutex mutex; -/// -/// void function() -/// { -/// sf::Lock lock(mutex); // mutex is now locked -/// -/// functionThatMayThrowAnException(); // mutex is unlocked if this function throws -/// -/// if (someCondition) -/// return; // mutex is unlocked -/// -/// } // mutex is unlocked -/// \endcode -/// -/// Because the mutex is not explicitly unlocked in the code, -/// it may remain locked longer than needed. If the region -/// of the code that needs to be protected by the mutex is -/// not the entire function, a good practice is to create a -/// smaller, inner scope so that the lock is limited to this -/// part of the code. -/// -/// \code -/// sf::Mutex mutex; -/// -/// void function() -/// { -/// { -/// sf::Lock lock(mutex); -/// codeThatRequiresProtection(); -/// -/// } // mutex is unlocked here -/// -/// codeThatDoesntCareAboutTheMutex(); -/// } -/// \endcode -/// -/// Having a mutex locked longer than required is a bad practice -/// which can lead to bad performances. Don't forget that when -/// a mutex is locked, other threads may be waiting doing nothing -/// until it is released. -/// -/// \see sf::Mutex -/// -//////////////////////////////////////////////////////////// diff --git a/include/SFML/System/Mutex.hpp b/include/SFML/System/Mutex.hpp deleted file mode 100644 index 7ac8e8ad..00000000 --- a/include/SFML/System/Mutex.hpp +++ /dev/null @@ -1,149 +0,0 @@ -//////////////////////////////////////////////////////////// -// -// SFML - Simple and Fast Multimedia Library -// Copyright (C) 2007-2017 Laurent Gomila (laurent@sfml-dev.org) -// -// This software is provided 'as-is', without any express or implied warranty. -// In no event will the authors be held liable for any damages arising from the use of this software. -// -// Permission is granted to anyone to use this software for any purpose, -// including commercial applications, and to alter it and redistribute it freely, -// subject to the following restrictions: -// -// 1. The origin of this software must not be misrepresented; -// you must not claim that you wrote the original software. -// If you use this software in a product, an acknowledgment -// in the product documentation would be appreciated but is not required. -// -// 2. Altered source versions must be plainly marked as such, -// and must not be misrepresented as being the original software. -// -// 3. This notice may not be removed or altered from any source distribution. -// -//////////////////////////////////////////////////////////// - -#ifndef SFML_MUTEX_HPP -#define SFML_MUTEX_HPP - -//////////////////////////////////////////////////////////// -// Headers -//////////////////////////////////////////////////////////// -#include -#include -#include - - -namespace sf -{ -namespace priv -{ - class MutexImpl; -} - -//////////////////////////////////////////////////////////// -/// \brief Blocks concurrent access to shared resources -/// from multiple threads -/// -//////////////////////////////////////////////////////////// -class SFML_SYSTEM_API Mutex : NonCopyable -{ -public: - - //////////////////////////////////////////////////////////// - /// \brief Default constructor - /// - //////////////////////////////////////////////////////////// - Mutex(); - - //////////////////////////////////////////////////////////// - /// \brief Destructor - /// - //////////////////////////////////////////////////////////// - ~Mutex(); - - //////////////////////////////////////////////////////////// - /// \brief Lock the mutex - /// - /// If the mutex is already locked in another thread, - /// this call will block the execution until the mutex - /// is released. - /// - /// \see unlock - /// - //////////////////////////////////////////////////////////// - void lock(); - - //////////////////////////////////////////////////////////// - /// \brief Unlock the mutex - /// - /// \see lock - /// - //////////////////////////////////////////////////////////// - void unlock(); - -private: - - //////////////////////////////////////////////////////////// - // Member data - //////////////////////////////////////////////////////////// - std::shared_ptr m_mutexImpl; ///< OS-specific implementation -}; - -} // namespace sf - - -#endif // SFML_MUTEX_HPP - - -//////////////////////////////////////////////////////////// -/// \class sf::Mutex -/// \ingroup system -/// -/// Mutex stands for "MUTual EXclusion". A mutex is a -/// synchronization object, used when multiple threads are involved. -/// -/// When you want to protect a part of the code from being accessed -/// simultaneously by multiple threads, you typically use a -/// mutex. When a thread is locked by a mutex, any other thread -/// trying to lock it will be blocked until the mutex is released -/// by the thread that locked it. This way, you can allow only -/// one thread at a time to access a critical region of your code. -/// -/// Usage example: -/// \code -/// Database database; // this is a critical resource that needs some protection -/// sf::Mutex mutex; -/// -/// void thread1() -/// { -/// mutex.lock(); // this call will block the thread if the mutex is already locked by thread2 -/// database.write(...); -/// mutex.unlock(); // if thread2 was waiting, it will now be unblocked -/// } -/// -/// void thread2() -/// { -/// mutex.lock(); // this call will block the thread if the mutex is already locked by thread1 -/// database.write(...); -/// mutex.unlock(); // if thread1 was waiting, it will now be unblocked -/// } -/// \endcode -/// -/// Be very careful with mutexes. A bad usage can lead to bad problems, -/// like deadlocks (two threads are waiting for each other and the -/// application is globally stuck). -/// -/// To make the usage of mutexes more robust, particularly in -/// environments where exceptions can be thrown, you should -/// use the helper class sf::Lock to lock/unlock mutexes. -/// -/// SFML mutexes are recursive, which means that you can lock -/// a mutex multiple times in the same thread without creating -/// a deadlock. In this case, the first call to lock() behaves -/// as usual, and the following ones have no effect. -/// However, you must call unlock() exactly as many times as you -/// called lock(). If you don't, the mutex won't be released. -/// -/// \see sf::Lock -/// -//////////////////////////////////////////////////////////// diff --git a/include/SFML/System/Thread.hpp b/include/SFML/System/Thread.hpp deleted file mode 100644 index 45c5d62f..00000000 --- a/include/SFML/System/Thread.hpp +++ /dev/null @@ -1,283 +0,0 @@ -//////////////////////////////////////////////////////////// -// -// SFML - Simple and Fast Multimedia Library -// Copyright (C) 2007-2017 Laurent Gomila (laurent@sfml-dev.org) -// -// This software is provided 'as-is', without any express or implied warranty. -// In no event will the authors be held liable for any damages arising from the use of this software. -// -// Permission is granted to anyone to use this software for any purpose, -// including commercial applications, and to alter it and redistribute it freely, -// subject to the following restrictions: -// -// 1. The origin of this software must not be misrepresented; -// you must not claim that you wrote the original software. -// If you use this software in a product, an acknowledgment -// in the product documentation would be appreciated but is not required. -// -// 2. Altered source versions must be plainly marked as such, -// and must not be misrepresented as being the original software. -// -// 3. This notice may not be removed or altered from any source distribution. -// -//////////////////////////////////////////////////////////// - -#ifndef SFML_THREAD_HPP -#define SFML_THREAD_HPP - -//////////////////////////////////////////////////////////// -// Headers -//////////////////////////////////////////////////////////// -#include -#include -#include -#include - - -namespace sf -{ -namespace priv -{ - class ThreadImpl; - struct ThreadFunc; -} - -//////////////////////////////////////////////////////////// -/// \brief Utility class to manipulate threads -/// -//////////////////////////////////////////////////////////// -class SFML_SYSTEM_API Thread : NonCopyable -{ -public: - - //////////////////////////////////////////////////////////// - /// \brief Construct the thread from a functor with no argument - /// - /// This constructor works for function objects, as well - /// as free functions. - /// - /// Use this constructor for this kind of function: - /// \code - /// void function(); - /// - /// // --- or ---- - /// - /// struct Functor - /// { - /// void operator()(); - /// }; - /// \endcode - /// Note: this does *not* run the thread, use launch(). - /// - /// \param function Functor or free function to use as the entry point of the thread - /// - //////////////////////////////////////////////////////////// - template - Thread(F function); - - //////////////////////////////////////////////////////////// - /// \brief Construct the thread from a functor with an argument - /// - /// This constructor works for function objects, as well - /// as free functions. - /// It is a template, which means that the argument can - /// have any type (int, std::string, void*, Toto, ...). - /// - /// Use this constructor for this kind of function: - /// \code - /// void function(int arg); - /// - /// // --- or ---- - /// - /// struct Functor - /// { - /// void operator()(std::string arg); - /// }; - /// \endcode - /// Note: this does *not* run the thread, use launch(). - /// - /// \param function Functor or free function to use as the entry point of the thread - /// \param argument argument to forward to the function - /// - //////////////////////////////////////////////////////////// - template - Thread(F function, A argument); - - //////////////////////////////////////////////////////////// - /// \brief Construct the thread from a member function and an object - /// - /// This constructor is a template, which means that you can - /// use it with any class. - /// Use this constructor for this kind of function: - /// \code - /// class MyClass - /// { - /// public: - /// - /// void function(); - /// }; - /// \endcode - /// Note: this does *not* run the thread, use launch(). - /// - /// \param function Entry point of the thread - /// \param object Pointer to the object to use - /// - //////////////////////////////////////////////////////////// - template - Thread(void(C::*function)(), C* object); - - //////////////////////////////////////////////////////////// - /// \brief Destructor - /// - /// This destructor calls wait(), so that the internal thread - /// cannot survive after its sf::Thread instance is destroyed. - /// - //////////////////////////////////////////////////////////// - ~Thread(); - - //////////////////////////////////////////////////////////// - /// \brief Run the thread - /// - /// This function starts the entry point passed to the - /// thread's constructor, and returns immediately. - /// After this function returns, the thread's function is - /// running in parallel to the calling code. - /// - //////////////////////////////////////////////////////////// - void launch(); - - //////////////////////////////////////////////////////////// - /// \brief Wait until the thread finishes - /// - /// This function will block the execution until the - /// thread's function ends. - /// Warning: if the thread function never ends, the calling - /// thread will block forever. - /// If this function is called from its owner thread, it - /// returns without doing anything. - /// - //////////////////////////////////////////////////////////// - void wait(); - - //////////////////////////////////////////////////////////// - /// \brief Terminate the thread - /// - /// This function immediately stops the thread, without waiting - /// for its function to finish. - /// Terminating a thread with this function is not safe, - /// and can lead to local variables not being destroyed - /// on some operating systems. You should rather try to make - /// the thread function terminate by itself. - /// - //////////////////////////////////////////////////////////// - void terminate(); - -private: - - friend class priv::ThreadImpl; - - //////////////////////////////////////////////////////////// - /// \brief Internal entry point of the thread - /// - /// This function is called by the thread implementation. - /// - //////////////////////////////////////////////////////////// - void run(); - - //////////////////////////////////////////////////////////// - // Member data - //////////////////////////////////////////////////////////// - std::shared_ptr m_impl; ///< OS-specific implementation of the thread - std::shared_ptr m_entryPoint; ///< Abstraction of the function to run -}; - -#include - -} // namespace sf - -#endif // SFML_THREAD_HPP - - -//////////////////////////////////////////////////////////// -/// \class sf::Thread -/// \ingroup system -/// -/// Threads provide a way to run multiple parts of the code -/// in parallel. When you launch a new thread, the execution -/// is split and both the new thread and the caller run -/// in parallel. -/// -/// To use a sf::Thread, you construct it directly with the -/// function to execute as the entry point of the thread. -/// sf::Thread has multiple template constructors, which means -/// that you can use several types of entry points: -/// \li non-member functions with no argument -/// \li non-member functions with one argument of any type -/// \li functors with no argument (this one is particularly useful for compatibility with boost/std::%bind) -/// \li functors with one argument of any type -/// \li member functions from any class with no argument -/// -/// The function argument, if any, is copied in the sf::Thread -/// instance, as well as the functor (if the corresponding -/// constructor is used). Class instances, however, are passed -/// by pointer so you must make sure that the object won't be -/// destroyed while the thread is still using it. -/// -/// The thread ends when its function is terminated. If the -/// owner sf::Thread instance is destroyed before the -/// thread is finished, the destructor will wait (see wait()) -/// -/// Usage examples: -/// \code -/// // example 1: non member function with one argument -/// -/// void threadFunc(int argument) -/// { -/// ... -/// } -/// -/// sf::Thread thread(&threadFunc, 5); -/// thread.launch(); // start the thread (internally calls threadFunc(5)) -/// \endcode -/// -/// \code -/// // example 2: member function -/// -/// class Task -/// { -/// public: -/// void run() -/// { -/// ... -/// } -/// }; -/// -/// Task task; -/// sf::Thread thread(&Task::run, &task); -/// thread.launch(); // start the thread (internally calls task.run()) -/// \endcode -/// -/// \code -/// // example 3: functor -/// -/// struct Task -/// { -/// void operator()() -/// { -/// ... -/// } -/// }; -/// -/// sf::Thread thread(Task()); -/// thread.launch(); // start the thread (internally calls operator() on the Task instance) -/// \endcode -/// -/// Creating parallel threads of execution can be dangerous: -/// all threads inside the same process share the same memory space, -/// which means that you may end up accessing the same variable -/// from multiple threads at the same time. To prevent this -/// kind of situations, you can use mutexes (see sf::Mutex). -/// -/// \see sf::Mutex -/// -//////////////////////////////////////////////////////////// diff --git a/include/SFML/System/Thread.inl b/include/SFML/System/Thread.inl deleted file mode 100644 index fd3602fe..00000000 --- a/include/SFML/System/Thread.inl +++ /dev/null @@ -1,87 +0,0 @@ -//////////////////////////////////////////////////////////// -// -// SFML - Simple and Fast Multimedia Library -// Copyright (C) 2007-2017 Laurent Gomila (laurent@sfml-dev.org) -// -// This software is provided 'as-is', without any express or implied warranty. -// In no event will the authors be held liable for any damages arising from the use of this software. -// -// Permission is granted to anyone to use this software for any purpose, -// including commercial applications, and to alter it and redistribute it freely, -// subject to the following restrictions: -// -// 1. The origin of this software must not be misrepresented; -// you must not claim that you wrote the original software. -// If you use this software in a product, an acknowledgment -// in the product documentation would be appreciated but is not required. -// -// 2. Altered source versions must be plainly marked as such, -// and must not be misrepresented as being the original software. -// -// 3. This notice may not be removed or altered from any source distribution. -// -//////////////////////////////////////////////////////////// - -namespace priv -{ -// Base class for abstract thread functions -struct ThreadFunc -{ - virtual ~ThreadFunc() {} - virtual void run() = 0; -}; - -// Specialization using a functor (including free functions) with no argument -template -struct ThreadFunctor : ThreadFunc -{ - ThreadFunctor(T functor) : m_functor(functor) {} - virtual void run() {m_functor();} - T m_functor; -}; - -// Specialization using a functor (including free functions) with one argument -template -struct ThreadFunctorWithArg : ThreadFunc -{ - ThreadFunctorWithArg(F function, A arg) : m_function(function), m_arg(arg) {} - virtual void run() {m_function(m_arg);} - F m_function; - A m_arg; -}; - -// Specialization using a member function -template -struct ThreadMemberFunc : ThreadFunc -{ - ThreadMemberFunc(void(C::*function)(), C* object) : m_function(function), m_object(object) {} - virtual void run() {(m_object->*m_function)();} - void(C::*m_function)(); - C* m_object; -}; - -} // namespace priv - - -//////////////////////////////////////////////////////////// -template -Thread::Thread(F functor) : -m_entryPoint(std::make_shared>(functor)) -{ -} - - -//////////////////////////////////////////////////////////// -template -Thread::Thread(F function, A argument) : -m_entryPoint(std::make_shared>(function, argument)) -{ -} - - -//////////////////////////////////////////////////////////// -template -Thread::Thread(void(C::*function)(), C* object) : -m_entryPoint(std::make_shared>(function, object)) -{ -} diff --git a/include/SFML/System/ThreadLocal.hpp b/include/SFML/System/ThreadLocal.hpp deleted file mode 100644 index 160abdfd..00000000 --- a/include/SFML/System/ThreadLocal.hpp +++ /dev/null @@ -1,104 +0,0 @@ -//////////////////////////////////////////////////////////// -// -// SFML - Simple and Fast Multimedia Library -// Copyright (C) 2007-2017 Laurent Gomila (laurent@sfml-dev.org) -// -// This software is provided 'as-is', without any express or implied warranty. -// In no event will the authors be held liable for any damages arising from the use of this software. -// -// Permission is granted to anyone to use this software for any purpose, -// including commercial applications, and to alter it and redistribute it freely, -// subject to the following restrictions: -// -// 1. The origin of this software must not be misrepresented; -// you must not claim that you wrote the original software. -// If you use this software in a product, an acknowledgment -// in the product documentation would be appreciated but is not required. -// -// 2. Altered source versions must be plainly marked as such, -// and must not be misrepresented as being the original software. -// -// 3. This notice may not be removed or altered from any source distribution. -// -//////////////////////////////////////////////////////////// - -#ifndef SFML_THREADLOCAL_HPP -#define SFML_THREADLOCAL_HPP - -//////////////////////////////////////////////////////////// -// Headers -//////////////////////////////////////////////////////////// -#include -#include -#include -#include - - -namespace sf -{ -namespace priv -{ - class ThreadLocalImpl; -} - -//////////////////////////////////////////////////////////// -/// \brief Defines variables with thread-local storage -/// -//////////////////////////////////////////////////////////// -class SFML_SYSTEM_API ThreadLocal : NonCopyable -{ -public: - - //////////////////////////////////////////////////////////// - /// \brief Default constructor - /// - /// \param value Optional value to initialize the variable - /// - //////////////////////////////////////////////////////////// - ThreadLocal(void* value = NULL); - - //////////////////////////////////////////////////////////// - /// \brief Destructor - /// - //////////////////////////////////////////////////////////// - ~ThreadLocal(); - - //////////////////////////////////////////////////////////// - /// \brief Set the thread-specific value of the variable - /// - /// \param value Value of the variable for the current thread - /// - //////////////////////////////////////////////////////////// - void setValue(void* value); - - //////////////////////////////////////////////////////////// - /// \brief Retrieve the thread-specific value of the variable - /// - /// \return Value of the variable for the current thread - /// - //////////////////////////////////////////////////////////// - void* getValue() const; - -private: - - //////////////////////////////////////////////////////////// - // Member data - //////////////////////////////////////////////////////////// - std::unique_ptr m_impl; ///< Pointer to the OS specific implementation -}; - -} // namespace sf - - -#endif // SFML_THREADLOCAL_HPP - - -//////////////////////////////////////////////////////////// -/// \class sf::ThreadLocal -/// \ingroup system -/// -/// This class manipulates void* parameters and thus is not -/// appropriate for strongly-typed variables. You should rather -/// use the sf::ThreadLocalPtr template class. -/// -//////////////////////////////////////////////////////////// diff --git a/include/SFML/System/ThreadLocalPtr.hpp b/include/SFML/System/ThreadLocalPtr.hpp deleted file mode 100644 index 751a9b53..00000000 --- a/include/SFML/System/ThreadLocalPtr.hpp +++ /dev/null @@ -1,158 +0,0 @@ -//////////////////////////////////////////////////////////// -// -// SFML - Simple and Fast Multimedia Library -// Copyright (C) 2007-2017 Laurent Gomila (laurent@sfml-dev.org) -// -// This software is provided 'as-is', without any express or implied warranty. -// In no event will the authors be held liable for any damages arising from the use of this software. -// -// Permission is granted to anyone to use this software for any purpose, -// including commercial applications, and to alter it and redistribute it freely, -// subject to the following restrictions: -// -// 1. The origin of this software must not be misrepresented; -// you must not claim that you wrote the original software. -// If you use this software in a product, an acknowledgment -// in the product documentation would be appreciated but is not required. -// -// 2. Altered source versions must be plainly marked as such, -// and must not be misrepresented as being the original software. -// -// 3. This notice may not be removed or altered from any source distribution. -// -//////////////////////////////////////////////////////////// - -#ifndef SFML_THREADLOCALPTR_HPP -#define SFML_THREADLOCALPTR_HPP - -//////////////////////////////////////////////////////////// -// Headers -//////////////////////////////////////////////////////////// -#include - - -namespace sf -{ -//////////////////////////////////////////////////////////// -/// \brief Pointer to a thread-local variable -/// -//////////////////////////////////////////////////////////// -template -class ThreadLocalPtr : private ThreadLocal -{ -public: - - //////////////////////////////////////////////////////////// - /// \brief Default constructor - /// - /// \param value Optional value to initialize the variable - /// - //////////////////////////////////////////////////////////// - ThreadLocalPtr(T* value = NULL); - - //////////////////////////////////////////////////////////// - /// \brief Overload of unary operator * - /// - /// Like raw pointers, applying the * operator returns a - /// reference to the pointed-to object. - /// - /// \return Reference to the thread-local variable - /// - //////////////////////////////////////////////////////////// - T& operator *() const; - - //////////////////////////////////////////////////////////// - /// \brief Overload of operator -> - /// - /// Similarly to raw pointers, applying the -> operator - /// returns the pointed-to object. - /// - /// \return Pointer to the thread-local variable - /// - //////////////////////////////////////////////////////////// - T* operator ->() const; - - //////////////////////////////////////////////////////////// - /// \brief Conversion operator to implicitly convert the - /// pointer to its raw pointer type (T*) - /// - /// \return Pointer to the actual object - /// - //////////////////////////////////////////////////////////// - operator T*() const; - - //////////////////////////////////////////////////////////// - /// \brief Assignment operator for a raw pointer parameter - /// - /// \param value Pointer to assign - /// - /// \return Reference to self - /// - //////////////////////////////////////////////////////////// - ThreadLocalPtr& operator =(T* value); - - //////////////////////////////////////////////////////////// - /// \brief Assignment operator for a ThreadLocalPtr parameter - /// - /// \param right ThreadLocalPtr to assign - /// - /// \return Reference to self - /// - //////////////////////////////////////////////////////////// - ThreadLocalPtr& operator =(const ThreadLocalPtr& right); -}; - -} // namespace sf - -#include - - -#endif // SFML_THREADLOCALPTR_HPP - - -//////////////////////////////////////////////////////////// -/// \class sf::ThreadLocalPtr -/// \ingroup system -/// -/// sf::ThreadLocalPtr is a type-safe wrapper for storing -/// pointers to thread-local variables. A thread-local -/// variable holds a different value for each different -/// thread, unlike normal variables that are shared. -/// -/// Its usage is completely transparent, so that it is similar -/// to manipulating the raw pointer directly (like any smart pointer). -/// -/// Usage example: -/// \code -/// MyClass object1; -/// MyClass object2; -/// sf::ThreadLocalPtr objectPtr; -/// -/// void thread1() -/// { -/// objectPtr = &object1; // doesn't impact thread2 -/// ... -/// } -/// -/// void thread2() -/// { -/// objectPtr = &object2; // doesn't impact thread1 -/// ... -/// } -/// -/// int main() -/// { -/// // Create and launch the two threads -/// sf::Thread t1(&thread1); -/// sf::Thread t2(&thread2); -/// t1.launch(); -/// t2.launch(); -/// -/// return 0; -/// } -/// \endcode -/// -/// ThreadLocalPtr is designed for internal use; however you -/// can use it if you feel like it fits well your implementation. -/// -//////////////////////////////////////////////////////////// diff --git a/include/SFML/System/ThreadLocalPtr.inl b/include/SFML/System/ThreadLocalPtr.inl deleted file mode 100644 index 5652f56e..00000000 --- a/include/SFML/System/ThreadLocalPtr.inl +++ /dev/null @@ -1,77 +0,0 @@ -//////////////////////////////////////////////////////////// -// -// SFML - Simple and Fast Multimedia Library -// Copyright (C) 2007-2017 Laurent Gomila (laurent@sfml-dev.org) -// -// This software is provided 'as-is', without any express or implied warranty. -// In no event will the authors be held liable for any damages arising from the use of this software. -// -// Permission is granted to anyone to use this software for any purpose, -// including commercial applications, and to alter it and redistribute it freely, -// subject to the following restrictions: -// -// 1. The origin of this software must not be misrepresented; -// you must not claim that you wrote the original software. -// If you use this software in a product, an acknowledgment -// in the product documentation would be appreciated but is not required. -// -// 2. Altered source versions must be plainly marked as such, -// and must not be misrepresented as being the original software. -// -// 3. This notice may not be removed or altered from any source distribution. -// -//////////////////////////////////////////////////////////// - - -namespace sf -{ -//////////////////////////////////////////////////////////// -template -ThreadLocalPtr::ThreadLocalPtr(T* value) : -ThreadLocal(value) -{ -} - - -//////////////////////////////////////////////////////////// -template -T& ThreadLocalPtr::operator *() const -{ - return *static_cast(getValue()); -} - - -//////////////////////////////////////////////////////////// -template -T* ThreadLocalPtr::operator ->() const -{ - return static_cast(getValue()); -} - - -//////////////////////////////////////////////////////////// -template -ThreadLocalPtr::operator T*() const -{ - return static_cast(getValue()); -} - - -//////////////////////////////////////////////////////////// -template -ThreadLocalPtr& ThreadLocalPtr::operator =(T* value) -{ - setValue(value); - return *this; -} - - -//////////////////////////////////////////////////////////// -template -ThreadLocalPtr& ThreadLocalPtr::operator =(const ThreadLocalPtr& right) -{ - setValue(right.getValue()); - return *this; -} - -} // namespace sf diff --git a/src/SFML/Audio/AlResource.cpp b/src/SFML/Audio/AlResource.cpp index 6dd1d207..82febebf 100644 --- a/src/SFML/Audio/AlResource.cpp +++ b/src/SFML/Audio/AlResource.cpp @@ -27,16 +27,15 @@ //////////////////////////////////////////////////////////// #include #include -#include -#include #include +#include namespace { // OpenAL resources counter and its mutex unsigned int count = 0; - sf::Mutex mutex; + std::mutex mutex; // The audio device is instantiated on demand rather than at global startup, // which solves a lot of weird crashes and errors. @@ -51,7 +50,7 @@ namespace sf AlResource::AlResource() { // Protect from concurrent access - Lock lock(mutex); + std::lock_guard lock(mutex); // If this is the very first resource, trigger the global device initialization if (count == 0) @@ -66,7 +65,7 @@ AlResource::AlResource() AlResource::~AlResource() { // Protect from concurrent access - Lock lock(mutex); + std::lock_guard lock(mutex); // Decrement the resources counter count--; diff --git a/src/SFML/Audio/Music.cpp b/src/SFML/Audio/Music.cpp index e959cea8..3c3bae7f 100644 --- a/src/SFML/Audio/Music.cpp +++ b/src/SFML/Audio/Music.cpp @@ -27,7 +27,6 @@ //////////////////////////////////////////////////////////// #include #include -#include #include #include @@ -111,7 +110,7 @@ Time Music::getDuration() const //////////////////////////////////////////////////////////// bool Music::onGetData(SoundStream::Chunk& data) { - Lock lock(m_mutex); + std::lock_guard lock(m_mutex); // Fill the chunk parameters data.samples = &m_samples[0]; @@ -125,7 +124,7 @@ bool Music::onGetData(SoundStream::Chunk& data) //////////////////////////////////////////////////////////// void Music::onSeek(Time timeOffset) { - Lock lock(m_mutex); + std::lock_guard lock(m_mutex); m_file.seek(timeOffset); } diff --git a/src/SFML/Audio/SoundRecorder.cpp b/src/SFML/Audio/SoundRecorder.cpp index 7105fbdf..0e41f27d 100644 --- a/src/SFML/Audio/SoundRecorder.cpp +++ b/src/SFML/Audio/SoundRecorder.cpp @@ -47,7 +47,6 @@ namespace sf { //////////////////////////////////////////////////////////// SoundRecorder::SoundRecorder() : -m_thread (&SoundRecorder::record, this), m_sampleRate (0), m_processingInterval(milliseconds(100)), m_isCapturing (false), @@ -112,7 +111,10 @@ bool SoundRecorder::start(unsigned int sampleRate) // Start the capture in a new thread, to avoid blocking the main thread m_isCapturing = true; - m_thread.launch(); + m_thread = std::thread([this] + { + record(); + }); return true; } @@ -128,7 +130,9 @@ void SoundRecorder::stop() if (m_isCapturing) { m_isCapturing = false; - m_thread.wait(); + + if (m_thread.joinable()) + m_thread.join(); // Notify derived class onStop(); @@ -182,7 +186,9 @@ bool SoundRecorder::setDevice(const std::string& name) { // Stop the capturing thread m_isCapturing = false; - m_thread.wait(); + + if (m_thread.joinable()) + m_thread.join(); // Determine the recording format ALCenum format = (m_channelCount == 1) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16; @@ -203,7 +209,10 @@ bool SoundRecorder::setDevice(const std::string& name) // Start the capture in a new thread, to avoid blocking the main thread m_isCapturing = true; - m_thread.launch(); + m_thread = std::thread([this] + { + record(); + }); } return true; diff --git a/src/SFML/Audio/SoundStream.cpp b/src/SFML/Audio/SoundStream.cpp index baefc097..3d6b01e0 100644 --- a/src/SFML/Audio/SoundStream.cpp +++ b/src/SFML/Audio/SoundStream.cpp @@ -30,7 +30,6 @@ #include #include #include -#include #ifdef _MSC_VER #pragma warning(disable: 4355) // 'this' used in base member initializer list @@ -41,7 +40,6 @@ namespace sf { //////////////////////////////////////////////////////////// SoundStream::SoundStream() : -m_thread (&SoundStream::streamData, this), m_threadMutex (), m_threadStartState(Stopped), m_isStreaming (false), @@ -64,12 +62,13 @@ SoundStream::~SoundStream() // Request the thread to terminate { - Lock lock(m_threadMutex); + std::lock_guard lock(m_threadMutex); m_isStreaming = false; } // Wait for the thread to terminate - m_thread.wait(); + if (m_thread.joinable()) + m_thread.join(); } @@ -108,7 +107,7 @@ void SoundStream::play() Status threadStartState = Stopped; { - Lock lock(m_threadMutex); + std::lock_guard lock(m_threadMutex); isStreaming = m_isStreaming; threadStartState = m_threadStartState; @@ -118,7 +117,7 @@ void SoundStream::play() if (isStreaming && (threadStartState == Paused)) { // If the sound is paused, resume it - Lock lock(m_threadMutex); + std::lock_guard lock(m_threadMutex); m_threadStartState = Playing; alCheck(alSourcePlay(m_source)); return; @@ -132,7 +131,10 @@ void SoundStream::play() // Start updating the stream in a separate thread to avoid blocking the application m_isStreaming = true; m_threadStartState = Playing; - m_thread.launch(); + m_thread = std::thread([this] + { + streamData(); + }); } @@ -141,7 +143,7 @@ void SoundStream::pause() { // Handle pause() being called before the thread has started { - Lock lock(m_threadMutex); + std::lock_guard lock(m_threadMutex); if (!m_isStreaming) return; @@ -158,12 +160,13 @@ void SoundStream::stop() { // Request the thread to terminate { - Lock lock(m_threadMutex); + std::lock_guard lock(m_threadMutex); m_isStreaming = false; } // Wait for the thread to terminate - m_thread.wait(); + if (m_thread.joinable()) + m_thread.join(); // Move to the beginning onSeek(Time::Zero); @@ -195,7 +198,7 @@ SoundStream::Status SoundStream::getStatus() const // To compensate for the lag between play() and alSourceplay() if (status == Stopped) { - Lock lock(m_threadMutex); + std::lock_guard lock(m_threadMutex); if (m_isStreaming) status = m_threadStartState; @@ -225,7 +228,10 @@ void SoundStream::setPlayingOffset(Time timeOffset) m_isStreaming = true; m_threadStartState = oldStatus; - m_thread.launch(); + m_thread = std::thread([this] + { + streamData(); + }); } @@ -266,7 +272,7 @@ void SoundStream::streamData() bool requestStop = false; { - Lock lock(m_threadMutex); + std::lock_guard lock(m_threadMutex); // Check if the thread was launched Stopped if (m_threadStartState == Stopped) @@ -288,7 +294,7 @@ void SoundStream::streamData() alCheck(alSourcePlay(m_source)); { - Lock lock(m_threadMutex); + std::lock_guard lock(m_threadMutex); // Check if the thread was launched Paused if (m_threadStartState == Paused) @@ -298,7 +304,7 @@ void SoundStream::streamData() for (;;) { { - Lock lock(m_threadMutex); + std::lock_guard lock(m_threadMutex); if (!m_isStreaming) break; } @@ -314,7 +320,7 @@ void SoundStream::streamData() else { // End streaming - Lock lock(m_threadMutex); + std::lock_guard lock(m_threadMutex); m_isStreaming = false; } } @@ -358,7 +364,7 @@ void SoundStream::streamData() << "and initialize() has been called correctly" << std::endl; // Abort streaming (exit main loop) - Lock lock(m_threadMutex); + std::lock_guard lock(m_threadMutex); m_isStreaming = false; requestStop = true; break; diff --git a/src/SFML/Graphics/Shader.cpp b/src/SFML/Graphics/Shader.cpp index 5d36e14c..bae40eee 100644 --- a/src/SFML/Graphics/Shader.cpp +++ b/src/SFML/Graphics/Shader.cpp @@ -33,8 +33,6 @@ #include #include #include -#include -#include #include #include #include diff --git a/src/SFML/Graphics/Texture.cpp b/src/SFML/Graphics/Texture.cpp index 8825d75a..4c09dcf5 100644 --- a/src/SFML/Graphics/Texture.cpp +++ b/src/SFML/Graphics/Texture.cpp @@ -31,8 +31,6 @@ #include #include #include -#include -#include #include #include #include @@ -41,13 +39,13 @@ namespace { - sf::Mutex idMutex; + std::mutex idMutex; // Thread-safe unique identifier generator, // is used for states cache (see RenderTarget) sf::Uint64 getUniqueId() { - sf::Lock lock(idMutex); + std::lock_guard lock(idMutex); static sf::Uint64 id = 1; // start at 1, zero is "no texture" diff --git a/src/SFML/Main/MainAndroid.cpp b/src/SFML/Main/MainAndroid.cpp index 595a3a26..c5a352e0 100644 --- a/src/SFML/Main/MainAndroid.cpp +++ b/src/SFML/Main/MainAndroid.cpp @@ -40,11 +40,11 @@ #include #include -#include -#include #include #include #include +#include +#include extern int main(int argc, char *argv[]); @@ -86,7 +86,7 @@ ActivityStates* retrieveStates(ANativeActivity* activity) static void initializeMain(ActivityStates* states) { // Protect from concurrent access - Lock lock(states->mutex); + std::lock_guard lock(states->mutex); // Prepare and share the looper to be read later ALooper* looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS); @@ -102,7 +102,7 @@ static void initializeMain(ActivityStates* states) static void terminateMain(ActivityStates* states) { // Protect from concurrent access - Lock lock(states->mutex); + std::lock_guard lock(states->mutex); // The main thread has finished, we must explicitly ask the activity to finish states->mainOver = true; @@ -123,7 +123,7 @@ void* main(ActivityStates* states) terminateMain(states); { - Lock lock(states->mutex); + std::lock_guard lock(states->mutex); states->terminated = true; } @@ -241,7 +241,7 @@ static void onResume(ANativeActivity* activity) { // Retrieve our activity states from the activity instance sf::priv::ActivityStates* states = sf::priv::retrieveStates(activity); - sf::Lock lock(states->mutex); + std::lock_guard lock(states->mutex); if (states->fullscreen) goToFullscreenMode(activity); @@ -259,7 +259,7 @@ static void onPause(ANativeActivity* activity) { // Retrieve our activity states from the activity instance sf::priv::ActivityStates* states = sf::priv::retrieveStates(activity); - sf::Lock lock(states->mutex); + std::lock_guard lock(states->mutex); // Send an event to warn people the activity has been paused sf::Event event; @@ -283,7 +283,7 @@ static void onDestroy(ANativeActivity* activity) // Send an event to warn people the activity is being destroyed { - sf::Lock lock(states->mutex); + std::lock_guard lock(states->mutex); // If the main thread hasn't yet finished, send the event and wait for // it to finish. @@ -325,7 +325,7 @@ static void onDestroy(ANativeActivity* activity) static void onNativeWindowCreated(ANativeActivity* activity, ANativeWindow* window) { sf::priv::ActivityStates* states = sf::priv::retrieveStates(activity); - sf::Lock lock(states->mutex); + std::lock_guard lock(states->mutex); // Update the activity states states->window = window; @@ -350,7 +350,7 @@ static void onNativeWindowCreated(ANativeActivity* activity, ANativeWindow* wind static void onNativeWindowDestroyed(ANativeActivity* activity, ANativeWindow* window) { sf::priv::ActivityStates* states = sf::priv::retrieveStates(activity); - sf::Lock lock(states->mutex); + std::lock_guard lock(states->mutex); // Update the activity states states->window = NULL; @@ -391,7 +391,7 @@ static void onInputQueueCreated(ANativeActivity* activity, AInputQueue* queue) // Attach the input queue { - sf::Lock lock(states->mutex); + std::lock_guard lock(states->mutex); AInputQueue_attachLooper(queue, states->looper, 1, states->processEvent, NULL); states->inputQueue = queue; @@ -407,7 +407,7 @@ static void onInputQueueDestroyed(ANativeActivity* activity, AInputQueue* queue) // Detach the input queue { - sf::Lock lock(states->mutex); + std::lock_guard lock(states->mutex); states->inputQueue = NULL; AInputQueue_detachLooper(queue); @@ -426,7 +426,7 @@ static void onContentRectChanged(ANativeActivity* activity, const ARect* rect) { // Retrieve our activity states from the activity instance sf::priv::ActivityStates* states = sf::priv::retrieveStates(activity); - sf::Lock lock(states->mutex); + std::lock_guard lock(states->mutex); // Make sure the window still exists before we access the dimensions on it if (states->window != NULL) { @@ -535,8 +535,10 @@ void ANativeActivity_onCreate(ANativeActivity* activity, void* savedState, size_ sf::err().rdbuf(&states->logcat); // Launch the main thread - sf::Thread* thread = new sf::Thread(sf::priv::main, states); - thread->launch(); + auto thread = new std::thread([states] + { + sf::priv::main(states); + }); // Wait for the main thread to be initialized states->mutex.lock(); diff --git a/src/SFML/System/Android/Activity.hpp b/src/SFML/System/Android/Activity.hpp index bd6bbc9a..772ab70e 100644 --- a/src/SFML/System/Android/Activity.hpp +++ b/src/SFML/System/Android/Activity.hpp @@ -30,7 +30,6 @@ //////////////////////////////////////////////////////////// #include #include -#include #include #include #include @@ -38,6 +37,7 @@ #include #include #include +#include class SFML_SYSTEM_API LogcatStream : public std::streambuf { @@ -69,7 +69,7 @@ struct ActivityStates void* savedState; size_t savedStateSize; - Mutex mutex; + std::mutex mutex; void (*forwardEvent)(const Event& event); int (*processEvent)(int fd, int events, void* data); diff --git a/src/SFML/System/Android/ResourceStream.cpp b/src/SFML/System/Android/ResourceStream.cpp index 93280aa6..b6f2b390 100644 --- a/src/SFML/System/Android/ResourceStream.cpp +++ b/src/SFML/System/Android/ResourceStream.cpp @@ -28,7 +28,7 @@ //////////////////////////////////////////////////////////// #include #include -#include +#include namespace sf @@ -41,7 +41,7 @@ ResourceStream::ResourceStream(const std::string& filename) : m_file (NULL) { ActivityStates* states = getActivity(NULL); - Lock(states->mutex); + std::lock_guard lock(states->mutex); m_file = AAssetManager_open(states->activity->assetManager, filename.c_str(), AASSET_MODE_UNKNOWN); } diff --git a/src/SFML/System/CMakeLists.txt b/src/SFML/System/CMakeLists.txt index 54da9a07..dbac1081 100644 --- a/src/SFML/System/CMakeLists.txt +++ b/src/SFML/System/CMakeLists.txt @@ -10,10 +10,6 @@ set(SRC ${INCROOT}/Err.hpp ${INCROOT}/Export.hpp ${INCROOT}/InputStream.hpp - ${SRCROOT}/Lock.cpp - ${INCROOT}/Lock.hpp - ${SRCROOT}/Mutex.cpp - ${INCROOT}/Mutex.hpp ${INCROOT}/NativeActivity.hpp ${INCROOT}/NonCopyable.hpp ${SRCROOT}/Sleep.cpp @@ -21,13 +17,6 @@ set(SRC ${SRCROOT}/String.cpp ${INCROOT}/String.hpp ${INCROOT}/String.inl - ${SRCROOT}/Thread.cpp - ${INCROOT}/Thread.hpp - ${INCROOT}/Thread.inl - ${SRCROOT}/ThreadLocal.cpp - ${INCROOT}/ThreadLocal.hpp - ${INCROOT}/ThreadLocalPtr.hpp - ${INCROOT}/ThreadLocalPtr.inl ${SRCROOT}/Time.cpp ${INCROOT}/Time.hpp ${INCROOT}/Utf.hpp @@ -48,28 +37,16 @@ if(SFML_OS_WINDOWS) set(PLATFORM_SRC ${SRCROOT}/Win32/ClockImpl.cpp ${SRCROOT}/Win32/ClockImpl.hpp - ${SRCROOT}/Win32/MutexImpl.cpp - ${SRCROOT}/Win32/MutexImpl.hpp ${SRCROOT}/Win32/SleepImpl.cpp ${SRCROOT}/Win32/SleepImpl.hpp - ${SRCROOT}/Win32/ThreadImpl.cpp - ${SRCROOT}/Win32/ThreadImpl.hpp - ${SRCROOT}/Win32/ThreadLocalImpl.cpp - ${SRCROOT}/Win32/ThreadLocalImpl.hpp ) source_group("windows" FILES ${PLATFORM_SRC}) else() set(PLATFORM_SRC ${SRCROOT}/Unix/ClockImpl.cpp ${SRCROOT}/Unix/ClockImpl.hpp - ${SRCROOT}/Unix/MutexImpl.cpp - ${SRCROOT}/Unix/MutexImpl.hpp ${SRCROOT}/Unix/SleepImpl.cpp ${SRCROOT}/Unix/SleepImpl.hpp - ${SRCROOT}/Unix/ThreadImpl.cpp - ${SRCROOT}/Unix/ThreadImpl.hpp - ${SRCROOT}/Unix/ThreadLocalImpl.cpp - ${SRCROOT}/Unix/ThreadLocalImpl.hpp ) if(SFML_OS_ANDROID) diff --git a/src/SFML/System/Lock.cpp b/src/SFML/System/Lock.cpp deleted file mode 100644 index 4a371271..00000000 --- a/src/SFML/System/Lock.cpp +++ /dev/null @@ -1,48 +0,0 @@ -//////////////////////////////////////////////////////////// -// -// SFML - Simple and Fast Multimedia Library -// Copyright (C) 2007-2017 Laurent Gomila (laurent@sfml-dev.org) -// -// This software is provided 'as-is', without any express or implied warranty. -// In no event will the authors be held liable for any damages arising from the use of this software. -// -// Permission is granted to anyone to use this software for any purpose, -// including commercial applications, and to alter it and redistribute it freely, -// subject to the following restrictions: -// -// 1. The origin of this software must not be misrepresented; -// you must not claim that you wrote the original software. -// If you use this software in a product, an acknowledgment -// in the product documentation would be appreciated but is not required. -// -// 2. Altered source versions must be plainly marked as such, -// and must not be misrepresented as being the original software. -// -// 3. This notice may not be removed or altered from any source distribution. -// -//////////////////////////////////////////////////////////// - -//////////////////////////////////////////////////////////// -// Headers -//////////////////////////////////////////////////////////// -#include -#include - - -namespace sf -{ -//////////////////////////////////////////////////////////// -Lock::Lock(Mutex& mutex) : -m_mutex(mutex) -{ - m_mutex.lock(); -} - - -//////////////////////////////////////////////////////////// -Lock::~Lock() -{ - m_mutex.unlock(); -} - -} // namespace sf diff --git a/src/SFML/System/Mutex.cpp b/src/SFML/System/Mutex.cpp deleted file mode 100644 index e204ae04..00000000 --- a/src/SFML/System/Mutex.cpp +++ /dev/null @@ -1,63 +0,0 @@ -//////////////////////////////////////////////////////////// -// -// SFML - Simple and Fast Multimedia Library -// Copyright (C) 2007-2017 Laurent Gomila (laurent@sfml-dev.org) -// -// This software is provided 'as-is', without any express or implied warranty. -// In no event will the authors be held liable for any damages arising from the use of this software. -// -// Permission is granted to anyone to use this software for any purpose, -// including commercial applications, and to alter it and redistribute it freely, -// subject to the following restrictions: -// -// 1. The origin of this software must not be misrepresented; -// you must not claim that you wrote the original software. -// If you use this software in a product, an acknowledgment -// in the product documentation would be appreciated but is not required. -// -// 2. Altered source versions must be plainly marked as such, -// and must not be misrepresented as being the original software. -// -// 3. This notice may not be removed or altered from any source distribution. -// -//////////////////////////////////////////////////////////// - -//////////////////////////////////////////////////////////// -// Headers -//////////////////////////////////////////////////////////// -#include - -#if defined(SFML_SYSTEM_WINDOWS) - #include -#else - #include -#endif - - -namespace sf -{ -//////////////////////////////////////////////////////////// -Mutex::Mutex() -{ - m_mutexImpl = std::make_shared(); -} - - -//////////////////////////////////////////////////////////// -Mutex::~Mutex() = default; - - -//////////////////////////////////////////////////////////// -void Mutex::lock() -{ - m_mutexImpl->lock(); -} - - -//////////////////////////////////////////////////////////// -void Mutex::unlock() -{ - m_mutexImpl->unlock(); -} - -} // namespace sf diff --git a/src/SFML/System/Thread.cpp b/src/SFML/System/Thread.cpp deleted file mode 100644 index c708fdc0..00000000 --- a/src/SFML/System/Thread.cpp +++ /dev/null @@ -1,83 +0,0 @@ -//////////////////////////////////////////////////////////// -// -// SFML - Simple and Fast Multimedia Library -// Copyright (C) 2007-2017 Laurent Gomila (laurent@sfml-dev.org) -// -// This software is provided 'as-is', without any express or implied warranty. -// In no event will the authors be held liable for any damages arising from the use of this software. -// -// Permission is granted to anyone to use this software for any purpose, -// including commercial applications, and to alter it and redistribute it freely, -// subject to the following restrictions: -// -// 1. The origin of this software must not be misrepresented; -// you must not claim that you wrote the original software. -// If you use this software in a product, an acknowledgment -// in the product documentation would be appreciated but is not required. -// -// 2. Altered source versions must be plainly marked as such, -// and must not be misrepresented as being the original software. -// -// 3. This notice may not be removed or altered from any source distribution. -// -//////////////////////////////////////////////////////////// - -//////////////////////////////////////////////////////////// -// Headers -//////////////////////////////////////////////////////////// -#include - - -#if defined(SFML_SYSTEM_WINDOWS) - #include -#else - #include -#endif - - -namespace sf -{ -//////////////////////////////////////////////////////////// -Thread::~Thread() -{ - wait(); -} - - -//////////////////////////////////////////////////////////// -void Thread::launch() -{ - wait(); - m_impl = std::make_shared(this); -} - - -//////////////////////////////////////////////////////////// -void Thread::wait() -{ - if (m_impl) - { - m_impl->wait(); - m_impl.reset(); - } -} - - -//////////////////////////////////////////////////////////// -void Thread::terminate() -{ - if (m_impl) - { - m_impl->terminate(); - m_impl.reset(); - } -} - - -//////////////////////////////////////////////////////////// -void Thread::run() -{ - m_entryPoint->run(); -} - -} // namespace sf diff --git a/src/SFML/System/ThreadLocal.cpp b/src/SFML/System/ThreadLocal.cpp deleted file mode 100644 index 6a3cc8c7..00000000 --- a/src/SFML/System/ThreadLocal.cpp +++ /dev/null @@ -1,64 +0,0 @@ -//////////////////////////////////////////////////////////// -// -// SFML - Simple and Fast Multimedia Library -// Copyright (C) 2007-2017 Laurent Gomila (laurent@sfml-dev.org) -// -// This software is provided 'as-is', without any express or implied warranty. -// In no event will the authors be held liable for any damages arising from the use of this software. -// -// Permission is granted to anyone to use this software for any purpose, -// including commercial applications, and to alter it and redistribute it freely, -// subject to the following restrictions: -// -// 1. The origin of this software must not be misrepresented; -// you must not claim that you wrote the original software. -// If you use this software in a product, an acknowledgment -// in the product documentation would be appreciated but is not required. -// -// 2. Altered source versions must be plainly marked as such, -// and must not be misrepresented as being the original software. -// -// 3. This notice may not be removed or altered from any source distribution. -// -//////////////////////////////////////////////////////////// - -//////////////////////////////////////////////////////////// -// Headers -//////////////////////////////////////////////////////////// -#include - -#if defined(SFML_SYSTEM_WINDOWS) - #include -#else - #include -#endif - - -namespace sf -{ -//////////////////////////////////////////////////////////// -ThreadLocal::ThreadLocal(void* value) -{ - m_impl = std::make_unique(); - setValue(value); -} - - -//////////////////////////////////////////////////////////// -ThreadLocal::~ThreadLocal() = default; - - -//////////////////////////////////////////////////////////// -void ThreadLocal::setValue(void* value) -{ - m_impl->setValue(value); -} - - -//////////////////////////////////////////////////////////// -void* ThreadLocal::getValue() const -{ - return m_impl->getValue(); -} - -} // namespace sf diff --git a/src/SFML/System/Unix/MutexImpl.cpp b/src/SFML/System/Unix/MutexImpl.cpp deleted file mode 100644 index 2ab46c0e..00000000 --- a/src/SFML/System/Unix/MutexImpl.cpp +++ /dev/null @@ -1,69 +0,0 @@ -//////////////////////////////////////////////////////////// -// -// SFML - Simple and Fast Multimedia Library -// Copyright (C) 2007-2017 Laurent Gomila (laurent@sfml-dev.org) -// -// This software is provided 'as-is', without any express or implied warranty. -// In no event will the authors be held liable for any damages arising from the use of this software. -// -// Permission is granted to anyone to use this software for any purpose, -// including commercial applications, and to alter it and redistribute it freely, -// subject to the following restrictions: -// -// 1. The origin of this software must not be misrepresented; -// you must not claim that you wrote the original software. -// If you use this software in a product, an acknowledgment -// in the product documentation would be appreciated but is not required. -// -// 2. Altered source versions must be plainly marked as such, -// and must not be misrepresented as being the original software. -// -// 3. This notice may not be removed or altered from any source distribution. -// -//////////////////////////////////////////////////////////// - -//////////////////////////////////////////////////////////// -// Headers -//////////////////////////////////////////////////////////// -#include - - -namespace sf -{ -namespace priv -{ -//////////////////////////////////////////////////////////// -MutexImpl::MutexImpl() -{ - // Make it recursive to follow the expected behavior - pthread_mutexattr_t attributes; - pthread_mutexattr_init(&attributes); - pthread_mutexattr_settype(&attributes, PTHREAD_MUTEX_RECURSIVE); - - pthread_mutex_init(&m_mutex, &attributes); -} - - -//////////////////////////////////////////////////////////// -MutexImpl::~MutexImpl() -{ - pthread_mutex_destroy(&m_mutex); -} - - -//////////////////////////////////////////////////////////// -void MutexImpl::lock() -{ - pthread_mutex_lock(&m_mutex); -} - - -//////////////////////////////////////////////////////////// -void MutexImpl::unlock() -{ - pthread_mutex_unlock(&m_mutex); -} - -} // namespace priv - -} // namespace sf diff --git a/src/SFML/System/Unix/MutexImpl.hpp b/src/SFML/System/Unix/MutexImpl.hpp deleted file mode 100644 index ea4c838f..00000000 --- a/src/SFML/System/Unix/MutexImpl.hpp +++ /dev/null @@ -1,83 +0,0 @@ -//////////////////////////////////////////////////////////// -// -// SFML - Simple and Fast Multimedia Library -// Copyright (C) 2007-2017 Laurent Gomila (laurent@sfml-dev.org) -// -// This software is provided 'as-is', without any express or implied warranty. -// In no event will the authors be held liable for any damages arising from the use of this software. -// -// Permission is granted to anyone to use this software for any purpose, -// including commercial applications, and to alter it and redistribute it freely, -// subject to the following restrictions: -// -// 1. The origin of this software must not be misrepresented; -// you must not claim that you wrote the original software. -// If you use this software in a product, an acknowledgment -// in the product documentation would be appreciated but is not required. -// -// 2. Altered source versions must be plainly marked as such, -// and must not be misrepresented as being the original software. -// -// 3. This notice may not be removed or altered from any source distribution. -// -//////////////////////////////////////////////////////////// - -#ifndef SFML_MUTEXIMPL_HPP -#define SFML_MUTEXIMPL_HPP - -//////////////////////////////////////////////////////////// -// Headers -//////////////////////////////////////////////////////////// -#include -#include - - -namespace sf -{ -namespace priv -{ -//////////////////////////////////////////////////////////// -/// \brief Unix implementation of mutexes -//////////////////////////////////////////////////////////// -class MutexImpl : NonCopyable -{ -public: - - //////////////////////////////////////////////////////////// - /// \brief Default constructor - /// - //////////////////////////////////////////////////////////// - MutexImpl(); - - //////////////////////////////////////////////////////////// - /// \brief Destructor - /// - //////////////////////////////////////////////////////////// - ~MutexImpl(); - - //////////////////////////////////////////////////////////// - /// \brief Lock the mutex - /// - //////////////////////////////////////////////////////////// - void lock(); - - //////////////////////////////////////////////////////////// - /// \brief Unlock the mutex - /// - //////////////////////////////////////////////////////////// - void unlock(); - -private: - - //////////////////////////////////////////////////////////// - // Member data - //////////////////////////////////////////////////////////// - pthread_mutex_t m_mutex; ///< pthread handle of the mutex -}; - -} // namespace priv - -} // namespace sf - - -#endif // SFML_MUTEXIMPL_HPP diff --git a/src/SFML/System/Unix/ThreadImpl.cpp b/src/SFML/System/Unix/ThreadImpl.cpp deleted file mode 100644 index b6dd383c..00000000 --- a/src/SFML/System/Unix/ThreadImpl.cpp +++ /dev/null @@ -1,94 +0,0 @@ -//////////////////////////////////////////////////////////// -// -// SFML - Simple and Fast Multimedia Library -// Copyright (C) 2007-2017 Laurent Gomila (laurent@sfml-dev.org) -// -// This software is provided 'as-is', without any express or implied warranty. -// In no event will the authors be held liable for any damages arising from the use of this software. -// -// Permission is granted to anyone to use this software for any purpose, -// including commercial applications, and to alter it and redistribute it freely, -// subject to the following restrictions: -// -// 1. The origin of this software must not be misrepresented; -// you must not claim that you wrote the original software. -// If you use this software in a product, an acknowledgment -// in the product documentation would be appreciated but is not required. -// -// 2. Altered source versions must be plainly marked as such, -// and must not be misrepresented as being the original software. -// -// 3. This notice may not be removed or altered from any source distribution. -// -//////////////////////////////////////////////////////////// - -//////////////////////////////////////////////////////////// -// Headers -//////////////////////////////////////////////////////////// -#include -#include -#include -#include - - -namespace sf -{ -namespace priv -{ -//////////////////////////////////////////////////////////// -ThreadImpl::ThreadImpl(Thread* owner) : -m_isActive(true) -{ - m_isActive = pthread_create(&m_thread, NULL, &ThreadImpl::entryPoint, owner) == 0; - - if (!m_isActive) - std::cerr << "Failed to create thread" << std::endl; -} - - -//////////////////////////////////////////////////////////// -void ThreadImpl::wait() -{ - if (m_isActive) - { - assert(pthread_equal(pthread_self(), m_thread) == 0); // A thread cannot wait for itself! - pthread_join(m_thread, NULL); - } -} - - -//////////////////////////////////////////////////////////// -void ThreadImpl::terminate() -{ - if (m_isActive) - { - #ifndef SFML_SYSTEM_ANDROID - pthread_cancel(m_thread); - #else - // See http://stackoverflow.com/questions/4610086/pthread-cancel-al - pthread_kill(m_thread, SIGUSR1); - #endif - } -} - - -//////////////////////////////////////////////////////////// -void* ThreadImpl::entryPoint(void* userData) -{ - // The Thread instance is stored in the user data - Thread* owner = static_cast(userData); - - #ifndef SFML_SYSTEM_ANDROID - // Tell the thread to handle cancel requests immediately - pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); - #endif - - // Forward to the owner - owner->run(); - - return NULL; -} - -} // namespace priv - -} // namespace sf diff --git a/src/SFML/System/Unix/ThreadImpl.hpp b/src/SFML/System/Unix/ThreadImpl.hpp deleted file mode 100644 index 0191d5e2..00000000 --- a/src/SFML/System/Unix/ThreadImpl.hpp +++ /dev/null @@ -1,93 +0,0 @@ -//////////////////////////////////////////////////////////// -// -// SFML - Simple and Fast Multimedia Library -// Copyright (C) 2007-2017 Laurent Gomila (laurent@sfml-dev.org) -// -// This software is provided 'as-is', without any express or implied warranty. -// In no event will the authors be held liable for any damages arising from the use of this software. -// -// Permission is granted to anyone to use this software for any purpose, -// including commercial applications, and to alter it and redistribute it freely, -// subject to the following restrictions: -// -// 1. The origin of this software must not be misrepresented; -// you must not claim that you wrote the original software. -// If you use this software in a product, an acknowledgment -// in the product documentation would be appreciated but is not required. -// -// 2. Altered source versions must be plainly marked as such, -// and must not be misrepresented as being the original software. -// -// 3. This notice may not be removed or altered from any source distribution. -// -//////////////////////////////////////////////////////////// - -#ifndef SFML_THREADIMPL_HPP -#define SFML_THREADIMPL_HPP - -//////////////////////////////////////////////////////////// -// Headers -//////////////////////////////////////////////////////////// -#include -#include -#include - - -namespace sf -{ -class Thread; - -namespace priv -{ -//////////////////////////////////////////////////////////// -/// \brief Unix implementation of threads -//////////////////////////////////////////////////////////// -class ThreadImpl : NonCopyable -{ -public: - - //////////////////////////////////////////////////////////// - /// \brief Default constructor, launch the thread - /// - /// \param owner The Thread instance to run - /// - //////////////////////////////////////////////////////////// - ThreadImpl(Thread* owner); - - //////////////////////////////////////////////////////////// - /// \brief Wait until the thread finishes - /// - //////////////////////////////////////////////////////////// - void wait(); - - //////////////////////////////////////////////////////////// - /// \brief Terminate the thread - /// - //////////////////////////////////////////////////////////// - void terminate(); - -private: - - //////////////////////////////////////////////////////////// - /// \brief Global entry point for all threads - /// - /// \param userData User-defined data (contains the Thread instance) - /// - /// \return Os specific error code - /// - //////////////////////////////////////////////////////////// - static void* entryPoint(void* userData); - - //////////////////////////////////////////////////////////// - // Member data - //////////////////////////////////////////////////////////// - pthread_t m_thread; ///< pthread thread instance - bool m_isActive; ///< Thread state (active or inactive) -}; - -} // namespace priv - -} // namespace sf - - -#endif // SFML_THREADIMPL_HPP diff --git a/src/SFML/System/Unix/ThreadLocalImpl.cpp b/src/SFML/System/Unix/ThreadLocalImpl.cpp deleted file mode 100644 index a0a157e2..00000000 --- a/src/SFML/System/Unix/ThreadLocalImpl.cpp +++ /dev/null @@ -1,65 +0,0 @@ -//////////////////////////////////////////////////////////// -// -// SFML - Simple and Fast Multimedia Library -// Copyright (C) 2007-2017 Laurent Gomila (laurent@sfml-dev.org) -// -// This software is provided 'as-is', without any express or implied warranty. -// In no event will the authors be held liable for any damages arising from the use of this software. -// -// Permission is granted to anyone to use this software for any purpose, -// including commercial applications, and to alter it and redistribute it freely, -// subject to the following restrictions: -// -// 1. The origin of this software must not be misrepresented; -// you must not claim that you wrote the original software. -// If you use this software in a product, an acknowledgment -// in the product documentation would be appreciated but is not required. -// -// 2. Altered source versions must be plainly marked as such, -// and must not be misrepresented as being the original software. -// -// 3. This notice may not be removed or altered from any source distribution. -// -//////////////////////////////////////////////////////////// - -//////////////////////////////////////////////////////////// -// Headers -//////////////////////////////////////////////////////////// -#include - - -namespace sf -{ -namespace priv -{ -//////////////////////////////////////////////////////////// -ThreadLocalImpl::ThreadLocalImpl() : -m_key(0) -{ - pthread_key_create(&m_key, NULL); -} - - -//////////////////////////////////////////////////////////// -ThreadLocalImpl::~ThreadLocalImpl() -{ - pthread_key_delete(m_key); -} - - -//////////////////////////////////////////////////////////// -void ThreadLocalImpl::setValue(void* value) -{ - pthread_setspecific(m_key, value); -} - - -//////////////////////////////////////////////////////////// -void* ThreadLocalImpl::getValue() const -{ - return pthread_getspecific(m_key); -} - -} // namespace priv - -} // namespace sf diff --git a/src/SFML/System/Unix/ThreadLocalImpl.hpp b/src/SFML/System/Unix/ThreadLocalImpl.hpp deleted file mode 100644 index b72ce65f..00000000 --- a/src/SFML/System/Unix/ThreadLocalImpl.hpp +++ /dev/null @@ -1,87 +0,0 @@ -//////////////////////////////////////////////////////////// -// -// SFML - Simple and Fast Multimedia Library -// Copyright (C) 2007-2017 Laurent Gomila (laurent@sfml-dev.org) -// -// This software is provided 'as-is', without any express or implied warranty. -// In no event will the authors be held liable for any damages arising from the use of this software. -// -// Permission is granted to anyone to use this software for any purpose, -// including commercial applications, and to alter it and redistribute it freely, -// subject to the following restrictions: -// -// 1. The origin of this software must not be misrepresented; -// you must not claim that you wrote the original software. -// If you use this software in a product, an acknowledgment -// in the product documentation would be appreciated but is not required. -// -// 2. Altered source versions must be plainly marked as such, -// and must not be misrepresented as being the original software. -// -// 3. This notice may not be removed or altered from any source distribution. -// -//////////////////////////////////////////////////////////// - -#ifndef SFML_THREADLOCALIMPL_HPP -#define SFML_THREADLOCALIMPL_HPP - -//////////////////////////////////////////////////////////// -// Headers -//////////////////////////////////////////////////////////// -#include -#include - - -namespace sf -{ -namespace priv -{ -//////////////////////////////////////////////////////////// -/// \brief Unix implementation of thread-local storage -//////////////////////////////////////////////////////////// -class ThreadLocalImpl : NonCopyable -{ -public: - - //////////////////////////////////////////////////////////// - /// \brief Default constructor -- allocate the storage - /// - //////////////////////////////////////////////////////////// - ThreadLocalImpl(); - - //////////////////////////////////////////////////////////// - /// \brief Destructor -- free the storage - /// - //////////////////////////////////////////////////////////// - ~ThreadLocalImpl(); - - //////////////////////////////////////////////////////////// - /// \brief Set the thread-specific value of the variable - /// - /// \param value Value of the variable for this thread - /// - //////////////////////////////////////////////////////////// - void setValue(void* value); - - //////////////////////////////////////////////////////////// - /// \brief Retrieve the thread-specific value of the variable - /// - /// \return Value of the variable for this thread - /// - //////////////////////////////////////////////////////////// - void* getValue() const; - -private: - - //////////////////////////////////////////////////////////// - // Member data - //////////////////////////////////////////////////////////// - pthread_key_t m_key; ///< Index of our thread-local storage slot -}; - -} // namespace priv - -} // namespace sf - - -#endif // SFML_THREADLOCALIMPL_HPP diff --git a/src/SFML/System/Win32/ClockImpl.cpp b/src/SFML/System/Win32/ClockImpl.cpp index c81288d2..b41f4e76 100644 --- a/src/SFML/System/Win32/ClockImpl.cpp +++ b/src/SFML/System/Win32/ClockImpl.cpp @@ -26,9 +26,8 @@ // Headers //////////////////////////////////////////////////////////// #include -#include -#include #include +#include namespace sf { @@ -57,10 +56,10 @@ Time ClockImpl::getCurrentTime() if (oldWindows) { - static sf::Mutex oldWindowsMutex; + static std::mutex oldWindowsMutex; - // Acquire a lock (CRITICAL_SECTION) to prevent travelling back in time - Lock lock(oldWindowsMutex); + // Acquire a lock to prevent travelling back in time + std::lock_guard lock(oldWindowsMutex); // Get the current time QueryPerformanceCounter(&time); diff --git a/src/SFML/System/Win32/MutexImpl.cpp b/src/SFML/System/Win32/MutexImpl.cpp deleted file mode 100644 index 7b47cd51..00000000 --- a/src/SFML/System/Win32/MutexImpl.cpp +++ /dev/null @@ -1,64 +0,0 @@ -//////////////////////////////////////////////////////////// -// -// SFML - Simple and Fast Multimedia Library -// Copyright (C) 2007-2017 Laurent Gomila (laurent@sfml-dev.org) -// -// This software is provided 'as-is', without any express or implied warranty. -// In no event will the authors be held liable for any damages arising from the use of this software. -// -// Permission is granted to anyone to use this software for any purpose, -// including commercial applications, and to alter it and redistribute it freely, -// subject to the following restrictions: -// -// 1. The origin of this software must not be misrepresented; -// you must not claim that you wrote the original software. -// If you use this software in a product, an acknowledgment -// in the product documentation would be appreciated but is not required. -// -// 2. Altered source versions must be plainly marked as such, -// and must not be misrepresented as being the original software. -// -// 3. This notice may not be removed or altered from any source distribution. -// -//////////////////////////////////////////////////////////// - -//////////////////////////////////////////////////////////// -// Headers -//////////////////////////////////////////////////////////// -#include - - -namespace sf -{ -namespace priv -{ -//////////////////////////////////////////////////////////// -MutexImpl::MutexImpl() -{ - InitializeCriticalSection(&m_mutex); -} - - -//////////////////////////////////////////////////////////// -MutexImpl::~MutexImpl() -{ - DeleteCriticalSection(&m_mutex); -} - - -//////////////////////////////////////////////////////////// -void MutexImpl::lock() -{ - EnterCriticalSection(&m_mutex); -} - - -//////////////////////////////////////////////////////////// -void MutexImpl::unlock() -{ - LeaveCriticalSection(&m_mutex); -} - -} // namespace priv - -} // namespace sf diff --git a/src/SFML/System/Win32/MutexImpl.hpp b/src/SFML/System/Win32/MutexImpl.hpp deleted file mode 100644 index c7174e8b..00000000 --- a/src/SFML/System/Win32/MutexImpl.hpp +++ /dev/null @@ -1,83 +0,0 @@ -//////////////////////////////////////////////////////////// -// -// SFML - Simple and Fast Multimedia Library -// Copyright (C) 2007-2017 Laurent Gomila (laurent@sfml-dev.org) -// -// This software is provided 'as-is', without any express or implied warranty. -// In no event will the authors be held liable for any damages arising from the use of this software. -// -// Permission is granted to anyone to use this software for any purpose, -// including commercial applications, and to alter it and redistribute it freely, -// subject to the following restrictions: -// -// 1. The origin of this software must not be misrepresented; -// you must not claim that you wrote the original software. -// If you use this software in a product, an acknowledgment -// in the product documentation would be appreciated but is not required. -// -// 2. Altered source versions must be plainly marked as such, -// and must not be misrepresented as being the original software. -// -// 3. This notice may not be removed or altered from any source distribution. -// -//////////////////////////////////////////////////////////// - -#ifndef SFML_MUTEXIMPL_HPP -#define SFML_MUTEXIMPL_HPP - -//////////////////////////////////////////////////////////// -// Headers -//////////////////////////////////////////////////////////// -#include -#include - - -namespace sf -{ -namespace priv -{ -//////////////////////////////////////////////////////////// -/// \brief Windows implementation of mutexes -//////////////////////////////////////////////////////////// -class MutexImpl : NonCopyable -{ -public: - - //////////////////////////////////////////////////////////// - /// \brief Default constructor - /// - //////////////////////////////////////////////////////////// - MutexImpl(); - - //////////////////////////////////////////////////////////// - /// \brief Destructor - /// - //////////////////////////////////////////////////////////// - ~MutexImpl(); - - //////////////////////////////////////////////////////////// - /// \brief Lock the mutex - /// - //////////////////////////////////////////////////////////// - void lock(); - - //////////////////////////////////////////////////////////// - /// \brief Unlock the mutex - /// - //////////////////////////////////////////////////////////// - void unlock(); - -private: - - //////////////////////////////////////////////////////////// - // Member data - //////////////////////////////////////////////////////////// - CRITICAL_SECTION m_mutex; ///< Win32 handle of the mutex -}; - -} // namespace priv - -} // namespace sf - - -#endif // SFML_MUTEXIMPL_HPP diff --git a/src/SFML/System/Win32/ThreadImpl.cpp b/src/SFML/System/Win32/ThreadImpl.cpp deleted file mode 100644 index d34950ab..00000000 --- a/src/SFML/System/Win32/ThreadImpl.cpp +++ /dev/null @@ -1,93 +0,0 @@ -//////////////////////////////////////////////////////////// -// -// SFML - Simple and Fast Multimedia Library -// Copyright (C) 2007-2017 Laurent Gomila (laurent@sfml-dev.org) -// -// This software is provided 'as-is', without any express or implied warranty. -// In no event will the authors be held liable for any damages arising from the use of this software. -// -// Permission is granted to anyone to use this software for any purpose, -// including commercial applications, and to alter it and redistribute it freely, -// subject to the following restrictions: -// -// 1. The origin of this software must not be misrepresented; -// you must not claim that you wrote the original software. -// If you use this software in a product, an acknowledgment -// in the product documentation would be appreciated but is not required. -// -// 2. Altered source versions must be plainly marked as such, -// and must not be misrepresented as being the original software. -// -// 3. This notice may not be removed or altered from any source distribution. -// -//////////////////////////////////////////////////////////// - -//////////////////////////////////////////////////////////// -// Headers -//////////////////////////////////////////////////////////// -#include -#include -#include -#include -#include - - -namespace sf -{ -namespace priv -{ -//////////////////////////////////////////////////////////// -ThreadImpl::ThreadImpl(Thread* owner) -{ - m_thread = reinterpret_cast(_beginthreadex(NULL, 0, &ThreadImpl::entryPoint, owner, 0, &m_threadId)); - - if (!m_thread) - err() << "Failed to create thread" << std::endl; -} - - -//////////////////////////////////////////////////////////// -ThreadImpl::~ThreadImpl() -{ - if (m_thread) - CloseHandle(m_thread); -} - - -//////////////////////////////////////////////////////////// -void ThreadImpl::wait() -{ - if (m_thread) - { - assert(m_threadId != GetCurrentThreadId()); // A thread cannot wait for itself! - WaitForSingleObject(m_thread, INFINITE); - } -} - - -//////////////////////////////////////////////////////////// -void ThreadImpl::terminate() -{ - if (m_thread) - TerminateThread(m_thread, 0); -} - - -//////////////////////////////////////////////////////////// -unsigned int __stdcall ThreadImpl::entryPoint(void* userData) -{ - // The Thread instance is stored in the user data - Thread* owner = static_cast(userData); - - // Forward to the owner - owner->run(); - - // Optional, but it is cleaner - _endthreadex(0); - - return 0; -} - -} // namespace priv - -} // namespace sf diff --git a/src/SFML/System/Win32/ThreadImpl.hpp b/src/SFML/System/Win32/ThreadImpl.hpp deleted file mode 100644 index cfd10399..00000000 --- a/src/SFML/System/Win32/ThreadImpl.hpp +++ /dev/null @@ -1,109 +0,0 @@ -//////////////////////////////////////////////////////////// -// -// SFML - Simple and Fast Multimedia Library -// Copyright (C) 2007-2017 Laurent Gomila (laurent@sfml-dev.org) -// -// This software is provided 'as-is', without any express or implied warranty. -// In no event will the authors be held liable for any damages arising from the use of this software. -// -// Permission is granted to anyone to use this software for any purpose, -// including commercial applications, and to alter it and redistribute it freely, -// subject to the following restrictions: -// -// 1. The origin of this software must not be misrepresented; -// you must not claim that you wrote the original software. -// If you use this software in a product, an acknowledgment -// in the product documentation would be appreciated but is not required. -// -// 2. Altered source versions must be plainly marked as such, -// and must not be misrepresented as being the original software. -// -// 3. This notice may not be removed or altered from any source distribution. -// -//////////////////////////////////////////////////////////// - -#ifndef SFML_THREADIMPL_HPP -#define SFML_THREADIMPL_HPP - -//////////////////////////////////////////////////////////// -// Headers -//////////////////////////////////////////////////////////// -#include -#include - -// Fix for unaligned stack with clang and GCC on Windows XP 32-bit -#if defined(SFML_SYSTEM_WINDOWS) && (defined(__clang__) || defined(__GNUC__)) - - #define ALIGN_STACK __attribute__((__force_align_arg_pointer__)) - -#else - - #define ALIGN_STACK - -#endif - - -namespace sf -{ -class Thread; - -namespace priv -{ -//////////////////////////////////////////////////////////// -/// \brief Windows implementation of threads -//////////////////////////////////////////////////////////// -class ThreadImpl : NonCopyable -{ -public: - - //////////////////////////////////////////////////////////// - /// \brief Default constructor, launch the thread - /// - /// \param owner The Thread instance to run - /// - //////////////////////////////////////////////////////////// - ThreadImpl(Thread* owner); - - //////////////////////////////////////////////////////////// - /// \brief Destructor - /// - //////////////////////////////////////////////////////////// - ~ThreadImpl(); - - //////////////////////////////////////////////////////////// - /// \brief Wait until the thread finishes - /// - //////////////////////////////////////////////////////////// - void wait(); - - //////////////////////////////////////////////////////////// - /// \brief Terminate the thread - /// - //////////////////////////////////////////////////////////// - void terminate(); - -private: - - //////////////////////////////////////////////////////////// - /// \brief Global entry point for all threads - /// - /// \param userData User-defined data (contains the Thread instance) - /// - /// \return OS specific error code - /// - //////////////////////////////////////////////////////////// - ALIGN_STACK static unsigned int __stdcall entryPoint(void* userData); - - //////////////////////////////////////////////////////////// - // Member data - //////////////////////////////////////////////////////////// - HANDLE m_thread; ///< Win32 thread handle - unsigned int m_threadId; ///< Win32 thread identifier -}; - -} // namespace priv - -} // namespace sf - - -#endif // SFML_THREADIMPL_HPP diff --git a/src/SFML/System/Win32/ThreadLocalImpl.cpp b/src/SFML/System/Win32/ThreadLocalImpl.cpp deleted file mode 100644 index 221fffe9..00000000 --- a/src/SFML/System/Win32/ThreadLocalImpl.cpp +++ /dev/null @@ -1,64 +0,0 @@ -//////////////////////////////////////////////////////////// -// -// SFML - Simple and Fast Multimedia Library -// Copyright (C) 2007-2017 Laurent Gomila (laurent@sfml-dev.org) -// -// This software is provided 'as-is', without any express or implied warranty. -// In no event will the authors be held liable for any damages arising from the use of this software. -// -// Permission is granted to anyone to use this software for any purpose, -// including commercial applications, and to alter it and redistribute it freely, -// subject to the following restrictions: -// -// 1. The origin of this software must not be misrepresented; -// you must not claim that you wrote the original software. -// If you use this software in a product, an acknowledgment -// in the product documentation would be appreciated but is not required. -// -// 2. Altered source versions must be plainly marked as such, -// and must not be misrepresented as being the original software. -// -// 3. This notice may not be removed or altered from any source distribution. -// -//////////////////////////////////////////////////////////// - -//////////////////////////////////////////////////////////// -// Headers -//////////////////////////////////////////////////////////// -#include - - -namespace sf -{ -namespace priv -{ -//////////////////////////////////////////////////////////// -ThreadLocalImpl::ThreadLocalImpl() -{ - m_index = TlsAlloc(); -} - - -//////////////////////////////////////////////////////////// -ThreadLocalImpl::~ThreadLocalImpl() -{ - TlsFree(m_index); -} - - -//////////////////////////////////////////////////////////// -void ThreadLocalImpl::setValue(void* value) -{ - TlsSetValue(m_index, value); -} - - -//////////////////////////////////////////////////////////// -void* ThreadLocalImpl::getValue() const -{ - return TlsGetValue(m_index); -} - -} // namespace priv - -} // namespace sf diff --git a/src/SFML/System/Win32/ThreadLocalImpl.hpp b/src/SFML/System/Win32/ThreadLocalImpl.hpp deleted file mode 100644 index 458b28a5..00000000 --- a/src/SFML/System/Win32/ThreadLocalImpl.hpp +++ /dev/null @@ -1,87 +0,0 @@ -//////////////////////////////////////////////////////////// -// -// SFML - Simple and Fast Multimedia Library -// Copyright (C) 2007-2017 Laurent Gomila (laurent@sfml-dev.org) -// -// This software is provided 'as-is', without any express or implied warranty. -// In no event will the authors be held liable for any damages arising from the use of this software. -// -// Permission is granted to anyone to use this software for any purpose, -// including commercial applications, and to alter it and redistribute it freely, -// subject to the following restrictions: -// -// 1. The origin of this software must not be misrepresented; -// you must not claim that you wrote the original software. -// If you use this software in a product, an acknowledgment -// in the product documentation would be appreciated but is not required. -// -// 2. Altered source versions must be plainly marked as such, -// and must not be misrepresented as being the original software. -// -// 3. This notice may not be removed or altered from any source distribution. -// -//////////////////////////////////////////////////////////// - -#ifndef SFML_THREADLOCALIMPL_HPP -#define SFML_THREADLOCALIMPL_HPP - -//////////////////////////////////////////////////////////// -// Headers -//////////////////////////////////////////////////////////// -#include -#include - - -namespace sf -{ -namespace priv -{ -//////////////////////////////////////////////////////////// -/// \brief Windows implementation of thread-local storage -//////////////////////////////////////////////////////////// -class ThreadLocalImpl : NonCopyable -{ -public: - - //////////////////////////////////////////////////////////// - /// \brief Default constructor -- allocate the storage - /// - //////////////////////////////////////////////////////////// - ThreadLocalImpl(); - - //////////////////////////////////////////////////////////// - /// \brief Destructor -- free the storage - /// - //////////////////////////////////////////////////////////// - ~ThreadLocalImpl(); - - //////////////////////////////////////////////////////////// - /// \brief Set the thread-specific value of the variable - /// - /// \param value Value of the variable for this thread - /// - //////////////////////////////////////////////////////////// - void setValue(void* value); - - //////////////////////////////////////////////////////////// - /// \brief Retrieve the thread-specific value of the variable - /// - /// \return Value of the variable for this thread - /// - //////////////////////////////////////////////////////////// - void* getValue() const; - -private: - - //////////////////////////////////////////////////////////// - // Member data - //////////////////////////////////////////////////////////// - DWORD m_index; ///< Index of our thread-local storage slot -}; - -} // namespace priv - -} // namespace sf - - -#endif // SFML_THREADLOCALIMPL_HPP diff --git a/src/SFML/Window/Android/InputImpl.cpp b/src/SFML/Window/Android/InputImpl.cpp index 9da3a131..3108f78c 100644 --- a/src/SFML/Window/Android/InputImpl.cpp +++ b/src/SFML/Window/Android/InputImpl.cpp @@ -27,9 +27,9 @@ //////////////////////////////////////////////////////////// #include #include -#include #include #include +#include namespace sf @@ -49,7 +49,7 @@ void InputImpl::setVirtualKeyboardVisible(bool visible) // todo: Check if the window is active ActivityStates* states = getActivity(NULL); - Lock lock(states->mutex); + std::lock_guard lock(states->mutex); // Initializes JNI jint lResult; @@ -139,7 +139,7 @@ bool InputImpl::isMouseButtonPressed(Mouse::Button button) ALooper_pollAll(0, NULL, NULL, NULL); priv::ActivityStates* states = priv::getActivity(NULL); - Lock lock(states->mutex); + std::lock_guard lock(states->mutex); return states->isButtonPressed[button]; } @@ -151,7 +151,7 @@ Vector2i InputImpl::getMousePosition() ALooper_pollAll(0, NULL, NULL, NULL); priv::ActivityStates* states = priv::getActivity(NULL); - Lock lock(states->mutex); + std::lock_guard lock(states->mutex); return states->mousePosition; } @@ -184,7 +184,7 @@ bool InputImpl::isTouchDown(unsigned int finger) ALooper_pollAll(0, NULL, NULL, NULL); priv::ActivityStates* states = priv::getActivity(NULL); - Lock lock(states->mutex); + std::lock_guard lock(states->mutex); return states->touchEvents.find(finger) != states->touchEvents.end(); } @@ -196,7 +196,7 @@ Vector2i InputImpl::getTouchPosition(unsigned int finger) ALooper_pollAll(0, NULL, NULL, NULL); priv::ActivityStates* states = priv::getActivity(NULL); - Lock lock(states->mutex); + std::lock_guard lock(states->mutex); return states->touchEvents.find(finger)->second; } diff --git a/src/SFML/Window/Android/VideoModeImpl.cpp b/src/SFML/Window/Android/VideoModeImpl.cpp index d9c171c1..4d11ad61 100644 --- a/src/SFML/Window/Android/VideoModeImpl.cpp +++ b/src/SFML/Window/Android/VideoModeImpl.cpp @@ -29,7 +29,7 @@ #include #include #include -#include +#include namespace sf { @@ -53,7 +53,7 @@ VideoMode VideoModeImpl::getDesktopMode() { // Get the activity states priv::ActivityStates* states = priv::getActivity(NULL); - Lock lock(states->mutex); + std::lock_guard lock(states->mutex); return VideoMode(states->screenSize.x, states->screenSize.y); } diff --git a/src/SFML/Window/Android/WindowImplAndroid.cpp b/src/SFML/Window/Android/WindowImplAndroid.cpp index 27d2f10e..6f226890 100644 --- a/src/SFML/Window/Android/WindowImplAndroid.cpp +++ b/src/SFML/Window/Android/WindowImplAndroid.cpp @@ -29,9 +29,9 @@ #include // important to be included first (conflict with None) #include #include -#include #include #include +#include // Define missing constants for older API levels #if __ANDROID_API__ < 13 @@ -66,7 +66,7 @@ WindowImplAndroid::WindowImplAndroid(VideoMode mode, const String& title, unsign , m_hasFocus(false) { ActivityStates* states = getActivity(NULL); - Lock lock(states->mutex); + std::lock_guard lock(states->mutex); if (style& Style::Fullscreen) states->fullscreen = true; @@ -91,7 +91,7 @@ WindowImplAndroid::~WindowImplAndroid() WindowHandle WindowImplAndroid::getSystemHandle() const { ActivityStates* states = getActivity(NULL); - Lock lock(states->mutex); + std::lock_guard lock(states->mutex); return states->window; } @@ -104,7 +104,7 @@ void WindowImplAndroid::processEvents() ALooper_pollAll(0, NULL, NULL, NULL); ActivityStates* states = getActivity(NULL); - sf::Lock lock(states->mutex); + std::lock_guard lock(states->mutex); if (m_windowBeingCreated) { @@ -232,7 +232,7 @@ void WindowImplAndroid::forwardEvent(const Event& event) int WindowImplAndroid::processEvent(int fd, int events, void* data) { ActivityStates* states = getActivity(NULL); - Lock lock(states->mutex); + std::lock_guard lock(states->mutex); AInputEvent* _event = NULL; @@ -666,7 +666,7 @@ int WindowImplAndroid::getUnicode(AInputEvent* event) { // Retrieve activity states ActivityStates* states = getActivity(NULL); - Lock lock(states->mutex); + std::lock_guard lock(states->mutex); // Initializes JNI jint lResult; diff --git a/src/SFML/Window/Context.cpp b/src/SFML/Window/Context.cpp index 05bddfc0..b59e4deb 100644 --- a/src/SFML/Window/Context.cpp +++ b/src/SFML/Window/Context.cpp @@ -27,13 +27,12 @@ //////////////////////////////////////////////////////////// #include #include -#include namespace { // This per-thread variable holds the current context for each thread - sf::ThreadLocalPtr currentContext(NULL); + thread_local sf::Context* currentContext = nullptr; } namespace sf diff --git a/src/SFML/Window/EglContext.cpp b/src/SFML/Window/EglContext.cpp index 03c41979..d1893886 100644 --- a/src/SFML/Window/EglContext.cpp +++ b/src/SFML/Window/EglContext.cpp @@ -31,14 +31,13 @@ #include #include #include -#include -#include #ifdef SFML_SYSTEM_ANDROID #include #endif #ifdef SFML_SYSTEM_LINUX #include #endif +#include namespace { @@ -60,7 +59,7 @@ namespace // On Android, its native activity handles this for us sf::priv::ActivityStates* states = sf::priv::getActivity(NULL); - sf::Lock lock(states->mutex); + std::lock_guard lock(states->mutex); return states->display; @@ -113,7 +112,7 @@ m_config (NULL) // On Android, we must save the created context ActivityStates* states = getActivity(NULL); - Lock lock(states->mutex); + std::lock_guard lock(states->mutex); states->context = this; diff --git a/src/SFML/Window/GlContext.cpp b/src/SFML/Window/GlContext.cpp index 1dbbde33..e4695be1 100644 --- a/src/SFML/Window/GlContext.cpp +++ b/src/SFML/Window/GlContext.cpp @@ -27,15 +27,13 @@ //////////////////////////////////////////////////////////// #include #include -#include -#include -#include #include #include #include #include #include #include +#include #include #include #include @@ -134,13 +132,13 @@ namespace // This mutex is also used to protect the shared context // from being locked on multiple threads and for managing // the resource count - sf::Mutex mutex; + std::recursive_mutex mutex; // OpenGL resources counter unsigned int resourceCount = 0; // This per-thread variable holds the current context for each thread - sf::ThreadLocalPtr currentContext(NULL); + thread_local sf::priv::GlContext* currentContext = nullptr; // The hidden, inactive context that will be shared with all other contexts std::unique_ptr sharedContext; @@ -163,7 +161,7 @@ namespace } else if (!currentContext) { - sharedContextLock = std::make_unique(mutex); + sharedContextLock = std::make_unique>(mutex); useSharedContext = true; sharedContext->setActive(true); } @@ -184,7 +182,7 @@ namespace //////////////////////////////////////////////////////////// unsigned int referenceCount; std::unique_ptr context; - std::unique_ptr sharedContextLock; + std::unique_ptr> sharedContextLock; bool useSharedContext; }; @@ -205,7 +203,7 @@ namespace priv void GlContext::initResource() { // Protect from concurrent access - Lock lock(mutex); + std::lock_guard lock(mutex); // If this is the very first resource, trigger the global context initialization if (resourceCount == 0) @@ -281,7 +279,7 @@ void GlContext::initResource() void GlContext::cleanupResource() { // Protect from concurrent access - Lock lock(mutex); + std::lock_guard lock(mutex); // Decrement the resources counter resourceCount--; @@ -299,7 +297,7 @@ void GlContext::cleanupResource() void GlContext::acquireTransientContext() { // Protect from concurrent access - Lock lock(mutex); + std::lock_guard lock(mutex); // If this is the first TransientContextLock on this thread // construct the state object @@ -315,7 +313,7 @@ void GlContext::acquireTransientContext() void GlContext::releaseTransientContext() { // Protect from concurrent access - Lock lock(mutex); + std::lock_guard lock(mutex); // Make sure a matching acquireTransientContext() was called assert(transientContext); @@ -336,7 +334,7 @@ std::unique_ptr GlContext::create() // Make sure that there's an active context (context creation may need extensions, and thus a valid context) assert(sharedContext != NULL); - Lock lock(mutex); + std::lock_guard lock(mutex); std::unique_ptr context; @@ -364,7 +362,7 @@ std::unique_ptr GlContext::create(const ContextSettings& settings, co // Make sure that there's an active context (context creation may need extensions, and thus a valid context) assert(sharedContext != NULL); - Lock lock(mutex); + std::lock_guard lock(mutex); std::unique_ptr context; @@ -393,7 +391,7 @@ std::unique_ptr GlContext::create(const ContextSettings& settings, un // Make sure that there's an active context (context creation may need extensions, and thus a valid context) assert(sharedContext != NULL); - Lock lock(mutex); + std::lock_guard lock(mutex); std::unique_ptr context; @@ -428,7 +426,7 @@ GlFunctionPointer GlContext::getFunction(const char* name) { #if !defined(SFML_OPENGL_ES) - Lock lock(mutex); + std::lock_guard lock(mutex); return ContextType::getFunction(name); @@ -466,7 +464,7 @@ bool GlContext::setActive(bool active) { if (this != currentContext) { - Lock lock(mutex); + std::lock_guard lock(mutex); // Activate the context if (makeCurrent(true)) @@ -490,7 +488,7 @@ bool GlContext::setActive(bool active) { if (this == currentContext) { - Lock lock(mutex); + std::lock_guard lock(mutex); // Deactivate the context if (makeCurrent(false)) diff --git a/src/SFML/Window/Unix/Display.cpp b/src/SFML/Window/Unix/Display.cpp index 60f4c55a..5d6a3142 100644 --- a/src/SFML/Window/Unix/Display.cpp +++ b/src/SFML/Window/Unix/Display.cpp @@ -26,12 +26,11 @@ // Headers //////////////////////////////////////////////////////////// #include -#include -#include #include #include #include #include +#include #include @@ -40,7 +39,7 @@ namespace // The shared display and its reference counter Display* sharedDisplay = NULL; unsigned int referenceCount = 0; - sf::Mutex mutex; + std::mutex mutex; typedef std::map AtomMap; AtomMap atoms; @@ -53,7 +52,7 @@ namespace priv //////////////////////////////////////////////////////////// Display* OpenDisplay() { - Lock lock(mutex); + std::lock_guard lock(mutex); if (referenceCount == 0) { @@ -76,7 +75,7 @@ Display* OpenDisplay() //////////////////////////////////////////////////////////// void CloseDisplay(Display* display) { - Lock lock(mutex); + std::lock_guard lock(mutex); assert(display == sharedDisplay); diff --git a/src/SFML/Window/Unix/GlxContext.cpp b/src/SFML/Window/Unix/GlxContext.cpp index b16c0e28..28d9f3de 100644 --- a/src/SFML/Window/Unix/GlxContext.cpp +++ b/src/SFML/Window/Unix/GlxContext.cpp @@ -28,10 +28,9 @@ #include #include #include -#include -#include #include #include +#include #if !defined(GLX_DEBUGGING) && defined(SFML_DEBUG) // Enable this to print messages to err() everytime GLX produces errors @@ -40,7 +39,7 @@ namespace { - sf::Mutex glxErrorMutex; + std::mutex glxErrorMutex; bool glxErrorOccurred = false; int HandleXError(::Display*, XErrorEvent*) @@ -68,9 +67,9 @@ namespace } private: - sf::Lock m_lock; - ::Display* m_display; - int (*m_previousHandler)(::Display*, XErrorEvent*); + std::lock_guard m_lock; + ::Display* m_display; + int (*m_previousHandler)(::Display*, XErrorEvent*); }; } diff --git a/src/SFML/Window/Unix/WindowImplX11.cpp b/src/SFML/Window/Unix/WindowImplX11.cpp index f14f69b0..64886443 100644 --- a/src/SFML/Window/Unix/WindowImplX11.cpp +++ b/src/SFML/Window/Unix/WindowImplX11.cpp @@ -31,8 +31,6 @@ #include #include #include -#include -#include #include #include #include @@ -47,6 +45,7 @@ #include #include #include +#include #include #ifdef SFML_OPENGL_ES @@ -64,7 +63,7 @@ namespace { sf::priv::WindowImplX11* fullscreenWindow = NULL; std::vector allWindows; - sf::Mutex allWindowsMutex; + std::mutex allWindowsMutex; sf::String windowManagerName; static const unsigned long eventMask = FocusChangeMask | ButtonPressMask | @@ -649,7 +648,7 @@ WindowImplX11::~WindowImplX11() CloseDisplay(m_display); // Remove this window from the global list of windows (required for focus request) - Lock lock(allWindowsMutex); + std::lock_guard lock(allWindowsMutex); allWindows.erase(std::find(allWindows.begin(), allWindows.end(), this)); } @@ -950,7 +949,7 @@ void WindowImplX11::requestFocus() bool sfmlWindowFocused = false; { - Lock lock(allWindowsMutex); + std::lock_guard lock(allWindowsMutex); for (std::vector::iterator itr = allWindows.begin(); itr != allWindows.end(); ++itr) { if ((*itr)->hasFocus()) @@ -1313,7 +1312,7 @@ void WindowImplX11::initialize() XFlush(m_display); // Add this window to the global list of windows (required for focus request) - Lock lock(allWindowsMutex); + std::lock_guard lock(allWindowsMutex); allWindows.push_back(this); } diff --git a/src/SFML/Window/Win32/WglContext.cpp b/src/SFML/Window/Win32/WglContext.cpp index c2a813f7..6037b0dc 100644 --- a/src/SFML/Window/Win32/WglContext.cpp +++ b/src/SFML/Window/Win32/WglContext.cpp @@ -28,9 +28,6 @@ #include // included first to avoid a warning about macro redefinition #include // included second to avoid an error in WglExtensions.hpp #include -#include -#include -#include #include #include #include @@ -41,7 +38,7 @@ namespace { // Some drivers are bugged and don't track the current HDC/HGLRC properly // In order to deactivate successfully, we need to track it ourselves as well - sf::ThreadLocalPtr currentContext(NULL); + thread_local sf::priv::WglContext* currentContext = nullptr; } @@ -636,8 +633,8 @@ void WglContext::createContext(WglContext* shared) if (sharedContext) { - static Mutex mutex; - Lock lock(mutex); + static std::mutex mutex; + std::lock_guard lock(mutex); if (currentContext == shared) { @@ -706,8 +703,8 @@ void WglContext::createContext(WglContext* shared) if (sharedContext) { // wglShareLists doesn't seem to be thread-safe - static Mutex mutex; - Lock lock(mutex); + static std::mutex mutex; + std::lock_guard lock(mutex); if (currentContext == shared) {