IBR-DTNSuite
0.12
Main Page
Namespaces
Classes
Files
File List
File Members
IBR-DTNSuite
Namespaces
Classes
Files
File List
daemon
ibrcommon
ibrcommon
data
link
net
ssl
thread
AtomicCounter.cpp
AtomicCounter.h
Conditional.cpp
Conditional.h
Mutex.cpp
Mutex.h
MutexLock.cpp
MutexLock.h
Queue.h
RWLock.cpp
RWLock.h
RWMutex.cpp
RWMutex.h
Semaphore.cpp
Semaphore.h
SharedReference.h
SignalHandler.cpp
SignalHandler.h
Thread.cpp
Thread.h
ThreadsafeReference.h
ThreadsafeState.h
Timer.cpp
Timer.h
xml
appstreambuf.cpp
appstreambuf.h
config.h
Exceptions.h
ibrcommon.h
Iterator.h
Logger.cpp
Logger.h
MonotonicClock.cpp
MonotonicClock.h
refcnt_ptr.h
SyslogStream.cpp
SyslogStream.h
TimeMeasurement.cpp
TimeMeasurement.h
TLSExceptions.h
ibrdtn
tools
File Members
Thread.h
Go to the documentation of this file.
1
/*
2
* Thread.h
3
*
4
* Copyright (C) 2011 IBR, TU Braunschweig
5
*
6
* Written-by: Johannes Morgenroth <morgenroth@ibr.cs.tu-bs.de>
7
*
8
* Licensed under the Apache License, Version 2.0 (the "License");
9
* you may not use this file except in compliance with the License.
10
* You may obtain a copy of the License at
11
*
12
* http://www.apache.org/licenses/LICENSE-2.0
13
*
14
* Unless required by applicable law or agreed to in writing, software
15
* distributed under the License is distributed on an "AS IS" BASIS,
16
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
* See the License for the specific language governing permissions and
18
* limitations under the License.
19
*
20
*/
21
22
#ifndef IBRCOMMON_THREAD_H_
23
#define IBRCOMMON_THREAD_H_
24
25
#include <pthread.h>
26
#include <sys/types.h>
27
#include "
ibrcommon/thread/Mutex.h
"
28
#include "
ibrcommon/thread/Conditional.h
"
29
#include "
ibrcommon/thread/ThreadsafeState.h
"
30
31
//2 MB is a sensible default, we set this since system defaults vary widely: https://computing.llnl.gov/tutorials/pthreads/#Stack
32
#define DEFAULT_STACKSIZE 2*1024*1024
33
34
namespace
ibrcommon
35
{
36
class
ThreadException
:
public
ibrcommon::Exception
37
{
38
public
:
39
ThreadException
(
int
err = 0,
string
what
=
"An error occured during a thread operation."
) throw() : ibrcommon::
Exception
(
what
),
error
(err)
40
{
41
};
42
43
const
int
error
;
44
};
45
56
class
Thread
57
{
58
protected
:
59
enum
THREAD_STATE
60
{
61
THREAD_CREATED
= 1 << 0,
62
THREAD_STARTED
= 1 << 1,
63
THREAD_INITIALIZED
= 1 << 2,
64
THREAD_RUNNING
= 1 << 3,
65
THREAD_CANCELLED
= 1 << 4,
66
THREAD_FINALIZING
= 1 << 5,
67
THREAD_JOINABLE
= 1 << 6,
68
THREAD_FINALIZED
= 1 << 7
69
};
70
71
ibrcommon::ThreadsafeState<THREAD_STATE>
_state
;
72
73
// thread's id
74
pthread_t
tid
;
75
76
// thread's stack size
77
size_t
stack
;
78
79
// thread's priority
80
int
priority
;
81
82
// thread's attributes
83
pthread_attr_t
attr
;
84
85
// thread is detached
86
bool
_detached
;
87
93
Thread
(
size_t
stack
=
DEFAULT_STACKSIZE
);
94
95
public
:
96
static
size_t
getNumberOfProcessors
();
97
101
virtual
~Thread
() = 0;
102
106
void
reset
() throw (
ThreadException
);
107
112
static
void
yield
(
void
);
113
118
static
void
sleep
(time_t timeout);
119
123
virtual
void
setup
(
void
) throw () { };
124
128
virtual
void
run
(
void
) throw () = 0;
129
133
virtual
void
finally
(
void
) throw () { };
134
139
static
void
concurrency
(
int
level);
140
144
bool
isFinalized
() throw ();
145
151
bool
operator==(const ibrcommon::
Thread
&other)
152
{
153
return
(
equal
(other.tid,
tid
) != 0);
154
}
155
156
protected
:
162
int
kill
(
int
sig);
163
167
void
cancel
() throw ();
168
virtual
void
__cancellation
() throw () = 0;
169
176
static
bool
equal
(pthread_t thread1, pthread_t thread2);
177
181
static
void
*
__execute__
(
void
*obj) throw ();
182
};
183
194
class
JoinableThread
: protected
Thread
195
{
196
protected
:
201
JoinableThread
(
size_t
size =
DEFAULT_STACKSIZE
);
202
203
public
:
208
virtual
~
JoinableThread
() = 0;
209
215
void
join(
void
)
throw
(
ThreadException
);
216
221
inline
bool
isRunning
(
void
)
222
{
return
_state
==
THREAD_RUNNING
; };
223
232
void
start(
int
priority
= 0) throw (
ThreadException
);
233
237
void
stop() throw ();
238
};
239
247
class
DetachedThread
: protected Thread
248
{
249
protected
:
254
DetachedThread
(
size_t
size =
DEFAULT_STACKSIZE
);
255
256
public
:
262
virtual
~
DetachedThread
() = 0;
263
270
void
start(
int
priority
= 0)
throw
(ThreadException);
271
275
void
stop()
throw
(ThreadException);
276
};
277
}
278
279
#endif
/* THREAD_H_ */
ibrcommon
ibrcommon
thread
Thread.h
Generated on Thu Mar 27 2014 09:26:21 for IBR-DTNSuite by
1.8.4