00001 /* vim: set sw=2: 00002 * Copyright (c) 2009, Gennady Bystritsky <bystr@mac.com> 00003 * 00004 * Distributed under the MIT Licence. 00005 * This is free software. See 'LICENSE' for details. 00006 * You must read and accept the license prior to use. 00007 * 00008 * Author: Gennady Bystritsky (gennady.bystritsky@quest.com) 00009 */ 00010 00011 #ifndef _SK_RT_THREAD_H_ 00012 #define _SK_RT_THREAD_H_ 00013 00014 #include <sk/rt/Runnable.h> 00015 #include <sk/rt/thread/State.h> 00016 #include <sk/util/Holder.hxx> 00017 #include <sk/util/String.h> 00018 00019 #include <sk/rt/thread/Generic.h> 00020 #include <sk/rt/thread/UncaughtExceptionHandler.h> 00021 00022 namespace sk { 00023 namespace rt { 00024 namespace thread { 00025 class Runner; 00026 } 00027 00028 class Thread 00029 : public virtual sk::rt::thread::Generic, 00030 public virtual sk::rt::Runnable 00031 { 00032 public: 00033 Thread(); 00034 Thread(const sk::util::String& name); 00035 Thread(sk::rt::Runnable& target); 00036 Thread(sk::rt::Runnable& target, const sk::util::String& name); 00037 Thread(sk::rt::Runnable* target); 00038 Thread(sk::rt::Runnable* target, const sk::util::String& name); 00039 virtual ~Thread(); 00040 00041 // sk::rt::thread::Generic implementation 00042 void start(); 00043 void stop(); 00044 void join(); 00045 void detach(); 00046 void interrupt(); 00047 00048 // sk::rt::thread::Generic implementation 00049 const sk::util::String getName() const; 00050 const sk::rt::thread::State& getState() const; 00051 00052 // sk::rt::thread::Generic implementation 00053 bool isAlive() const; 00054 bool isInterrupted() const; 00055 bool isException() const; 00056 bool isExited() const; 00057 int exitStatus() const; 00058 00059 // sk::rt::thread::Generic implementation 00060 bool isMain() const; 00061 bool isService() const; 00062 bool isRegular() const; 00063 bool isDetached() const; 00064 00065 // sk::util::Object re-implementation. 00066 const sk::util::Class getClass() const; 00067 const sk::util::String inspect() const; 00068 uint64_t getId() const; 00069 00070 static thread::Generic& currentThread(); 00071 static int activeCount(); 00072 static void yield(); 00073 static void pass(); 00074 static void sleep(uint64_t milliseconds); 00075 00076 static void setDefaultUncaughtExceptionHandler(thread::UncaughtExceptionHandler& handler); 00077 static thread::UncaughtExceptionHandler& getDefaultUncaughtExceptionHandler(); 00078 00079 static void reset(); 00080 00081 protected: 00082 virtual void run(); 00083 00084 private: 00085 Thread(const Thread& other); 00086 Thread& operator = (const Thread& other); 00087 00088 void init(); 00089 00090 sk::util::Holder<sk::rt::Runnable> _targetHolder; 00091 sk::util::Holder<thread::Runner> _runnerHolder; 00092 00093 sk::util::String _name; 00094 uint64_t _id; 00095 }; 00096 } 00097 } 00098 00099 #endif /* _SK_RT_THREAD_H_ */