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);
69  static void setRootRunnable(Runnable& runnable);
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_ */
elm::MessageException
Definition: MessageException.h:30
elm::sys::Runnable::run
virtual void run(void)
Definition: system_Thread.cpp:54
elm::sys::Thread::kill
virtual void kill(void)=0
elm::sys::Runnable::current
static Runnable & current(void)
Definition: Thread.h:82
elm::sys::Thread::setRootRunnable
static void setRootRunnable(Runnable &runnable)
elm::sys::Runnable::stop
void stop(void)
Definition: system_Thread.cpp:75
elm::sys::Thread::start
virtual void start(void)=0
elm::MessageException::message
virtual String message(void)
Definition: util_MessageException.cpp:50
elm::sys::Runnable::thread
Thread * thread(void) const
Definition: Thread.h:49
elm::sys::Thread::make
static Thread * make(Runnable &runnable)
Definition: system_Thread.cpp:427
elm::sys::Thread::~Thread
virtual ~Thread(void)
Definition: system_Thread.cpp:113
elm::sys::Mutex::tryLock
virtual bool tryLock(void)=0
elm::sys::Thread::Thread
Thread(Runnable &runnable)
Definition: system_Thread.cpp:107
elm::sys::Thread::join
virtual void join(void)=0
elm::sys::Thread::runnable
Runnable & runnable(void) const
Definition: Thread.h:65
elm::sys::Mutex::lock
virtual void lock(void)=0
elm::sys::Thread::_runnable
Runnable * _runnable
Definition: Thread.h:77
elm
Definition: adapter.h:26
elm::sys::ThreadException
Definition: Thread.h:33
elm::sys::Thread::current
static Thread * current(void)
elm::sys::Mutex::make
static Mutex * make(void)
Definition: system_Thread.cpp:443
elm::sys::Runnable::~Runnable
virtual ~Runnable(void)
Definition: system_Thread.cpp:69
elm::sys::Thread::stop
virtual void stop(void)=0
elm::sys::Thread
Definition: Thread.h:60
elm::sys::Runnable::Runnable
Runnable(void)
Definition: system_Thread.cpp:64
elm::sys::Runnable
Definition: Thread.h:40
elm::sys::Mutex
Definition: Thread.h:85
elm::sys::Mutex::unlock
virtual void unlock(void)=0
elm::sys::Mutex::~Mutex
virtual ~Mutex(void)
Definition: system_Thread.cpp:177
elm::sys::Thread::isRunning
virtual bool isRunning(void)=0
elm::sys::ThreadException::ThreadException
ThreadException(const string &message)
Definition: Thread.h:35