summaryrefslogtreecommitdiff
path: root/src/Thread.cxx
blob: a8f1e328c4178609c3b46881f73a3eabb586f839 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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;
     }
};

}