diff options
author | Justin Dickow <jjdickow@gmail.com> | 2015-01-26 11:15:48 -0500 |
---|---|---|
committer | Justin Dickow <jjdickow@gmail.com> | 2015-01-26 11:15:48 -0500 |
commit | 46ed776c537131e72747538488b213cf45f23e22 (patch) | |
tree | 4cad1abf411addfd72ab591c93edeb5d225d9a39 /src/components/include/utils/threads/thread.h | |
parent | a24079cb8de5563376dcf782cdb9fe3cae063841 (diff) | |
download | sdl_core-46ed776c537131e72747538488b213cf45f23e22.tar.gz |
Latest internal release - last bulk merge before pull request and gitflow!?
Smoke tested on Ubuntu 12.04
Signed-off-by: Justin Dickow <jjdickow@gmail.com>
Diffstat (limited to 'src/components/include/utils/threads/thread.h')
-rw-r--r-- | src/components/include/utils/threads/thread.h | 124 |
1 files changed, 62 insertions, 62 deletions
diff --git a/src/components/include/utils/threads/thread.h b/src/components/include/utils/threads/thread.h index 6c3968c51f..3b81cf3454 100644 --- a/src/components/include/utils/threads/thread.h +++ b/src/components/include/utils/threads/thread.h @@ -43,16 +43,16 @@ #include "utils/macro.h" #include "utils/threads/thread_delegate.h" #include "utils/threads/thread_options.h" -#include "utils/conditional_variable.h" -#include "utils/lock.h" namespace threads { +namespace impl { #if defined(OS_POSIX) typedef pthread_t PlatformThreadHandle; #else #error Please implement thread for your OS #endif +} /** * Non platform specific thread abstraction that establishes a @@ -76,69 +76,56 @@ typedef pthread_t PlatformThreadHandle; * printf("ok!\n"); */ class Thread; -void enqueue_to_join(Thread*); - Thread* CreateThread(const char* name, ThreadDelegate* delegate); void DeleteThread(Thread*); class Thread { - private: - const std::string name_; - // Should be locked to protect delegate_ value - sync_primitives::Lock delegate_lock_; - ThreadDelegate* delegate_; - PlatformThreadHandle handle_; - ThreadOptions thread_options_; - // Should be locked to protect isThreadRunning_ and thread_created_ values - sync_primitives::Lock state_lock_; - volatile unsigned int isThreadRunning_; - volatile bool stopped_; - volatile bool finalized_; - bool thread_created_; - // Signalled when Thread::start() is called - sync_primitives::ConditionalVariable run_cond_; + friend Thread* CreateThread(const char*, ThreadDelegate*); + friend void DeleteThread(Thread*); public: /** + * Class that represents unique in-process thread identifier + * due to restriction of pthread API it only allows checks + * for equality to different thread id and no ordering. + * + * ostream<< operator is provided for this class which + * outputs thread name associated to an identifier. + */ + class Id { + public: + explicit Id(const impl::PlatformThreadHandle& id): id_(id) {} + bool operator==(const Id& that) const; + impl::PlatformThreadHandle Handle() const { return id_; } + private: + impl::PlatformThreadHandle id_; + friend class Thread; + }; + + // Get unique ID of currently executing thread + static Id CurrentId(); + + // Get name associated with thread identified by thread_id + static std::string NameFromId(const Id& thread_id); + + // Give thread thread_id a name, helpful for debugging + static void SetNameForId(const Id& thread_id, const std::string& name); + + /** * Starts the thread. * @return true if the thread was successfully started. */ bool start(); + ThreadDelegate* delegate() const; + /** - * Starts the thread. Behaves exactly like \ref start() in addition to + * Starts the thread. Behaves exactly like Start in addition to * allow to override the default options. - * @param options Thread options. Look for 'threads/thread_options.h' + * @param options - thread options. Look for 'threads/thread_options.h' * for details. * @return true if the thread was successfully started. */ - bool start(const ThreadOptions& options); - - void WaitForRun(); - - sync_primitives::Lock& delegate_lock() { - return delegate_lock_; - } - - ThreadDelegate *delegate() const { - return delegate_; - } - - void set_delegate(ThreadDelegate *delegate) { - DCHECK(!isThreadRunning_); - delegate_ = delegate; - } - - friend Thread* CreateThread(const char* name, ThreadDelegate* delegate); - friend void DeleteThread(Thread*); - - public: - - // Get unique ID of currently executing thread - static PlatformThreadHandle CurrentId(); - - // Give thread thread_id a name, helpful for debugging - static void SetNameForId(const PlatformThreadHandle& thread_id, std::string name); - + bool startWithOptions(const ThreadOptions& options); /** * Signals the thread to exit and returns once the thread has exited. @@ -150,14 +137,11 @@ class Thread { */ void stop(); - - void join(); - /** * Get thread name. * @return thread name */ - const std::string& name() { + const std::string& thread_name() { return name_; } @@ -170,7 +154,9 @@ class Thread { return isThreadRunning_; } - void set_running(bool running); + void set_running(bool running) { + isThreadRunning_ = running; + } /** * Is thread joinable? @@ -192,8 +178,16 @@ class Thread { * The native thread handle. * @return thread handle. */ - PlatformThreadHandle thread_handle() const { - return handle_; + impl::PlatformThreadHandle thread_handle() const { + return thread_handle_; + } + + /** + * Thread id. + * @return return thread id. + */ + Id thread_id() const { + return Id(thread_handle()); } /** @@ -210,7 +204,11 @@ class Thread { static size_t kMinStackSize; protected: - sync_primitives::ConditionalVariable state_cond_; + const std::string name_; + ThreadDelegate* delegate_; + impl::PlatformThreadHandle thread_handle_; + ThreadOptions thread_options_; + volatile unsigned int isThreadRunning_; private: /** @@ -218,17 +216,19 @@ class Thread { * @param name - display string to identify the thread. * @param delegate - thread procedure delegate. Look for * 'threads/thread_delegate.h' for details. - * LifeCycle thread , otherwise it will be joined in stop method - * NOTE: delegate will be deleted after thread will be joined + * NOTE: delegate will be deleted by destructor. * This constructor made private to prevent * Thread object to be created on stack */ Thread(const char* name, ThreadDelegate* delegate); + DISALLOW_COPY_AND_ASSIGN(Thread); - virtual ~Thread(); - static void* threadFunc(void* arg); - static void cleanup(void* arg); + virtual ~Thread() { } }; +inline bool operator!= (const Thread::Id& left, const Thread::Id& right) { + return !(left == right); +} +std::ostream& operator<<(std::ostream& os, const Thread::Id& thread_id); } // namespace threads #endif // SRC_COMPONENTS_INCLUDE_UTILS_THREADS_THREAD_H_ |