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
|
/* -*- c++ -*- */
/*
* Copyright 2006,2012-2013 Free Software Foundation, Inc.
*
* This file is part of GNU Radio
*
* SPDX-License-Identifier: GPL-3.0-or-later
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <gnuradio/basic_block.h>
#include <gnuradio/block_registry.h>
#include <gnuradio/logger.h>
#include <iostream>
#include <sstream>
#include <stdexcept>
namespace gr {
static long s_next_id = 0;
static long s_ncurrently_allocated = 0;
long basic_block_ncurrently_allocated() { return s_ncurrently_allocated; }
basic_block::basic_block(const std::string& name,
io_signature::sptr input_signature,
io_signature::sptr output_signature)
: d_name(name),
d_input_signature(input_signature),
d_output_signature(output_signature),
d_unique_id(s_next_id++),
d_symbolic_id(global_block_registry.block_register(this)),
d_symbol_name(global_block_registry.register_symbolic_name(this)),
d_color(WHITE),
d_rpc_set(false),
d_message_subscribers(pmt::make_dict())
{
configure_default_loggers(d_logger, d_debug_logger, d_symbol_name);
s_ncurrently_allocated++;
}
basic_block::~basic_block()
{
s_ncurrently_allocated--;
global_block_registry.block_unregister(this);
}
basic_block_sptr basic_block::to_basic_block() { return shared_from_this(); }
void basic_block::set_block_alias(std::string name)
{
// Only keep one alias'd name around for each block. If we don't
// have an alias, add it; if we do, update the entry in the
// registry.
if (alias_set())
global_block_registry.update_symbolic_name(this, name);
else
global_block_registry.register_symbolic_name(this, name);
// set the block's alias
d_symbol_alias = name;
update_logger_alias(symbol_name(), d_symbol_alias);
}
// ** Message passing interface **
// - register a new input message port
void basic_block::message_port_register_in(pmt::pmt_t port_id)
{
if (!pmt::is_symbol(port_id)) {
throw std::runtime_error("message_port_register_in: bad port id");
}
msg_queue[port_id] = msg_queue_t();
msg_queue_ready[port_id] =
std::shared_ptr<boost::condition_variable>(new boost::condition_variable());
}
pmt::pmt_t basic_block::message_ports_in()
{
pmt::pmt_t port_names = pmt::make_vector(msg_queue.size(), pmt::PMT_NIL);
msg_queue_map_itr itr = msg_queue.begin();
for (size_t i = 0; i < msg_queue.size(); i++) {
pmt::vector_set(port_names, i, (*itr).first);
itr++;
}
return port_names;
}
// - register a new output message port
void basic_block::message_port_register_out(pmt::pmt_t port_id)
{
if (!pmt::is_symbol(port_id)) {
throw std::runtime_error("message_port_register_out: bad port id");
}
if (pmt::dict_has_key(d_message_subscribers, port_id)) {
throw std::runtime_error("message_port_register_out: port already in use");
}
d_message_subscribers = pmt::dict_add(d_message_subscribers, port_id, pmt::PMT_NIL);
}
pmt::pmt_t basic_block::message_ports_out()
{
size_t len = pmt::length(d_message_subscribers);
pmt::pmt_t port_names = pmt::make_vector(len, pmt::PMT_NIL);
pmt::pmt_t keys = pmt::dict_keys(d_message_subscribers);
for (size_t i = 0; i < len; i++) {
pmt::vector_set(port_names, i, pmt::nth(i, keys));
}
return port_names;
}
// - publish a message on a message port
void basic_block::message_port_pub(pmt::pmt_t port_id, pmt::pmt_t msg)
{
if (!pmt::dict_has_key(d_message_subscribers, port_id)) {
throw std::runtime_error("port does not exist");
}
pmt::pmt_t currlist = pmt::dict_ref(d_message_subscribers, port_id, pmt::PMT_NIL);
// iterate through subscribers on port
while (pmt::is_pair(currlist)) {
pmt::pmt_t target = pmt::car(currlist);
pmt::pmt_t block = pmt::car(target);
pmt::pmt_t port = pmt::cdr(target);
currlist = pmt::cdr(currlist);
basic_block_sptr blk = global_block_registry.block_lookup(block);
// blk->post(msg);
blk->post(port, msg);
}
}
// - subscribe to a message port
void basic_block::message_port_sub(pmt::pmt_t port_id, pmt::pmt_t target)
{
if (!pmt::dict_has_key(d_message_subscribers, port_id)) {
std::stringstream ss;
ss << "Port does not exist: \"" << pmt::write_string(port_id)
<< "\" on block: " << pmt::write_string(target) << std::endl;
throw std::runtime_error(ss.str());
}
pmt::pmt_t currlist = pmt::dict_ref(d_message_subscribers, port_id, pmt::PMT_NIL);
// ignore re-adds of the same target
if (!pmt::list_has(currlist, target))
d_message_subscribers = pmt::dict_add(
d_message_subscribers, port_id, pmt::list_add(currlist, target));
}
void basic_block::message_port_unsub(pmt::pmt_t port_id, pmt::pmt_t target)
{
if (!pmt::dict_has_key(d_message_subscribers, port_id)) {
std::stringstream ss;
ss << "Port does not exist: \"" << pmt::write_string(port_id)
<< "\" on block: " << pmt::write_string(target) << std::endl;
throw std::runtime_error(ss.str());
}
// ignore unsubs of unknown targets
pmt::pmt_t currlist = pmt::dict_ref(d_message_subscribers, port_id, pmt::PMT_NIL);
d_message_subscribers =
pmt::dict_add(d_message_subscribers, port_id, pmt::list_rm(currlist, target));
}
void basic_block::_post(pmt::pmt_t which_port, pmt::pmt_t msg)
{
insert_tail(which_port, msg);
}
void basic_block::insert_tail(pmt::pmt_t which_port, pmt::pmt_t msg)
{
gr::thread::scoped_lock guard(mutex);
if ((msg_queue.find(which_port) == msg_queue.end()) ||
(msg_queue_ready.find(which_port) == msg_queue_ready.end())) {
GR_LOG_ERROR(d_logger,
std::string("attempted insertion on invalid queue ") +
pmt::symbol_to_string(which_port));
throw std::runtime_error("attempted to insert_tail on invalid queue!");
}
msg_queue[which_port].push_back(msg);
msg_queue_ready[which_port]->notify_one();
// wake up thread if BLKD_IN or BLKD_OUT
global_block_registry.notify_blk(d_symbol_name);
}
pmt::pmt_t basic_block::delete_head_nowait(pmt::pmt_t which_port)
{
gr::thread::scoped_lock guard(mutex);
if (empty_p(which_port)) {
return pmt::pmt_t();
}
pmt::pmt_t m(msg_queue[which_port].front());
msg_queue[which_port].pop_front();
return m;
}
pmt::pmt_t basic_block::message_subscribers(pmt::pmt_t port)
{
return pmt::dict_ref(d_message_subscribers, port, pmt::PMT_NIL);
}
} /* namespace gr */
|