Main Page
Related Pages
Namespaces
Classes
Files
Workshop
Donate
Hausmi
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Pages
Thread.h
Go to the documentation of this file.
1
/*
2
* Thread.h
3
*
4
* This file is part of the HausmiSEP project
5
*
6
* Copyright (C) 2012, 2013 Marco Alvarado (malvcr@gmail.com)
7
*
8
* This program is free software: you can redistribute it and/or modify
9
* it under the terms of the GNU General Public License as published by
10
* the Free Software Foundation, either version 3 of the License, or
11
* (at your option) any later version.
12
*
13
* This program is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
* GNU General Public License for more details.
17
*
18
* You should have received a copy of the GNU General Public License
19
* along with this program. If not, see <http://www.gnu.org/licenses/>.
20
*/
21
22
#ifndef THREAD_H_
23
#define THREAD_H_
24
25
#include <
HSEP/HSEP.h
>
26
#include <
HSEP/HSEPObject.h
>
27
#include <
HSEP/Mutex.h
>
28
29
#include <thread>
30
#include <list>
31
#include <cstring>
32
33
using namespace
std;
34
35
namespace
HSEP {
36
37
#define THREADNAME_LEN 24
38
#define NON_DISPATCHED -10
39
63
class
Thread
:
public
HSEPObject
{
64
65
thread aHandler;
/*< The standard thread handler */
66
int
aExitCode;
/*< Exit code emitted by the perform method and recoverable on the join one */
67
char
aThreadName[
THREADNAME_LEN
+1];
/*< Unique string representation for the thread */
68
69
struct
Flags {
70
71
bool
aMoving : 1;
/*< Flag that advice the thread to move to another process or computer */
72
bool
aContinue : 1;
/*< Flag that advice the thread to continue processing or not */
73
bool
aDispatched : 1;
/*< Flat that advice the thread was dispatched */
74
75
Flags() {
76
memset(
this
,0,
sizeof
(*
this
));
77
aContinue =
true
;
78
}
79
80
} aFlags;
81
85
void
run();
86
87
public
:
88
Thread
(
const
char
* pType) :
HSEPObject
(pType) {
89
aExitCode = -1;
90
memset(aThreadName,0,
THREADNAME_LEN
+1);
91
sprintf(aThreadName,
"%p"
,(
void
*)
this
);
92
}
93
94
virtual
~Thread
() {}
95
96
const
char
*
namePtr
()
const
{
97
return
aThreadName;
98
}
99
110
bool
dispatch();
111
112
117
int
join();
118
122
void
finish
() {
123
ExclusiveScope
vArea(&aMutex);
124
aFlags.aContinue =
false
;
125
}
126
132
bool
finishRequested
()
const
{
133
ExclusiveScope
vScope(&aMutex);
134
return
!aFlags.aContinue;
135
}
136
147
void
relocate(
string
pDestination);
148
149
150
protected
:
151
158
Thread
(
const
char
* pType,
string
& pReceivedState) :
HSEPObject
(pType) {
159
aFlags.aContinue =
true
;
160
aExitCode = -1;
161
aFlags.aMoving =
false
;
162
memset(aThreadName,0,
THREADNAME_LEN
+1);
163
sprintf(aThreadName,
"%p"
,(
void
*)
this
);
164
165
d_restoreState(pReceivedState);
166
}
167
168
mutable
Mutex
aMutex
;
/*< Internal concurrency protected handler */
169
176
virtual
bool
prepare() = 0;
177
185
virtual
int
perform() = 0;
186
191
virtual
void
dispose() = 0;
192
193
/*
194
* THIS WAS REPLACED BY AN INTERNAL LAMBDA FUNCTION
195
*
196
* Internal body function (internal use, registered as a friend function).
197
* @param pThreadBase pointer to "this".
198
*/
199
//friend void threadFunction(void* pThread);
200
201
// These are placeholders for the forthcoming distributed functionality
202
209
virtual
void
d_packState
(
string
& pStateData) {
210
}
211
219
virtual
bool
d_sendToDestination
(
string
& pStateData) {
220
return
false
;
221
}
222
227
virtual
void
d_restoreState
(
string
& pSource) {
228
}
229
230
};
// Thread
231
232
typedef
list<Thread*>
ThreadPtrList
;
233
typedef
ThreadPtrList::iterator
ThreadPtrListIterator
;
234
241
class
LThread
:
public
Thread
{
242
function< bool () > aWorker;
243
protected
:
244
245
bool
prepare
() {
246
return
true
;
247
}
248
249
int
perform
() {
250
if
(!aWorker()) {
251
finish();
252
}
253
return
0;
254
}
255
256
void
dispose
() {
257
}
258
259
public
:
260
LThread
(
function
<
bool
() > pWorker) :
Thread
(
"LThread"
), aWorker(pWorker) {}
261
262
};
// LThread class
263
264
}
// HSEP namespace
265
266
#endif
/* THREAD_H_ */
HSEP.0.1
include
HSEP
Thread.h
Generated on Fri Jul 19 2013 12:00:29 by
1.8.2