Removed SFML threading facilities.
This commit is contained in:
parent
a4acac813e
commit
c3567a4776
49 changed files with 143 additions and 2416 deletions
|
@ -31,10 +31,10 @@
|
|||
#include <SFML/Audio/Export.hpp>
|
||||
#include <SFML/Audio/SoundStream.hpp>
|
||||
#include <SFML/Audio/InputSoundFile.hpp>
|
||||
#include <SFML/System/Mutex.hpp>
|
||||
#include <SFML/System/Time.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <mutex>
|
||||
|
||||
|
||||
namespace sf
|
||||
|
@ -170,7 +170,7 @@ private:
|
|||
////////////////////////////////////////////////////////////
|
||||
InputSoundFile m_file; ///< The streamed music file
|
||||
std::vector<Int16> m_samples; ///< Temporary buffer of samples
|
||||
Mutex m_mutex; ///< Mutex protecting the data
|
||||
std::mutex m_mutex; ///< Mutex protecting the data
|
||||
};
|
||||
|
||||
} // namespace sf
|
||||
|
|
|
@ -30,10 +30,10 @@
|
|||
////////////////////////////////////////////////////////////
|
||||
#include <SFML/Audio/Export.hpp>
|
||||
#include <SFML/Audio/AlResource.hpp>
|
||||
#include <SFML/System/Thread.hpp>
|
||||
#include <SFML/System/Time.hpp>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
|
||||
|
||||
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<Int16> m_samples; ///< Buffer to store captured samples
|
||||
unsigned int m_sampleRate; ///< Sample rate
|
||||
Time m_processingInterval; ///< Time period between calls to onProcessSamples
|
||||
|
|
|
@ -30,10 +30,10 @@
|
|||
////////////////////////////////////////////////////////////
|
||||
#include <SFML/Audio/Export.hpp>
|
||||
#include <SFML/Audio/SoundSource.hpp>
|
||||
#include <SFML/System/Thread.hpp>
|
||||
#include <SFML/System/Time.hpp>
|
||||
#include <SFML/System/Mutex.hpp>
|
||||
#include <cstdlib>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
|
||||
|
||||
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
|
||||
|
|
|
@ -34,15 +34,10 @@
|
|||
#include <SFML/System/Err.hpp>
|
||||
#include <SFML/System/FileInputStream.hpp>
|
||||
#include <SFML/System/InputStream.hpp>
|
||||
#include <SFML/System/Lock.hpp>
|
||||
#include <SFML/System/MemoryInputStream.hpp>
|
||||
#include <SFML/System/Mutex.hpp>
|
||||
#include <SFML/System/NonCopyable.hpp>
|
||||
#include <SFML/System/Sleep.hpp>
|
||||
#include <SFML/System/String.hpp>
|
||||
#include <SFML/System/Thread.hpp>
|
||||
#include <SFML/System/ThreadLocal.hpp>
|
||||
#include <SFML/System/ThreadLocalPtr.hpp>
|
||||
#include <SFML/System/Time.hpp>
|
||||
#include <SFML/System/Utf.hpp>
|
||||
#include <SFML/System/Vector2.hpp>
|
||||
|
|
|
@ -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 <SFML/System/Export.hpp>
|
||||
#include <SFML/System/NonCopyable.hpp>
|
||||
|
||||
|
||||
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
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
|
@ -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 <SFML/System/Export.hpp>
|
||||
#include <SFML/System/NonCopyable.hpp>
|
||||
#include <memory>
|
||||
|
||||
|
||||
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<priv::MutexImpl> 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
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
|
@ -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 <SFML/System/Export.hpp>
|
||||
#include <SFML/System/NonCopyable.hpp>
|
||||
#include <cstdlib>
|
||||
#include <memory>
|
||||
|
||||
|
||||
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 <typename F>
|
||||
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 <typename F, typename A>
|
||||
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 <typename C>
|
||||
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<priv::ThreadImpl> m_impl; ///< OS-specific implementation of the thread
|
||||
std::shared_ptr<priv::ThreadFunc> m_entryPoint; ///< Abstraction of the function to run
|
||||
};
|
||||
|
||||
#include <SFML/System/Thread.inl>
|
||||
|
||||
} // 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
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
|
@ -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 <typename T>
|
||||
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 <typename F, typename A>
|
||||
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 <typename C>
|
||||
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 <typename F>
|
||||
Thread::Thread(F functor) :
|
||||
m_entryPoint(std::make_shared<priv::ThreadFunctor<F>>(functor))
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
template <typename F, typename A>
|
||||
Thread::Thread(F function, A argument) :
|
||||
m_entryPoint(std::make_shared<priv::ThreadFunctorWithArg<F, A>>(function, argument))
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
template <typename C>
|
||||
Thread::Thread(void(C::*function)(), C* object) :
|
||||
m_entryPoint(std::make_shared<priv::ThreadMemberFunc<C>>(function, object))
|
||||
{
|
||||
}
|
|
@ -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 <SFML/System/Export.hpp>
|
||||
#include <SFML/System/NonCopyable.hpp>
|
||||
#include <cstdlib>
|
||||
#include <memory>
|
||||
|
||||
|
||||
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<priv::ThreadLocalImpl> 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.
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
|
@ -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 <SFML/System/ThreadLocal.hpp>
|
||||
|
||||
|
||||
namespace sf
|
||||
{
|
||||
////////////////////////////////////////////////////////////
|
||||
/// \brief Pointer to a thread-local variable
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
template <typename T>
|
||||
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<T>& operator =(T* value);
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// \brief Assignment operator for a ThreadLocalPtr parameter
|
||||
///
|
||||
/// \param right ThreadLocalPtr to assign
|
||||
///
|
||||
/// \return Reference to self
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
ThreadLocalPtr<T>& operator =(const ThreadLocalPtr<T>& right);
|
||||
};
|
||||
|
||||
} // namespace sf
|
||||
|
||||
#include <SFML/System/ThreadLocalPtr.inl>
|
||||
|
||||
|
||||
#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<MyClass> 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.
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
|
@ -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 <typename T>
|
||||
ThreadLocalPtr<T>::ThreadLocalPtr(T* value) :
|
||||
ThreadLocal(value)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
template <typename T>
|
||||
T& ThreadLocalPtr<T>::operator *() const
|
||||
{
|
||||
return *static_cast<T*>(getValue());
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
template <typename T>
|
||||
T* ThreadLocalPtr<T>::operator ->() const
|
||||
{
|
||||
return static_cast<T*>(getValue());
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
template <typename T>
|
||||
ThreadLocalPtr<T>::operator T*() const
|
||||
{
|
||||
return static_cast<T*>(getValue());
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
template <typename T>
|
||||
ThreadLocalPtr<T>& ThreadLocalPtr<T>::operator =(T* value)
|
||||
{
|
||||
setValue(value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
template <typename T>
|
||||
ThreadLocalPtr<T>& ThreadLocalPtr<T>::operator =(const ThreadLocalPtr<T>& right)
|
||||
{
|
||||
setValue(right.getValue());
|
||||
return *this;
|
||||
}
|
||||
|
||||
} // namespace sf
|
Loading…
Add table
Add a link
Reference in a new issue