]> git.tdb.fi Git - libs/core.git/commitdiff
Add an interface for naming threads
authorMikko Rasa <tdb@tdb.fi>
Wed, 20 Jul 2016 23:50:56 +0000 (02:50 +0300)
committerMikko Rasa <tdb@tdb.fi>
Wed, 20 Jul 2016 23:50:56 +0000 (02:50 +0300)
source/core/thread.cpp
source/core/thread.h
source/core/unix/thread.cpp
source/core/windows/thread.cpp

index d2451a0d819187e67f0ea41340720e62ac7f3a4a..721e8cfef51f2ff772c304ff9ece65554c3f033a 100644 (file)
@@ -6,8 +6,9 @@ using namespace std;
 
 namespace Msp {
 
-Thread::Thread():
+Thread::Thread(const string &name):
        priv_(new Private),
+       name_(name),
        state_(PENDING)
 { }
 
@@ -48,6 +49,7 @@ void Thread::launch()
 ThreadReturn THREAD_CALL Thread::Private::main_wrapper(void *arg)
 {
        Thread *thread = reinterpret_cast<Thread *>(arg);
+       thread->platform_setname();
        thread->main();
        thread->state_ = FINISHED;
        return 0;
index 1fff3627b037287aac89cb8e68e77f15d5543b47..7cf17b8d327ed3e84075bb4c247f4c5d16c4ff6f 100644 (file)
@@ -1,6 +1,8 @@
 #ifndef MSP_CORE_THREAD_H_
 #define MSP_CORE_THREAD_H_
 
+#include <string>
+
 namespace Msp {
 
 /**
@@ -25,16 +27,19 @@ private:
        };
 
        Private *priv_;
+       std::string name_;
        State state_;
 
 protected:
-       Thread();
+       Thread(const std::string & = std::string());
 private:
        Thread(const Thread &);
        Thread &operator=(const Thread &);
 public:
        virtual ~Thread();
 
+       const std::string &get_name() const { return name_; }
+
        /** Indicates whether the thread has finished running. */
        bool is_finished() { return state_>=FINISHED; }
 
@@ -54,6 +59,7 @@ private:
        void platform_join();
        void platform_kill();
        void platform_launch();
+       void platform_setname();
 
 protected:
        virtual void main() = 0;
index 26afeefc4300e0955b52ca72931c1c2b63eb0d16..3c17a9aabc8054c423253c115ba803325dc3de62 100644 (file)
@@ -20,4 +20,10 @@ void Thread::platform_launch()
        pthread_create(&priv_->handle, 0, &Private::main_wrapper, this);
 }
 
+void Thread::platform_setname()
+{
+       if(!name_.empty())
+               pthread_setname_np(priv_->handle, name_.c_str());
+}
+
 } // namespace Msp
index 7c436183d4f279cdc2a034053041fb5c5b96ad1d..ab02c4d04094394e384b91defd717b70ac5918df 100644 (file)
@@ -20,4 +20,9 @@ void Thread::platform_launch()
        priv_->handle = CreateThread(0, 0, &Private::main_wrapper, this, 0, &dummy);
 }
 
+void Thread::platform_setname()
+{
+       // TODO: https://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx
+}
+
 } // namespace Msp