]> sigrok.org Git - pulseview.git/blobdiff - pv/sigsession.cpp
Some smaller whitespace fixes.
[pulseview.git] / pv / sigsession.cpp
index e8585c5f84db1465a8a482cbe6266eabc60e270f..e19291d40004d9bdaa5c7611eeaa1aa71e1f1e20 100644 (file)
@@ -36,7 +36,8 @@ namespace pv {
 // TODO: This should not be necessary
 SigSession* SigSession::_session = NULL;
 
-SigSession::SigSession()
+SigSession::SigSession() :
+       _capture_state(Stopped)
 {
        // TODO: This should not be necessary
        _session = this;
@@ -44,7 +45,9 @@ SigSession::SigSession()
 
 SigSession::~SigSession()
 {
-       if(_sampling_thread.get())
+       stop_capture();
+
+       if (_sampling_thread.get())
                _sampling_thread->join();
        _sampling_thread.reset();
 
@@ -63,20 +66,39 @@ void SigSession::load_file(const std::string &name)
        }
 }
 
+SigSession::capture_state SigSession::get_capture_state() const
+{
+       lock_guard<mutex> lock(_state_mutex);
+       return _capture_state;
+}
+
 void SigSession::start_capture(struct sr_dev_inst *sdi,
        uint64_t record_length, uint64_t sample_rate)
 {
-       // Check sampling isn't already active
-       if(_sampling_thread.get())
-               _sampling_thread->join();
+       stop_capture();
+
 
        _sampling_thread.reset(new boost::thread(
                &SigSession::sample_thread_proc, this, sdi,
                record_length, sample_rate));
 }
 
-vector< shared_ptr<view::Signal> >& SigSession::get_signals()
+void SigSession::stop_capture()
+{
+       if (get_capture_state() == Stopped)
+               return;
+
+       sr_session_stop();
+
+       // Check that sampling stopped
+       if (_sampling_thread.get())
+               _sampling_thread->join();
+       _sampling_thread.reset();
+}
+
+vector< shared_ptr<view::Signal> > SigSession::get_signals()
 {
+       lock_guard<mutex> lock(_signals_mutex);
        return _signals;
 }
 
@@ -85,6 +107,13 @@ boost::shared_ptr<LogicData> SigSession::get_data()
        return _logic_data;
 }
 
+void SigSession::set_capture_state(capture_state state)
+{
+       lock_guard<mutex> lock(_state_mutex);
+       _capture_state = state;
+       capture_state_changed(state);
+}
+
 void SigSession::sample_thread_proc(struct sr_dev_inst *sdi,
        uint64_t record_length, uint64_t sample_rate)
 {
@@ -116,8 +145,12 @@ void SigSession::sample_thread_proc(struct sr_dev_inst *sdi,
                return;
        }
 
+       set_capture_state(Running);
+
        sr_session_run();
        sr_session_destroy();
+
+       set_capture_state(Stopped);
 }
 
 void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
@@ -131,7 +164,7 @@ void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
        switch (packet->type) {
        case SR_DF_HEADER:
        {
-               lock_guard<mutex> lock(_data_mutex);
+               lock_guard<mutex> lock(_signals_mutex);
                _signals.clear();
                break;
        }
@@ -139,17 +172,21 @@ void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
        case SR_DF_META_LOGIC:
        {
                assert(packet->payload);
-
-               lock_guard<mutex> lock(_data_mutex);
-
                const sr_datafeed_meta_logic &meta_logic =
                        *(sr_datafeed_meta_logic*)packet->payload;
 
+       {
+               lock_guard<mutex> lock(_data_mutex);
+
                // Create an empty LogiData for coming data snapshots
                _logic_data.reset(new LogicData(meta_logic));
                assert(_logic_data);
-               if(!_logic_data)
+               if (!_logic_data)
                        break;
+       }
+
+       {
+               lock_guard<mutex> lock(_signals_mutex);
 
                // Add the signals
                for (int i = 0; i < meta_logic.num_probes; i++)
@@ -157,7 +194,7 @@ void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
                        const sr_probe *const probe =
                                (const sr_probe*)g_slist_nth_data(
                                        sdi->probes, i);
-                       if(probe->enabled)
+                       if (probe->enabled)
                        {
                                shared_ptr<LogicSignal> signal(
                                        new LogicSignal(probe->name,
@@ -170,12 +207,13 @@ void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
                signals_changed();
                break;
        }
+       }
 
        case SR_DF_LOGIC:
        {
                lock_guard<mutex> lock(_data_mutex);
                assert(packet->payload);
-               if(!_cur_logic_snapshot)
+               if (!_cur_logic_snapshot)
                {
                        // Create a new data snapshot
                        _cur_logic_snapshot = shared_ptr<LogicDataSnapshot>(
@@ -190,6 +228,7 @@ void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
                                *(sr_datafeed_logic*)packet->payload);
                }
 
+               data_updated();
                break;
        }