]> sigrok.org Git - pulseview.git/commitdiff
Initial threaded capture
authorJoel Holdsworth <redacted>
Sun, 2 Dec 2012 14:32:48 +0000 (14:32 +0000)
committerJoel Holdsworth <redacted>
Tue, 11 Dec 2012 20:05:19 +0000 (20:05 +0000)
CMakeLists.txt
pv/sigsession.cpp
pv/sigsession.h

index 812176ec6e8b52d17e1729c23d8e9beadbd82e77..042cdae3cf7e3028853b68647e13031c34a7f757 100644 (file)
@@ -46,14 +46,14 @@ pkg_check_modules(PKGDEPS REQUIRED
        libsigrok>=0.2.0
 )
 
+find_package(Qt4 REQUIRED)
+
 # On Windows/MinGW we explicitly point cmake to the Boost directory.
 if(WIN32)
        set(BOOST_ROOT /usr/local)
 endif()
 
-find_package(Qt4 REQUIRED)
-
-find_package(Boost 1.46)
+find_package(Boost 1.46 COMPONENTS thread)
 
 #===============================================================================
 #= Config Header
index 81ccca720a0b38ca7d5d0a2b58b926f8e91c51ed..137998bdb57760a336ab558c7c9b9591c65a1503 100644 (file)
@@ -44,6 +44,10 @@ SigSession::SigSession()
 
 SigSession::~SigSession()
 {
+       if(_sampling_thread.get())
+               _sampling_thread->join();
+       _sampling_thread.reset();
+
        // TODO: This should not be necessary
        _session = NULL;
 }
@@ -61,6 +65,28 @@ void SigSession::load_file(const std::string &name)
 
 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();
+
+       _sampling_thread.reset(new boost::thread(
+               &SigSession::sample_thread_proc, this, sdi,
+               record_length, sample_rate));
+}
+
+vector< shared_ptr<view::Signal> >& SigSession::get_signals()
+{
+       return _signals;
+}
+
+boost::shared_ptr<LogicData> SigSession::get_data()
+{
+       return _logic_data;
+}
+
+void SigSession::sample_thread_proc(struct sr_dev_inst *sdi,
+       uint64_t record_length, uint64_t sample_rate)
 {
        sr_session_new();
        sr_session_datafeed_callback_add(data_feed_in_proc);
@@ -94,16 +120,6 @@ void SigSession::start_capture(struct sr_dev_inst *sdi,
        sr_session_destroy();
 }
 
-vector< shared_ptr<view::Signal> >& SigSession::get_signals()
-{
-       return _signals;
-}
-
-boost::shared_ptr<LogicData> SigSession::get_data()
-{
-       return _logic_data;
-}
-
 void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
        struct sr_datafeed_packet *packet)
 {
@@ -114,43 +130,49 @@ void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
 
        switch (packet->type) {
        case SR_DF_HEADER:
+       {
+               lock_guard<mutex> lock(_data_mutex);
                _signals.clear();
                break;
+       }
 
        case SR_DF_META_LOGIC:
-               {
-                       assert(packet->payload);
+       {
+               assert(packet->payload);
 
-                       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)
-                               break;
+               const sr_datafeed_meta_logic &meta_logic =
+                       *(sr_datafeed_meta_logic*)packet->payload;
 
-                       // Add the signals
-                       for (int i = 0; i < meta_logic.num_probes; i++)
+               // Create an empty LogiData for coming data snapshots
+               _logic_data.reset(new LogicData(meta_logic));
+               assert(_logic_data);
+               if(!_logic_data)
+                       break;
+
+               // Add the signals
+               for (int i = 0; i < meta_logic.num_probes; i++)
+               {
+                       const sr_probe *const probe =
+                               (const sr_probe*)g_slist_nth_data(
+                                       sdi->probes, i);
+                       if(probe->enabled)
                        {
-                               const sr_probe *const probe =
-                                       (const sr_probe*)g_slist_nth_data(
-                                               sdi->probes, i);
-                               if(probe->enabled)
-                               {
-                                       shared_ptr<LogicSignal> signal(
-                                               new LogicSignal(probe->name,
-                                                       _logic_data,
-                                                       probe->index));
-                                       _signals.push_back(signal);
-                               }
+                               shared_ptr<LogicSignal> signal(
+                                       new LogicSignal(probe->name,
+                                               _logic_data,
+                                               probe->index));
+                               _signals.push_back(signal);
                        }
-
-                       break;
                }
 
-       case SR_DF_LOGIC:
+               break;
+       }
 
+       case SR_DF_LOGIC:
+       {
+               lock_guard<mutex> lock(_data_mutex);
                assert(packet->payload);
                if(!_cur_logic_snapshot)
                {
@@ -168,12 +190,18 @@ void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
                }
 
                break;
+       }
 
        case SR_DF_END:
-               _cur_logic_snapshot.reset();
+       {
+               {
+                       lock_guard<mutex> lock(_data_mutex);
+                       _cur_logic_snapshot.reset();
+               }
                data_updated();
                break;
        }
+       }
 }
 
 void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
index 29929a9dde63d1b1bbc4520885d28cc3e719370e..744d8340357117e44069fae550aad39064b20540 100644 (file)
 #define PULSEVIEW_PV_SIGSESSION_H
 
 #include <boost/shared_ptr.hpp>
+#include <boost/thread.hpp>
 
 #include <string>
+#include <utility>
 #include <vector>
 
 #include <QObject>
@@ -61,6 +63,9 @@ public:
        boost::shared_ptr<LogicData> get_data();
 
 private:
+       void sample_thread_proc(struct sr_dev_inst *sdi,
+               uint64_t record_length, uint64_t sample_rate);
+
        void data_feed_in(const struct sr_dev_inst *sdi,
                struct sr_datafeed_packet *packet);
 
@@ -68,10 +73,13 @@ private:
                struct sr_datafeed_packet *packet);
 
 private:
+       mutable boost::mutex _data_mutex;
        std::vector< boost::shared_ptr<view::Signal> > _signals;
        boost::shared_ptr<LogicData> _logic_data;
        boost::shared_ptr<LogicDataSnapshot> _cur_logic_snapshot;
 
+       std::auto_ptr<boost::thread> _sampling_thread;
+
 signals:
        void data_updated();