UniSet 2.32.1
ThreadCreator.h
1/*
2 * Copyright (c) 2015 Pavel Vainerman.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as
6 * published by the Free Software Foundation, version 2.1.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Lesser Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16//----------------------------------------------------------------------------------------
21//--------------------------------------------------------------------------------
22#ifndef ThreadCreator_h_
23#define ThreadCreator_h_
24//----------------------------------------------------------------------------------------
25#include <Poco/Thread.h>
26#include <sys/resource.h>
27#include <sys/types.h>
28#include <unistd.h>
29//----------------------------------------------------------------------------------------
30namespace uniset
31{
85 //----------------------------------------------------------------------------------------
86 template<class ThreadMaster>
88 public Poco::Runnable
89 {
90 public:
91
95 typedef void(ThreadMaster::* Action)(void);
96
97 ThreadCreator( ThreadMaster* m, Action a );
98 virtual ~ThreadCreator();
99
100 inline Poco::Thread::TID getTID() const
101 {
102 return thr.tid();
103 }
104
106 void setPriority( Poco::Thread::Priority prior );
107
109 Poco::Thread::Priority getPriority() const;
110
111 void stop();
112 void start();
113
114 void sleep( long milliseconds );
115
116 inline bool isRunning() const
117 {
118 return thr.isRunning();
119 }
120
121 inline void join()
122 {
123 thr.join();
124 }
125
126 inline void setFinalAction( ThreadMaster* m, Action a )
127 {
128 finm = m;
129 finact = a;
130 }
131
132 inline void setInitialAction( ThreadMaster* m, Action a )
133 {
134 initm = m;
135 initact = a;
136 }
137
138 protected:
139 virtual void run();
140 virtual void final()
141 {
142 if( finm )
143 (finm->*finact)();
144
145 //delete this;
146 }
147
148 virtual void initial()
149 {
150 if( initm )
151 (initm->*initact)();
152 }
153
154 virtual void terminate() {}
155
156 private:
157 ThreadCreator();
158
159 ThreadMaster* m = { nullptr };
160 Action act;
161
162 ThreadMaster* finm = { nullptr };
163 Action finact;
164
165 ThreadMaster* initm = { nullptr };
166 Action initact;
167
168 Poco::Thread thr;
169 };
170
171 //----------------------------------------------------------------------------------------
172 template <class ThreadMaster>
173 ThreadCreator<ThreadMaster>::ThreadCreator( ThreadMaster* m, Action a ):
174 m(m),
175 act(a),
176 finm(nullptr),
177 finact(nullptr),
178 initm(nullptr),
179 initact(nullptr)
180 {
181 }
182 //----------------------------------------------------------------------------------------
183 template <class ThreadMaster>
184 void ThreadCreator<ThreadMaster>::run()
185 {
186 initial();
187
188 if( m )
189 (m->*act)();
190
191 final();
192 }
193 //----------------------------------------------------------------------------------------
194 template <class ThreadMaster>
195 void ThreadCreator<ThreadMaster>::stop()
196 {
197 terminate();
198 }
199 //----------------------------------------------------------------------------------------
200 template <class ThreadMaster>
201 void ThreadCreator<ThreadMaster>::start()
202 {
203 thr.start( *this );
204 }
205 //----------------------------------------------------------------------------------------
206 template <class ThreadMaster>
207 void ThreadCreator<ThreadMaster>::sleep( long milliseconds )
208 {
209 thr.sleep(milliseconds);
210 }
211 //----------------------------------------------------------------------------------------
212 template <class ThreadMaster>
213 ThreadCreator<ThreadMaster>::ThreadCreator():
214 m(0),
215 act(0),
216 finm(0),
217 finact(0),
218 initm(0),
219 initact(0)
220 {
221 }
222 //----------------------------------------------------------------------------------------
223 template <class ThreadMaster>
224 ThreadCreator<ThreadMaster>::~ThreadCreator()
225 {
226 }
227 //----------------------------------------------------------------------------------------
228 template <class ThreadMaster>
229 void ThreadCreator<ThreadMaster>::setPriority( Poco::Thread::Priority prior )
230 {
231 return thr.setPriority(prior);
232 }
233 //----------------------------------------------------------------------------------------
234 template <class ThreadMaster>
235 Poco::Thread::Priority ThreadCreator<ThreadMaster>::getPriority() const
236 {
237 return thr.getPriority();
238 }
239 // -------------------------------------------------------------------------
240} // end of uniset namespace
241//----------------------------------------------------------------------------------------
242#endif // ThreadCreator_h_
Definition ThreadCreator.h:89
void(ThreadMaster::* Action)(void)
Definition ThreadCreator.h:95
void setPriority(Poco::Thread::Priority prior)
Definition ThreadCreator.h:229
Poco::Thread::Priority getPriority() const
Definition ThreadCreator.h:235
Definition Calibration.h:27