summaryrefslogtreecommitdiff
path: root/lib/block.cpp
blob: 716873b99c20f7f8fc077ced7f176e3b6cfad069 (plain)
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// Copyright (C) by Josh Blum. See LICENSE.txt for licensing information.

#include "element_impl.hpp"
#include <gras/block.hpp>
#include <boost/foreach.hpp>
#include <boost/thread/thread.hpp> //yield

using namespace gras;

InputPortConfig::InputPortConfig(void)
{
    reserve_items = 1;
    maximum_items = 0;
    inline_buffer = false;
    preload_items = 0;
}

OutputPortConfig::OutputPortConfig(void)
{
    reserve_items = 1;
    maximum_items = 0;
}

Block::Block(void)
{
    //NOP
}

Block::Block(const std::string &name):
    Element(name)
{
    (*this)->block.reset(new BlockActor());
    (*this)->thread_pool = (*this)->block->thread_pool; //ref copy of pool
    (*this)->block->name = name; //for debug purposes

    //setup some state variables
    (*this)->block->block_ptr = this;
    (*this)->block->block_state = BlockActor::BLOCK_STATE_INIT;

    //call block methods to init stuff
    this->set_input_config(0, InputPortConfig());
    this->set_output_config(0, OutputPortConfig());
    this->set_interruptible_work(false);
    this->set_buffer_affinity(-1);
}

void ElementImpl::block_cleanup(void)
{
    //wait for actor to chew through enqueued messages
    while (this->block->GetNumQueuedMessages())
    {
        //TODO timeout if this does not stop
        boost::this_thread::yield();
    }

    //delete the actor
    this->block.reset();

    //unref actor's framework
    this->thread_pool.reset(); //must be deleted after actor
}

template <typename V, typename T>
void vector_set(V &v, const T &t, const size_t index)
{
    if (v.size() <= index)
    {
        v.resize(index+1, t);
    }
    v[index] = t;
}

template <typename V>
typename V::value_type vector_get(const V &v, const size_t index)
{
    if (v.size() <= index)
    {
        return v.front();
    }
    return v[index];
}

InputPortConfig Block::get_input_config(const size_t which_input) const
{
    return vector_get((*this)->block->input_configs, which_input);
}

void Block::set_input_config(const size_t which_input, const InputPortConfig &config)
{
    vector_set((*this)->block->input_configs, config, which_input);
    {
        InputUpdateMessage message;
        message.index = which_input;
        (*this)->block->Push(message, Theron::Address());
    }
}

OutputPortConfig Block::get_output_config(const size_t which_output) const
{
    return vector_get((*this)->block->output_configs, which_output);
}

void Block::set_output_config(const size_t which_output, const OutputPortConfig &config)
{
    vector_set((*this)->block->output_configs, config, which_output);
    {
        OutputUpdateMessage message;
        message.index = which_output;
        (*this)->block->Push(message, Theron::Address());
    }
}

void Block::consume(const size_t which_input, const size_t num_items)
{
    ASSERT(long(num_items) >= 0); //sign bit set? you dont want a negative
    (*this)->block->consume(which_input, num_items);
}

void Block::produce(const size_t which_output, const size_t num_items)
{
    ASSERT(long(num_items) >= 0); //sign bit set? you dont want a negative
    (*this)->block->produce(which_output, num_items);
}

void Block::consume(const size_t num_items)
{
    const size_t num_inputs = (*this)->block->get_num_inputs();
    for (size_t i = 0; i < num_inputs; i++)
    {
        (*this)->block->consume(i, num_items);
    }
}

void Block::produce(const size_t num_items)
{
    const size_t num_outputs = (*this)->block->get_num_outputs();
    for (size_t o = 0; o < num_outputs; o++)
    {
        (*this)->block->produce(o, num_items);
    }
}

item_index_t Block::get_consumed(const size_t which_input)
{
    return (*this)->block->items_consumed[which_input];
}

item_index_t Block::get_produced(const size_t which_output)
{
    return (*this)->block->items_produced[which_output];
}

void Block::post_output_tag(const size_t which_output, const Tag &tag)
{
    (*this)->block->post_downstream(which_output, InputTagMessage(tag));
}

TagIter Block::get_input_tags(const size_t which_input)
{
    const std::vector<Tag> &input_tags = (*this)->block->input_tags[which_input];
    return TagIter(input_tags.begin(), input_tags.end());
}

void Block::post_output_msg(const size_t which_output, const PMCC &msg)
{
    this->post_output_tag(which_output, Tag(0, msg));
}

PMCC Block::pop_input_msg(const size_t which_input)
{
    std::vector<Tag> &input_tags = (*this)->block->input_tags[which_input];
    if (input_tags.empty()) return PMCC();
    PMCC p = input_tags.front().object;
    input_tags.erase(input_tags.begin());
    return p;
}

void Block::propagate_tags(const size_t i, const TagIter &iter)
{
    const size_t num_outputs = (*this)->block->get_num_outputs();
    for (size_t o = 0; o < num_outputs; o++)
    {
        BOOST_FOREACH(gras::Tag t, iter)
        {
            t.offset -= this->get_consumed(i);
            t.offset += this->get_produced(o);
            this->post_output_tag(o, t);
        }
    }
}

bool Block::start(void)
{
    return true;
}

bool Block::stop(void)
{
    return true;
}

void Block::notify_topology(const size_t, const size_t)
{
    return;
}

void Block::set_buffer_affinity(const long affinity)
{
    (*this)->block->buffer_affinity = affinity;
}

void Block::set_interruptible_work(const bool enb)
{
    (*this)->block->interruptible_work = enb;
}

void Block::mark_output_fail(const size_t which_output)
{
    (*this)->block->output_fail(which_output);
}

void Block::mark_input_fail(const size_t which_input)
{
    (*this)->block->input_fail(which_input);
}

void Block::mark_done(void)
{
    (*this)->block->mark_done();
}

SBuffer Block::get_input_buffer(const size_t which_input) const
{
    return (*this)->block->input_queues.front(which_input);
}

SBuffer Block::get_output_buffer(const size_t which_output) const
{
    SBuffer &buff = (*this)->block->output_queues.front(which_output);
    //increment length to auto pop full buffer size,
    //when user doesnt call pop_output_buffer()
    buff.length = buff.get_actual_length();
    return buff;
}

void Block::pop_output_buffer(const size_t which_output, const size_t num_bytes)
{
    (*this)->block->output_queues.front(which_output).length = num_bytes;
}

void Block::post_output_buffer(const size_t which_output, const SBuffer &buffer)
{
    (*this)->block->produce_buffer(which_output, buffer);
}