]> sigrok.org Git - pulseview.git/commitdiff
Session: Keep track of signal data locally
authorSoeren Apel <redacted>
Thu, 19 May 2016 18:29:37 +0000 (20:29 +0200)
committerSoeren Apel <redacted>
Thu, 19 May 2016 18:29:37 +0000 (20:29 +0200)
Currently, the Session class doesn't hold pointers to the signal
data itself but instead fetches the data on demand using the list
of signals it has. As the Session is providing the model in the
MVC paradigm, it owns the data and thus should keep track of it
directly. The signals are (in an ideal world) mere views on that
data, so querying them for the signal data isn't good style.

pv/session.cpp
pv/session.hpp

index 4bc510e9fa538d5eb3a848fde39ea4604520512d..6b7645bc7c7ef92e876e74076e062f7f532171c0 100644 (file)
@@ -140,8 +140,14 @@ void Session::set_device(shared_ptr<devices::Device> device)
 
        device_.reset();
 
-       // Remove all traces
+       // Remove all stored data
        signals_.clear();
+       {
+               shared_lock<shared_mutex> lock(signals_mutex_);
+               for (const shared_ptr<data::SignalData> d : all_signal_data_)
+                       d->clear();
+       }
+       all_signal_data_.clear();
        cur_logic_segment_.reset();
 
        for (auto entry : cur_analog_segments_) {
@@ -205,8 +211,11 @@ void Session::start_capture(function<void (const QString)> error_handler)
        }
 
        // Clear signal data
-       for (const shared_ptr<data::SignalData> d : get_data())
-               d->clear();
+       {
+               shared_lock<shared_mutex> lock(signals_mutex_);
+               for (const shared_ptr<data::SignalData> d : all_signal_data_)
+                       d->clear();
+       }
 
        // Begin the session
        sampling_thread_ = std::thread(
@@ -224,30 +233,20 @@ void Session::stop_capture()
                sampling_thread_.join();
 }
 
-set< shared_ptr<data::SignalData> > Session::get_data() const
-{
-       shared_lock<shared_mutex> lock(signals_mutex_);
-       set< shared_ptr<data::SignalData> > data;
-       for (const shared_ptr<view::Signal> sig : signals_) {
-               assert(sig);
-               data.insert(sig->data());
-       }
-
-       return data;
-}
-
 double Session::get_samplerate() const
 {
        double samplerate = 0.0;
 
-       for (const shared_ptr<pv::data::SignalData> d : get_data()) {
-               assert(d);
-               const vector< shared_ptr<pv::data::Segment> > segments =
-                       d->segments();
-               for (const shared_ptr<pv::data::Segment> &s : segments)
-                       samplerate = std::max(samplerate, s->samplerate());
+       {
+               shared_lock<shared_mutex> lock(signals_mutex_);
+               for (const shared_ptr<pv::data::SignalData> d : all_signal_data_) {
+                       assert(d);
+                       const vector< shared_ptr<pv::data::Segment> > segments =
+                               d->segments();
+                       for (const shared_ptr<pv::data::Segment> &s : segments)
+                               samplerate = std::max(samplerate, s->samplerate());
+               }
        }
-
        // If there is no sample rate given we use samples as unit
        if (samplerate == 0.0)
                samplerate = 1.0;
@@ -415,6 +414,7 @@ void Session::update_signals()
                                                new view::LogicSignal(*this,
                                                        device_, channel,
                                                        logic_data_));
+                                       all_signal_data_.insert(logic_data_);
                                        break;
 
                                case SR_CHANNEL_ANALOG:
@@ -424,6 +424,7 @@ void Session::update_signals()
                                        signal = shared_ptr<view::Signal>(
                                                new view::AnalogSignal(
                                                        *this, channel, data));
+                                       all_signal_data_.insert(data);
                                        break;
                                }
 
@@ -519,17 +520,20 @@ void Session::feed_in_trigger()
        // The channel containing most samples should be most accurate
        uint64_t sample_count = 0;
 
-       for (const shared_ptr<pv::data::SignalData> d : get_data()) {
-               assert(d);
-               uint64_t temp_count = 0;
-
-               const vector< shared_ptr<pv::data::Segment> > segments =
-                       d->segments();
-               for (const shared_ptr<pv::data::Segment> &s : segments)
-                       temp_count += s->get_sample_count();
-
-               if (temp_count > sample_count)
-                       sample_count = temp_count;
+       {
+               shared_lock<shared_mutex> lock(signals_mutex_);
+               for (const shared_ptr<pv::data::SignalData> d : all_signal_data_) {
+                       assert(d);
+                       uint64_t temp_count = 0;
+
+                       const vector< shared_ptr<pv::data::Segment> > segments =
+                               d->segments();
+                       for (const shared_ptr<pv::data::Segment> &s : segments)
+                               temp_count += s->get_sample_count();
+
+                       if (temp_count > sample_count)
+                               sample_count = temp_count;
+               }
        }
 
        trigger_event(sample_count / get_samplerate());
index ac3d654c63f06a4d70cd82db368bf16383c41453..22705c3670fffdf851f8bdf65e1e115aa82f5b75 100644 (file)
@@ -114,8 +114,6 @@ public:
 
        void stop_capture();
 
-       std::set< std::shared_ptr<data::SignalData> > get_data() const;
-
        double get_samplerate() const;
 
        const std::unordered_set< std::shared_ptr<view::Signal> >
@@ -168,6 +166,7 @@ private:
 
        mutable boost::shared_mutex signals_mutex_;
        std::unordered_set< std::shared_ptr<view::Signal> > signals_;
+       std::set< std::shared_ptr<data::SignalData> > all_signal_data_;
 
        mutable std::recursive_mutex data_mutex_;
        std::shared_ptr<data::Logic> logic_data_;