[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Commit-gnuradio] [gnuradio] 01/01: runtime: fix state handling in top_b
From: |
git |
Subject: |
[Commit-gnuradio] [gnuradio] 01/01: runtime: fix state handling in top_block |
Date: |
Sat, 25 Jun 2016 19:08:00 +0000 (UTC) |
This is an automated email from the git hooks/post-receive script.
jcorgan pushed a commit to branch maint
in repository gnuradio.
commit a99f8ac8cc9a15ab608081c10a7733fce125e276
Author: Jiří Pinkava <address@hidden>
Date: Thu Apr 9 08:55:04 2015 +0200
runtime: fix state handling in top_block
d_state is intended to track running state of top block.
* initial state is IDLE
* set state to RUNNING when start() is called
* set state to IDLE when stop() is called
* set state to IDLE when all work is done (reqires call of wait())
* unlock() resume/start execution only if state is RUNNING
---
gnuradio-runtime/lib/top_block_impl.cc | 20 ++++++++++++++------
gnuradio-runtime/lib/top_block_impl.h | 1 +
gr-blocks/python/blocks/qa_hier_block2.py | 26 +++++++++++++++++++++++++-
3 files changed, 40 insertions(+), 7 deletions(-)
diff --git a/gnuradio-runtime/lib/top_block_impl.cc
b/gnuradio-runtime/lib/top_block_impl.cc
index 48404a6..3f94867 100644
--- a/gnuradio-runtime/lib/top_block_impl.cc
+++ b/gnuradio-runtime/lib/top_block_impl.cc
@@ -80,7 +80,7 @@ namespace gr {
top_block_impl::top_block_impl(top_block *owner)
: d_owner(owner), d_ffg(),
- d_state(IDLE), d_lock_count(0)
+ d_state(IDLE), d_lock_count(0), d_retry_wait(false)
{
}
@@ -125,8 +125,12 @@ namespace gr {
void
top_block_impl::stop()
{
+ gr::thread::scoped_lock lock(d_mutex);
+
if(d_scheduler)
d_scheduler->stop();
+
+ d_state = IDLE;
}
void
@@ -137,6 +141,11 @@ namespace gr {
{
gr::thread::scoped_lock lock(d_mutex);
if (!d_lock_count) {
+ if(d_retry_wait) {
+ d_retry_wait = false;
+ continue;
+ }
+ d_state = IDLE;
break;
}
d_lock_cond.wait(lock);
@@ -149,8 +158,6 @@ namespace gr {
{
if(d_scheduler)
d_scheduler->wait();
-
- d_state = IDLE;
}
// N.B. lock() and unlock() cannot be called from a flow graph
@@ -159,7 +166,8 @@ namespace gr {
top_block_impl::lock()
{
gr::thread::scoped_lock lock(d_mutex);
- stop();
+ if(d_scheduler)
+ d_scheduler->stop();
d_lock_count++;
}
@@ -177,8 +185,8 @@ namespace gr {
if(d_lock_count > 0 || d_state == IDLE) // nothing to do
return;
- d_lock_cond.notify_all();
restart();
+ d_lock_cond.notify_all();
}
/*
@@ -197,7 +205,7 @@ namespace gr {
// Create a new scheduler to execute it
d_scheduler = make_scheduler(d_ffg, d_max_noutput_items);
- d_state = RUNNING;
+ d_retry_wait = true;
}
std::string
diff --git a/gnuradio-runtime/lib/top_block_impl.h
b/gnuradio-runtime/lib/top_block_impl.h
index 1ac5136..de0a67b 100644
--- a/gnuradio-runtime/lib/top_block_impl.h
+++ b/gnuradio-runtime/lib/top_block_impl.h
@@ -82,6 +82,7 @@ namespace gr {
gr::thread::mutex d_mutex; // protects d_state and d_lock_count
tb_state d_state;
int d_lock_count;
+ bool d_retry_wait;
boost::condition_variable d_lock_cond;
int d_max_noutput_items;
diff --git a/gr-blocks/python/blocks/qa_hier_block2.py
b/gr-blocks/python/blocks/qa_hier_block2.py
index 97206a2..3e78080 100755
--- a/gr-blocks/python/blocks/qa_hier_block2.py
+++ b/gr-blocks/python/blocks/qa_hier_block2.py
@@ -2,6 +2,7 @@
from gnuradio import gr, gr_unittest, blocks
import numpy
+import threading
import time
class add_ff(gr.sync_block):
@@ -427,7 +428,7 @@ class test_hier_block2(gr_unittest.TestCase):
procs = hblock.processor_affinity()
self.assertEquals((0,), procs)
- def test_lock_unlock(self):
+ def test_34a_lock_unlock(self):
hblock = gr.top_block("test_block")
src = blocks.null_source(gr.sizeof_float)
throttle = blocks.throttle(gr.sizeof_float, 32000)
@@ -442,5 +443,28 @@ class test_hier_block2(gr_unittest.TestCase):
hblock.stop()
hblock.wait()
+ def test_34b_lock_unlock(self):
+ hblock = gr.top_block("test_block")
+ src = blocks.null_source(gr.sizeof_float)
+ throttle = blocks.throttle(gr.sizeof_float, 32000)
+ sink = blocks.null_sink(gr.sizeof_float)
+ hblock.connect(src, throttle, sink)
+ hblock.set_processor_affinity([0,])
+ def thread_01(hblock, cls):
+ cls.test_34b_val = 10
+ hblock.lock()
+ cls.test_34b_val = 20
+ hblock.unlock()
+ cls.test_34b_val = 30
+ time.sleep(0.5)
+ cls.test_34b_val = 40
+ hblock.stop()
+ hblock.start()
+ self.test_34b_val = 0
+ t1 = threading.Thread(target=thread_01, args=(hblock, self, ))
+ t1.start()
+ hblock.wait()
+ self.assertEqual(40, self.test_34b_val)
+
if __name__ == "__main__":
gr_unittest.run(test_hier_block2, "test_hier_block2.xml")