Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
Thread.h
1 /*
2  * Thread class interface
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2011-16, IRIT UPS.
6  *
7  * OTAWA is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * OTAWA is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with OTAWA; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 #ifndef ELM_SYSTEM_THREAD_H_
22 #define ELM_SYSTEM_THREAD_H_
23 
24 #include <elm/sys/SystemException.h>
25 #include <elm/util/MessageException.h>
26 
27 namespace elm { namespace sys {
28 
29 // pre-declaration
30 class Thread;
31 
32 // ThreadException class
34 public:
35  inline ThreadException(const string& message): MessageException(message) { }
36 };
37 
38 
39 // Runnable class
40 class Runnable {
41  friend class Thread;
42 public:
43  static Runnable null;
44  Runnable(void);
45  virtual ~Runnable(void);
46 
47  virtual void run(void);
48 
49  inline Thread *thread(void) const { return thr; }
50  inline static Runnable& current(void);
51 
52 protected:
53  void stop(void);
54 private:
55  Thread *thr;
56 };
57 
58 
59 // Thread class
60 class Thread {
61  friend class Runnable;
62 
63 public:
64  virtual ~Thread(void);
65  inline Runnable& runnable(void) const { return *_runnable; }
66 
67  static Thread *make(Runnable& runnable);
68  static Thread *current(void);
70 
71  virtual void start(void) = 0;
72  virtual void join(void) = 0;
73  virtual void kill(void) = 0;
74  virtual bool isRunning(void) = 0;
75 
76 protected:
79  virtual void stop(void) = 0;
80 };
81 
82 inline Runnable& Runnable::current(void) { return Thread::current()->runnable(); }
83 
84 
85 class Mutex {
86 public:
87  static Mutex *make(void);
88  virtual ~Mutex(void);
89  virtual void lock(void) = 0;
90  virtual void unlock(void) = 0;
91  virtual bool tryLock(void) = 0;
92 };
93 
94 } } // elm::sys
95 
96 #endif /* ELM_SYSTEM_THREAD_H_ */
Definition: MessageException.h:30
virtual String message(void)
Definition: util_MessageException.cpp:50
Definition: Thread.h:85
virtual bool tryLock(void)=0
virtual void lock(void)=0
virtual void unlock(void)=0
virtual ~Mutex(void)
Definition: system_Thread.cpp:177
static Mutex * make(void)
Definition: system_Thread.cpp:443
Definition: Thread.h:40
Runnable(void)
Definition: system_Thread.cpp:64
virtual void run(void)
Definition: system_Thread.cpp:54
virtual ~Runnable(void)
Definition: system_Thread.cpp:69
Thread * thread(void) const
Definition: Thread.h:49
void stop(void)
Definition: system_Thread.cpp:75
static Runnable & current(void)
Definition: Thread.h:82
Definition: Thread.h:33
ThreadException(const string &message)
Definition: Thread.h:35
Definition: Thread.h:60
virtual void start(void)=0
Thread(Runnable &runnable)
Definition: system_Thread.cpp:107
Runnable & runnable(void) const
Definition: Thread.h:65
static Thread * make(Runnable &runnable)
Definition: system_Thread.cpp:427
Runnable * _runnable
Definition: Thread.h:77
static void setRootRunnable(Runnable &runnable)
virtual void kill(void)=0
virtual ~Thread(void)
Definition: system_Thread.cpp:113
virtual bool isRunning(void)=0
virtual void stop(void)=0
static Thread * current(void)
virtual void join(void)=0
Definition: adapter.h:26