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
ThreadsafeState.h
Go to the documentation of this file.
1
/*
2
* ThreadsafeState.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 THREADSAFESTATE_H_
23
#define THREADSAFESTATE_H_
24
25
#include <
ibrcommon/thread/MutexLock.h
>
26
#include <
ibrcommon/thread/Conditional.h
>
27
28
namespace
ibrcommon
29
{
30
template
<
class
T>
31
class
ThreadsafeState
:
protected
ibrcommon::Conditional
32
{
33
protected
:
34
T
_state
;
35
const
T
_final_state
;
36
37
public
:
38
ThreadsafeState
(T
init
, T
final
)
39
:
_state
(init),
_final_state
(final)
40
{
41
};
42
43
virtual
~ThreadsafeState
()
44
{
45
ibrcommon::MutexLock
l(*
this
);
46
_state
=
_final_state
;
47
this->
signal
(
true
);
48
};
49
50
void
reset
(T
init
)
51
{
52
_state
=
init
;
53
}
54
55
T
get
()
const
56
{
57
return
_state
;
58
}
59
60
void
wait
(T st)
61
{
62
ibrcommon::MutexLock
l(*
this
);
63
while
(
_state
!= st)
64
{
65
if
(
_state
==
_final_state
)
return
;
66
this->
wait
();
67
}
68
}
69
70
T
operator=
(T st)
71
{
72
// return, if the final state is reached
73
if
(
_state
==
_final_state
)
return
_state
;
74
75
ibrcommon::MutexLock
l(*
this
);
76
_state
= st;
77
this->
signal
(
true
);
78
return
_state
;
79
}
80
81
bool
operator==
(T st)
const
82
{
83
return
(st ==
_state
);
84
}
85
86
bool
operator!=
(T st)
const
87
{
88
return
(st !=
_state
);
89
}
90
91
class
Locked
92
{
93
private
:
94
ThreadsafeState<T>
&_tss;
95
MutexLock
_lock;
96
97
public
:
98
Locked
(
ThreadsafeState<T>
&tss)
99
: _tss(tss), _lock(tss)
100
{
101
};
102
103
virtual
~Locked
()
104
{
105
_tss.signal(
true
);
106
};
107
108
T
get
()
const
109
{
110
return
_tss._state;
111
}
112
113
T
operator=
(T st)
114
{
115
// return, if the final state is reached
116
if
(_tss._state == _tss._final_state)
return
_tss._state;
117
118
_tss._state = st;
119
return
_tss._state;
120
}
121
122
bool
operator==
(T st)
123
{
124
return
(_tss._state == st);
125
}
126
127
bool
operator!=
(T st)
128
{
129
return
(_tss._state != st);
130
}
131
132
void
wait
(
size_t
st)
133
{
134
while
(!(_tss._state & st))
135
{
136
if
(_tss._state == _tss._final_state)
return
;
137
((
Conditional
&)_tss).wait();
138
}
139
}
140
141
void
wait
(T st)
142
{
143
while
(_tss._state != st)
144
{
145
if
(_tss._state == _tss._final_state)
return
;
146
((
Conditional
&)_tss).wait();
147
}
148
}
149
};
150
151
Locked
lock
()
152
{
153
return
*
this
;
154
}
155
};
156
}
157
158
#endif
/* THREADSAFESTATE_H_ */
ibrcommon
ibrcommon
thread
ThreadsafeState.h
Generated on Thu Mar 27 2014 09:26:21 for IBR-DTNSuite by
1.8.4