#include <gr_hier_block2_detail.h>
#include <iostream>
+#define GR_HIER_BLOCK2_DEBUG 1
+
gr_hier_block2_sptr gr_make_hier_block2(const std::string &name,
gr_io_signature_sptr input_signature,
gr_io_signature_sptr output_signature)
{
d_detail->disconnect(src, src_port, dst, dst_port);
}
+
+void
+gr_hier_block2::set_runtime(gr_runtime *runtime)
+{
+ if (GR_HIER_BLOCK2_DEBUG)
+ std::cout << "Setting runtime on " << this << " to " << runtime << std::endl;
+ d_detail->set_runtime(runtime);
+}
+
+void
+gr_hier_block2::lock()
+{
+ d_detail->lock();
+}
+
+void
+gr_hier_block2::unlock()
+{
+ d_detail->unlock();
+}
* \brief Private implementation details of gr_hier_block2
*/
gr_hier_block2_detail *d_detail;
-
+
+ /* Internal use only */
+ void set_runtime(gr_runtime *runtime);
+
protected:
gr_hier_block2(const std::string &name,
gr_io_signature_sptr input_signature,
gr_basic_block_sptr dst, int dst_port);
void disconnect(gr_basic_block_sptr src, int src_port,
gr_basic_block_sptr dst, int dst_port);
+ void lock();
+ void unlock();
};
#endif /* INCLUDED_GR_HIER_BLOCK2_H */
void disconnect(gr_basic_block_sptr src, int src_port,
gr_basic_block_sptr dst, int dst_port)
throw (std::invalid_argument);
+ void lock();
+ void unlock();
};
#include <gr_hier_block2_detail.h>
#include <gr_simple_flowgraph.h>
#include <gr_io_signature.h>
+#include <gr_runtime.h>
#include <stdexcept>
#include <iostream>
gr_hier_block2_detail::gr_hier_block2_detail(gr_hier_block2 *owner) :
d_owner(owner),
+ d_parent_detail(0),
d_fg(gr_make_simple_flowgraph()),
d_inputs(owner->input_signature()->max_streams()),
- d_outputs(owner->output_signature()->max_streams())
+ d_outputs(owner->output_signature()->max_streams()),
+ d_runtime()
{
}
if (src.get() == dst.get())
throw std::invalid_argument("src and destination blocks cannot be the same");
+ gr_hier_block2_sptr src_block(boost::dynamic_pointer_cast<gr_hier_block2, gr_basic_block>(src));
+ gr_hier_block2_sptr dst_block(boost::dynamic_pointer_cast<gr_hier_block2, gr_basic_block>(dst));
+
+ if (src_block && src.get() != d_owner) {
+ if (GR_HIER_BLOCK2_DETAIL_DEBUG)
+ std::cout << "connect: src is hierarchical, setting parent to " << this << std::endl;
+ src_block->d_detail->d_parent_detail = this;
+ }
+
+ if (dst_block && dst.get() != d_owner) {
+ if (GR_HIER_BLOCK2_DETAIL_DEBUG)
+ std::cout << "connect: dst is hierarchical, setting parent to " << this << std::endl;
+ dst_block->d_detail->d_parent_detail = this;
+ }
+
// Connections to block inputs or outputs
int max_port;
if (src.get() == d_owner) {
if (src.get() == dst.get())
throw std::invalid_argument("src and destination blocks cannot be the same");
+ gr_hier_block2_sptr src_block(boost::dynamic_pointer_cast<gr_hier_block2, gr_basic_block>(src));
+ gr_hier_block2_sptr dst_block(boost::dynamic_pointer_cast<gr_hier_block2, gr_basic_block>(dst));
+
+ if (src_block && src.get() != d_owner) {
+ if (GR_HIER_BLOCK2_DETAIL_DEBUG)
+ std::cout << "connect: src is hierarchical, clearing parent" << std::endl;
+ src_block->d_detail->d_parent_detail = 0;
+ }
+
+ if (dst_block && dst.get() != d_owner) {
+ if (GR_HIER_BLOCK2_DETAIL_DEBUG)
+ std::cout << "connect: dst is hierarchical, clearing parent" << std::endl;
+ dst_block->d_detail->d_parent_detail = 0;
+ }
+
if (src.get() == d_owner)
return disconnect_input(src_port, dst_port, dst);
hier_block2->d_detail->flatten(sfg);
}
}
+
+void
+gr_hier_block2_detail::lock()
+{
+ if (GR_HIER_BLOCK2_DETAIL_DEBUG)
+ std::cout << "lock: entered in " << this << std::endl;
+
+ if (d_parent_detail)
+ d_parent_detail->lock();
+ else
+ if (d_runtime)
+ d_runtime->lock();
+}
+
+void
+gr_hier_block2_detail::unlock()
+{
+ if (GR_HIER_BLOCK2_DETAIL_DEBUG)
+ std::cout << "unlock: entered in " << this << std::endl;
+
+ if (d_parent_detail)
+ d_parent_detail->unlock();
+ else
+ if (d_runtime)
+ d_runtime->unlock();
+}
// Private implementation data
gr_hier_block2 *d_owner;
+ gr_hier_block2_detail *d_parent_detail;
gr_simple_flowgraph_sptr d_fg;
gr_endpoint_vector_t d_inputs;
gr_endpoint_vector_t d_outputs;
-
+ gr_runtime *d_runtime;
+
// Private implementation methods
void connect(gr_basic_block_sptr src, int src_port,
gr_basic_block_sptr dst, int dst_port);
void flatten(gr_simple_flowgraph_sptr sfg);
gr_endpoint resolve_port(int port, bool is_input);
gr_endpoint resolve_endpoint(const gr_endpoint &endp, bool is_input);
+ void set_runtime(gr_runtime *runtime) { d_runtime = runtime; }
+ void lock();
+ void unlock();
public:
~gr_hier_block2_detail();
gr_runtime::gr_runtime(gr_hier_block2_sptr top_block)
{
- d_impl = new gr_runtime_impl(top_block);
+ d_impl = new gr_runtime_impl(top_block, this);
}
gr_runtime::~gr_runtime()
{
d_impl->restart();
}
+
+void
+gr_runtime::lock()
+{
+ d_impl->lock();
+}
+
+void
+gr_runtime::unlock()
+{
+ d_impl->unlock();
+}
+
* recreates new threads (possibly a different number from before.)
*/
void restart();
+
+ /*!
+ * Lock a flow graph in preparation for reconfiguration. When an equal
+ * number of calls to lock() and unlock() have occurred, the flow graph
+ * will be restarted automatically.
+ *
+ * N.B. lock() and unlock() cannot be called from a flow graph thread or
+ * deadlock will occur when reconfiguration happens.
+ */
+ void lock();
+
+ /*!
+ * Lock a flow graph in preparation for reconfiguration. When an equal
+ * number of calls to lock() and unlock() have occurred, the flow graph
+ * will be restarted automatically.
+ *
+ * N.B. lock() and unlock() cannot be called from a flow graph thread or
+ * deadlock will occur when reconfiguration happens.
+ */
+ void unlock();
};
#endif /* INCLUDED_GR_RUNTIME_H */
s_runtime->stop();
}
-gr_runtime_impl::gr_runtime_impl(gr_hier_block2_sptr top_block)
+gr_runtime_impl::gr_runtime_impl(gr_hier_block2_sptr top_block, gr_runtime *owner)
: d_running(false),
d_top_block(top_block),
- d_sfg(gr_make_simple_flowgraph())
+ d_sfg(gr_make_simple_flowgraph()),
+ d_owner(owner)
{
s_runtime = this;
+ top_block->set_runtime(d_owner);
}
gr_runtime_impl::~gr_runtime_impl()
{
- s_runtime = 0; // we don't own this
+ s_runtime = 0; // don't call delete we don't own these
+ d_owner = 0;
}
void
d_threads.clear();
}
+
+// N.B. lock() and unlock() cannot be called from a flow graph thread or
+// deadlock will occur when reconfiguration happens
+void
+gr_runtime_impl::lock()
+{
+ omni_mutex_lock lock(d_reconf);
+ d_lock_count++;
+ if (GR_RUNTIME_IMPL_DEBUG)
+ std::cout << "runtime: locked, count = " << d_lock_count << std::endl;
+}
+
+void
+gr_runtime_impl::unlock()
+{
+ omni_mutex_lock lock(d_reconf);
+ if (d_lock_count == 0)
+ throw std::runtime_error("unpaired unlock() call");
+
+ d_lock_count--;
+ if (GR_RUNTIME_IMPL_DEBUG)
+ std::cout << "runtime: unlocked, count = " << d_lock_count << std::endl;
+
+ if (d_lock_count == 0)
+ restart();
+}
+
void
gr_runtime_impl::restart()
{
{
d_sts->stop();
}
-
class gr_runtime_impl
{
private:
- gr_runtime_impl(gr_hier_block2_sptr top_block);
+ gr_runtime_impl(gr_hier_block2_sptr top_block, gr_runtime *owner);
friend void runtime_sigint_handler(int signum);
friend class gr_runtime;
gr_simple_flowgraph_sptr d_sfg;
std::vector<gr_block_vector_t> d_graphs;
gr_scheduler_thread_vector_t d_threads;
-
+ gr_runtime *d_owner;
+ int d_lock_count;
+ omni_mutex d_reconf;
+
void start();
void start_threads();
void stop();
void wait();
void restart();
+ void lock();
+ void unlock();
public:
~gr_runtime_impl();