]> git.tdb.fi Git - libs/core.git/blobdiff - source/core/thread.h
Add an interface for naming threads
[libs/core.git] / source / core / thread.h
index 2b0766c68ba7fbd0d443265d30d58628ddda76f8..7cf17b8d327ed3e84075bb4c247f4c5d16c4ff6f 100644 (file)
@@ -1,12 +1,7 @@
-/*
-This file is part of libmspframework
-Copyright © 2006 Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-#ifndef MSP_FRAMEWORK_THREAD_H_
-#define MSP_FRAMEWORK_THREAD_H_
+#ifndef MSP_CORE_THREAD_H_
+#define MSP_CORE_THREAD_H_
 
-#include "types.h"
+#include <string>
 
 namespace Msp {
 
@@ -19,30 +14,55 @@ is started.
 */
 class Thread
 {
+private:
+       struct Private;
+
+       enum State
+       {
+               PENDING,
+               RUNNING,
+               FINISHED,
+               KILLED,
+               JOINED
+       };
+
+       Private *priv_;
+       std::string name_;
+       State state_;
+
+protected:
+       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; }
+
+       /** Waits for the thread to exit.  Calling this from the thread will cause a
+       deadlock. */
        void join();
-       void cancel();
+
+       /** Violently terminates the thread.  This should only be used as a last
+       resort, as the thread gets no chance to clean up. */
        void kill();
-       virtual ~Thread();
+
 protected:
-       Thread(): launched_(false) { }
+       /** Starts the thread.  Can only be called once for each Thread instance. */
        void launch();
-       virtual void main()=0;
-       void check_cancel();
-private:
-       ThreadHandle thread_;
-       bool         launched_;
 
-       Thread(const Thread &);
-       Thread &operator=(const Thread &);
+private:
+       void platform_join();
+       void platform_kill();
+       void platform_launch();
+       void platform_setname();
 
-#ifdef WIN32
-#      define THREAD_RETURN_ DWORD WINAPI
-#else
-#      define THREAD_RETURN_ void *
-#endif
-       static THREAD_RETURN_ main_(void *t) { ((Thread *)t)->main(); return 0; }
-#undef THREAD_RETURN_
+protected:
+       virtual void main() = 0;
 };
 
 } // namespace Msp