]> sigrok.org Git - pulseview.git/blobdiff - pv/session.cpp
Session: don't hold sampling_mutex_ during signal emission.
[pulseview.git] / pv / session.cpp
index 56ac89975f65e97c47d4f28a2de96142b4f6316d..2a794f0fb0c4af8a79b662e12b810ffdb28b510d 100644 (file)
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  */
 
+#ifdef _WIN32
+// Windows: Avoid boost/thread namespace pollution (which includes windows.h).
+#define NOGDI
+#define NORESOURCE
+#endif
+#include <boost/thread/locks.hpp>
+#include <boost/thread/shared_mutex.hpp>
+
 #ifdef ENABLE_DECODE
 #include <libsigrokdecode/libsigrokdecode.h>
 #endif
@@ -127,8 +135,11 @@ void Session::set_device(shared_ptr<devices::Device> device)
        // Ensure we are not capturing before setting the device
        stop_capture();
 
+       if (device_)
+               device_->close();
+
        device_ = std::move(device);
-       device_->create();
+       device_->open();
        device_->session()->add_datafeed_callback([=]
                (shared_ptr<sigrok::Device> device, shared_ptr<Packet> packet) {
                        data_feed_in(device, packet);
@@ -211,13 +222,28 @@ set< shared_ptr<data::SignalData> > Session::get_data() const
        return data;
 }
 
-boost::shared_mutex& Session::signals_mutex() const
+double Session::get_samplerate() const
 {
-       return signals_mutex_;
+       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());
+       }
+
+       // If there is no sample rate given we use samples as unit
+       if (samplerate == 0.0)
+               samplerate = 1.0;
+
+       return samplerate;
 }
 
-const unordered_set< shared_ptr<view::Signal> >& Session::signals() const
+const unordered_set< shared_ptr<view::Signal> > Session::signals() const
 {
+       shared_lock<shared_mutex> lock(signals_mutex_);
        return signals_;
 }
 
@@ -298,9 +324,14 @@ void Session::remove_decode_signal(view::DecodeTrace *signal)
 
 void Session::set_capture_state(capture_state state)
 {
-       lock_guard<mutex> lock(sampling_mutex_);
-       const bool changed = capture_state_ != state;
-       capture_state_ = state;
+       bool changed;
+
+       {
+               lock_guard<mutex> lock(sampling_mutex_);
+               changed = capture_state_ != state;
+               capture_state_ = state;
+       }
+
        if (changed)
                capture_state_changed(state);
 }
@@ -419,6 +450,8 @@ void Session::sample_thread_proc(shared_ptr<devices::Device> device,
 
        cur_samplerate_ = device_->read_config<uint64_t>(ConfigKey::SAMPLERATE);
 
+       out_of_memory_ = false;
+
        try {
                device_->start();
        } catch(Error e) {
@@ -438,6 +471,9 @@ void Session::sample_thread_proc(shared_ptr<devices::Device> device,
                qDebug("SR_DF_END was not received.");
                assert(0);
        }
+
+       if (out_of_memory_)
+               error_handler(tr("Out of memory, acquisition stopped."));
 }
 
 void Session::feed_in_header()
@@ -461,6 +497,27 @@ void Session::feed_in_meta(shared_ptr<Meta> meta)
        signals_changed();
 }
 
+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;
+       }
+
+       trigger_event(sample_count / get_samplerate());
+}
+
 void Session::feed_in_frame_begin()
 {
        if (cur_logic_segment_ || !cur_analog_segments_.empty())
@@ -514,7 +571,7 @@ void Session::feed_in_analog(shared_ptr<Analog> analog)
        const vector<shared_ptr<Channel>> channels = analog->channels();
        const unsigned int channel_count = channels.size();
        const size_t sample_count = analog->num_samples() / channel_count;
-       const float *data = analog->data_pointer();
+       const float *data = static_cast<const float *>(analog->data_pointer());
        bool sweep_beginning = false;
 
        for (auto channel : channels)
@@ -585,16 +642,30 @@ void Session::data_feed_in(shared_ptr<sigrok::Device> device,
                feed_in_meta(dynamic_pointer_cast<Meta>(packet->payload()));
                break;
 
+       case SR_DF_TRIGGER:
+               feed_in_trigger();
+               break;
+
        case SR_DF_FRAME_BEGIN:
                feed_in_frame_begin();
                break;
 
        case SR_DF_LOGIC:
-               feed_in_logic(dynamic_pointer_cast<Logic>(packet->payload()));
+               try {
+                       feed_in_logic(dynamic_pointer_cast<Logic>(packet->payload()));
+               } catch (std::bad_alloc) {
+                       out_of_memory_ = true;
+                       device_->stop();
+               }
                break;
 
        case SR_DF_ANALOG:
-               feed_in_analog(dynamic_pointer_cast<Analog>(packet->payload()));
+               try {
+                       feed_in_analog(dynamic_pointer_cast<Analog>(packet->payload()));
+               } catch (std::bad_alloc) {
+                       out_of_memory_ = true;
+                       device_->stop();
+               }
                break;
 
        case SR_DF_END: