Open Lighting Architecture
0.10.9
Loading...
Searching...
No Matches
include
ola
thread
FuturePrivate.h
1
/*
2
* This library is free software; you can redistribute it and/or
3
* modify it under the terms of the GNU Lesser General Public
4
* License as published by the Free Software Foundation; either
5
* version 2.1 of the License, or (at your option) any later version.
6
*
7
* This library is distributed in the hope that it will be useful,
8
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10
* Lesser General Public License for more details.
11
*
12
* You should have received a copy of the GNU Lesser General Public
13
* License along with this library; if not, write to the Free Software
14
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15
*
16
* FuturePrivate.h
17
* An experimental implementation of Futures.
18
* Copyright (C) 2010 Simon Newton
19
*/
20
21
#ifndef INCLUDE_OLA_THREAD_FUTUREPRIVATE_H_
22
#define INCLUDE_OLA_THREAD_FUTUREPRIVATE_H_
23
24
#include <
ola/Logging.h
>
25
#include <
ola/base/Macro.h
>
26
#include <ola/thread/Mutex.h>
27
28
namespace
ola
{
29
namespace
thread {
30
31
template
<
typename
T>
32
class
FutureImpl
{
33
public
:
34
FutureImpl
()
35
: m_ref_count(1),
36
m_is_set(
false
),
37
m_value() {
38
}
39
40
void
Ref() {
41
{
42
MutexLocker
l(&m_mutex);
43
m_ref_count++;
44
}
45
}
46
47
void
DeRef() {
48
unsigned
int
ref_count = 0;
49
{
50
MutexLocker
l(&m_mutex);
51
ref_count = --m_ref_count;
52
}
53
if
(ref_count == 0) {
54
delete
this
;
55
}
56
}
57
58
bool
IsComplete()
const
{
59
MutexLocker
l(&m_mutex);
60
return
m_is_set;
61
}
62
63
const
T& Get()
const
{
64
MutexLocker
l(&m_mutex);
65
if
(m_is_set) {
66
return
m_value;
67
}
68
m_condition.
Wait
(&m_mutex);
69
return
m_value;
70
}
71
72
void
Set(
const
T &t) {
73
{
74
MutexLocker
l(&m_mutex);
75
if
(m_is_set) {
76
OLA_FATAL
<<
"Double call to FutureImpl::Set()"
;
77
return
;
78
}
79
m_is_set =
true
;
80
m_value = t;
81
}
82
m_condition.
Broadcast
();
83
}
84
85
private
:
86
mutable
Mutex
m_mutex;
87
mutable
ConditionVariable
m_condition;
88
unsigned
int
m_ref_count;
89
bool
m_is_set;
90
T m_value;
91
92
FutureImpl
(
const
FutureImpl
&) =
delete
;
93
const
FutureImpl
&operator=(
const
FutureImpl
&) =
delete
;
94
};
95
99
template
<>
100
class
FutureImpl
<void> {
101
public
:
102
FutureImpl
()
103
: m_ref_count(1),
104
m_is_set(
false
) {
105
}
106
107
void
Ref() {
108
{
109
MutexLocker
l(&m_mutex);
110
m_ref_count++;
111
}
112
}
113
114
void
DeRef() {
115
unsigned
int
ref_count = 0;
116
{
117
MutexLocker
l(&m_mutex);
118
ref_count = --m_ref_count;
119
}
120
if
(ref_count == 0) {
121
delete
this
;
122
}
123
}
124
125
bool
IsComplete()
const
{
126
MutexLocker
l(&m_mutex);
127
return
m_is_set;
128
}
129
130
void
Get()
const
{
131
MutexLocker
l(&m_mutex);
132
if
(m_is_set) {
133
return
;
134
}
135
m_condition.
Wait
(&m_mutex);
136
}
137
138
void
Set() {
139
{
140
MutexLocker
l(&m_mutex);
141
if
(m_is_set) {
142
OLA_FATAL
<<
"Double call to FutureImpl::Set()"
;
143
return
;
144
}
145
m_is_set =
true
;
146
}
147
m_condition.
Broadcast
();
148
}
149
150
private
:
151
mutable
Mutex
m_mutex;
152
mutable
ConditionVariable
m_condition;
153
unsigned
int
m_ref_count;
154
bool
m_is_set;
155
156
FutureImpl
(
const
FutureImpl
&) =
delete
;
157
const
FutureImpl
&operator=(
const
FutureImpl
&) =
delete
;
158
};
159
160
}
// namespace thread
161
}
// namespace ola
162
#endif
// INCLUDE_OLA_THREAD_FUTUREPRIVATE_H_
Logging.h
Header file for OLA Logging.
Macro.h
Helper macros.
ola::thread::ConditionVariable
Definition
Mutex.h:83
ola::thread::ConditionVariable::Broadcast
void Broadcast()
Definition
Mutex.cpp:145
ola::thread::ConditionVariable::Wait
void Wait(Mutex *mutex)
Definition
Mutex.cpp:112
ola::thread::FutureImpl
Definition
FuturePrivate.h:32
ola::thread::Mutex
Definition
Mutex.h:41
ola::thread::MutexLocker
Definition
Mutex.h:64
OLA_FATAL
#define OLA_FATAL
Definition
Logging.h:65
ola
The namespace containing all OLA symbols.
Definition
Credentials.cpp:44
Generated on Sat Aug 24 2024 09:00:45 for Open Lighting Architecture by
1.12.0