1 /*-----------------------------------------------------------------------------
2 Name: ThreadBase.h
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 Comment: Encapsulates (and hides) threads
6 ------------------------------------------------------------------------------*/
7
8 #ifndef _UTIL_THREAD_THREADBASE_H
9 #define _UTIL_THREAD_THREADBASE_H
10
11 #include <util/xmlBlasterDef.h>
12
13 class thread;
14 class mutex;
15 class lock;
16 class condition;
17
18 namespace org { namespace xmlBlaster { namespace util { namespace thread {
19
20 class ThreadImpl;
21 class MutexImpl;
22 class LockImpl;
23 class ConditionImpl;
24
25 class Thread;
26 class Condition;
27 class Lock;
28
29 /* -------------------------- ThreadRunner --------------------------*/
30
31 class ThreadRunner
32 {
33 public:
34 Thread& owner_;
35 ThreadRunner(Thread& owner);
36 void operator()();
37 };
38
39
40 /* -------------------------- Thread --------------------------------*/
41
42 /**
43 * Little framework for the abstraction of threads and thread related stuff. Currently the implementation of
44 * threads and their synchronization is provided by boost::threads. Since some platforms have problems building
45 * this library we provided here an abstraction which makes it easy to switch to another implementation if
46 * needed.
47 *
48 * This framework provides the following classes:
49 * ThreadRunner: only used internally by the class Thread.
50 * Thread: This class has a pure virtual method 'void run()' which must be implemented by the user.
51 * Mutex: The mutual exclusion class
52 * Condition: The class for wait and notify.
53 * Lock: used to lock a mutex. The constructor locks and the destructor unlocks.
54 *
55 * @author <a href='mailto:laghi@swissinfo.org'>Michele Laghi</a>
56 *
57 */
58 class Dll_Export Thread
59 {
60 friend class ThreadRunner;
61 private:
62 ThreadImpl* thread_;
63 ThreadRunner* runner_;
64 bool isStarted_;
65
66 public:
67 Thread();
68
69 virtual ~Thread();
70
71 /**
72 * When this method is invoked, the thread starts to run. Note that you can not
73 * start a thread twice.
74 * @return true if the thread could be started, false otherwise.
75 */
76 virtual bool start(bool detached);
77
78 /** This is the method which has to be implemented by the user */
79 virtual void run() = 0;
80
81 /** Sleeps the specified amount of time in nanoseconds */
82 static void sleepNanos(org::xmlBlaster::util::Timestamp nanoSecondDelay);
83
84 /** Sleeps the specified amount of time in nanoseconds */
85 static void sleep(long millis);
86
87 /** Sleeps the specified amount of time in nanoseconds */
88 static void sleepSecs(long secs);
89
90 /** returns the current timestamp */
91 static org::xmlBlaster::util::Timestamp getCurrentTimestamp();
92
93
94 /**
95 * Joins the thread to the current thread, in other words, the it will wait until the
96 * thread on which this method is invoked, will terminate, before it continues.
97 */
98 virtual void join();
99
100
101 bool isRunning() const
102 {
103 return (thread_ != NULL);
104 }
105
106 };
107
108
109 /* -------------------------- MutexClass -----------------------------*/
110
111 class Dll_Export MutexClass
112 {
113 friend class Lock;
114 private:
115 MutexImpl* mutex_;
116
117 public:
118 /**
119 * The locks may not be called recursive.
120 * Posix supports recursive calls as an extension.
121 * @param mutex The mutex implementation
122 * @param ignore If true no lock is created
123 * @param recursive If true the same thread may call the lock recursive
124 * Note that the thread needs to free the lock as many times again.
125 * @since xmlBlaster 1.0.5
126 */
127 MutexClass(bool recursive=false);
128
129 ~MutexClass();
130 };
131
132
133 /* -------------------------- Lock ------------------------------*/
134
135 class Dll_Export Lock
136 {
137 friend class Condition;
138 private:
139 LockImpl* lock_;
140
141 public:
142 Lock(const MutexClass& mutex, bool ignore=false);
143
144 ~Lock();
145 };
146
147
148 /* -------------------------- Lock ------------------------------*/
149
150 class Dll_Export Condition
151 {
152 private:
153 ConditionImpl* condition_;
154 public:
155 Condition();
156
157 ~Condition();
158
159 void wait(const Lock& lock, long delay);
160
161 void notify();
162 };
163
164
165 }}}} // namespaces
166
167 #endif
syntax highlighted by Code2HTML, v. 0.9.1