diff options
Diffstat (limited to 'src/Thread.cxx')
-rw-r--r-- | src/Thread.cxx | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/src/Thread.cxx b/src/Thread.cxx new file mode 100644 index 0000000..a8f1e32 --- /dev/null +++ b/src/Thread.cxx @@ -0,0 +1,71 @@ +namespace PluggIt { + +class Thread +{ + private Runnable *m_target; + private string m_name; + + private DirectThread *m_thread; + private volatile bool m_interrupted; + + + public Thread( Runnable *target, string name = "" ) { + m_target = target; + m_name = name; + + m_thread = NULL; + m_interrupted = false; + } + + virtual ~Thread() { + if (m_thread) + direct_thread_destroy( m_thread ); + } + + public bool start() { + if (m_thread) + return false; + + m_thread = direct_thread_create( DTT_DEFAULT, main, m_target, m_name.c_str() ); + + return m_thread != NULL; + } + + public bool interrupt() { + if (!m_thread) + return false; + + m_interrupted = true; + + return true; + } + + public bool isInterrupted() { + return m_interrupted; + } + + public bool join() { + if (!m_thread) + return false; + + direct_thread_join( m_thread ); + direct_thread_destroy( m_thread ); + + m_thread = NULL; + m_interrupted = false; + + return true; + } + + + private static void *main( DirectThread *thread, void *arg ) { + Runnable *target = (Runnable*) arg; + + target->run(); + + return NULL; + } +}; + +} + |