diff options
-rw-r--r-- | gnuradio-runtime/lib/top_block_impl.cc | 20 | ||||
-rw-r--r-- | gnuradio-runtime/lib/top_block_impl.h | 1 | ||||
-rwxr-xr-x | 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 48404a6832..3f94867bc2 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 1ac5136ddf..de0a67b4c1 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 97206a248d..3e780806bc 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") |