1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
// Copyright (C) by Josh Blum. See LICENSE.txt for licensing information.
#include "element_impl.hpp"
#include <gras/block.hpp>
#include <boost/thread/thread.hpp> //sleep
#include <iostream>
using namespace gras;
InputPortConfig::InputPortConfig(void)
{
item_size = 1;
reserve_items = 1;
maximum_items = 0;
inline_buffer = false;
preload_items = 0;
force_done = true;
}
OutputPortConfig::OutputPortConfig(void)
{
item_size = 1;
reserve_items = 1;
maximum_items = 0;
}
Block::Block(void)
{
//NOP
}
Block::Block(const std::string &name):
Element(name)
{
//create non-actor containers
(*this)->block_data.reset(new BlockData());
(*this)->block_data->block = this;
(*this)->worker.reset(new Apology::Worker());
//create actor and init members
(*this)->block_actor.reset(new BlockActor());
(*this)->setup_actor();
//setup some state variables
(*this)->block_data->block_state = BLOCK_STATE_INIT;
//call block methods to init stuff
this->set_interruptible_work(false);
this->set_buffer_affinity(-1);
}
Block::~Block(void)
{
//NOP
}
void ElementImpl::setup_actor(void)
{
this->block_actor->worker = this->worker.get();
this->block_actor->name = name; //for debug purposes
this->block_actor->data = this->block_data;
this->worker->set_actor(this->block_actor.get());
this->thread_pool = this->block_actor->thread_pool; //ref copy of pool
}
enum block_cleanup_state_type
{
BLOCK_CLEANUP_WAIT,
BLOCK_CLEANUP_WARN,
BLOCK_CLEANUP_DAMN,
BLOCK_CLEANUP_DOTS,
};
static void wait_actor_idle(const std::string &repr, Theron::Actor &actor)
{
const boost::system_time start = boost::get_system_time();
block_cleanup_state_type state = BLOCK_CLEANUP_WAIT;
while (actor.GetNumQueuedMessages())
{
boost::this_thread::sleep(boost::posix_time::milliseconds(1));
switch (state)
{
case BLOCK_CLEANUP_WAIT:
if (boost::get_system_time() > start + boost::posix_time::seconds(1))
{
std::cerr << repr << ", waiting for you to finish." << std::endl;
state = BLOCK_CLEANUP_WARN;
}
break;
case BLOCK_CLEANUP_WARN:
if (boost::get_system_time() > start + boost::posix_time::seconds(2))
{
std::cerr << repr << ", give up the thread context!" << std::endl;
state = BLOCK_CLEANUP_DAMN;
}
break;
case BLOCK_CLEANUP_DAMN:
if (boost::get_system_time() > start + boost::posix_time::seconds(3))
{
std::cerr << repr << " FAIL; application will now hang..." << std::endl;
state = BLOCK_CLEANUP_DOTS;
}
break;
case BLOCK_CLEANUP_DOTS: break;
}
}
}
void ElementImpl::block_cleanup(void)
{
//wait for actor to chew through enqueued messages
wait_actor_idle(this->repr, *this->block_actor);
//delete the actor
this->block_actor.reset();
//unref actor's framework
this->thread_pool.reset(); //must be deleted after actor
}
template <typename V>
const typename V::value_type &vector_get_const(const V &v, const size_t index)
{
if (v.size() <= index)
{
return v.back();
}
return v[index];
}
template <typename V>
typename V::value_type &vector_get_resize(V &v, const size_t index)
{
if (v.size() <= index)
{
if (v.empty()) v.resize(1);
v.resize(index+1, v.back());
}
return v[index];
}
InputPortConfig &Block::input_config(const size_t which_input)
{
return vector_get_resize((*this)->block_data->input_configs, which_input);
}
const InputPortConfig &Block::input_config(const size_t which_input) const
{
return vector_get_const((*this)->block_data->input_configs, which_input);
}
OutputPortConfig &Block::output_config(const size_t which_output)
{
return vector_get_resize((*this)->block_data->output_configs, which_output);
}
const OutputPortConfig &Block::output_config(const size_t which_output) const
{
return vector_get_const((*this)->block_data->output_configs, which_output);
}
void Block::commit_config(void)
{
Theron::Actor &actor = *((*this)->block_actor);
for (size_t i = 0; i < (*this)->worker->get_num_inputs(); i++)
{
InputUpdateMessage message;
message.index = i;
actor.GetFramework().Send(message, Theron::Address::Null(), actor.GetAddress());
}
for (size_t i = 0; i < (*this)->worker->get_num_outputs(); i++)
{
OutputUpdateMessage message;
message.index = i;
actor.GetFramework().Send(message, Theron::Address::Null(), actor.GetAddress());
}
}
void Block::notify_active(void)
{
//NOP
}
void Block::notify_inactive(void)
{
//NOP
}
void Block::notify_topology(const size_t, const size_t)
{
return;
}
void Block::set_thread_pool(const ThreadPool &thread_pool)
{
boost::shared_ptr<BlockActor> old_actor = (*this)->block_actor;
(*this)->block_actor.reset(new BlockActor(thread_pool));
(*this)->setup_actor();
wait_actor_idle((*this)->repr, *old_actor);
}
void Block::set_buffer_affinity(const long affinity)
{
(*this)->block_data->buffer_affinity = affinity;
}
void Block::set_interruptible_work(const bool enb)
{
(*this)->block_data->interruptible_work = enb;
}
|