forked from Ivasoft/DSView
change boost thread as std lib, and boost mutex
This commit is contained in:
@@ -18,9 +18,7 @@
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#include <boost/foreach.hpp>
|
||||
#include <boost/thread/thread.hpp>
|
||||
|
||||
|
||||
#include <stdexcept>
|
||||
#include <algorithm>
|
||||
@@ -29,12 +27,15 @@
|
||||
|
||||
#include "decoderstack.h"
|
||||
|
||||
#include <pv/data/logic.h>
|
||||
#include <pv/data/logicsnapshot.h>
|
||||
#include <pv/data/decode/decoder.h>
|
||||
#include <pv/data/decode/annotation.h>
|
||||
#include <pv/sigsession.h>
|
||||
#include <pv/view/logicsignal.h>
|
||||
#include "logic.h"
|
||||
#include "logicsnapshot.h"
|
||||
#include "decode/decoder.h"
|
||||
#include "decode/annotation.h"
|
||||
#include "decode/rowdata.h"
|
||||
#include "../sigsession.h"
|
||||
#include "../view/logicsignal.h"
|
||||
#include "../dsvdef.h"
|
||||
|
||||
|
||||
using namespace boost;
|
||||
using namespace std;
|
||||
@@ -50,19 +51,23 @@ const int64_t DecoderStack::DecodeChunkLength = 4 * 1024;
|
||||
//const int64_t DecoderStack::DecodeChunkLength = 1024 * 1024;
|
||||
const unsigned int DecoderStack::DecodeNotifyPeriod = 1024;
|
||||
|
||||
boost::mutex DecoderStack::_global_decode_mutex;
|
||||
std::mutex DecoderStack::_global_decode_mutex;
|
||||
|
||||
DecoderStack::DecoderStack(pv::SigSession *session,
|
||||
const srd_decoder *const dec, DecoderStatus *decoder_status) :
|
||||
_session(session),
|
||||
_sample_count(0),
|
||||
_frame_complete(false),
|
||||
_samples_decoded(0),
|
||||
_decode_state(Stopped),
|
||||
_options_changed(false),
|
||||
_no_memory(false),
|
||||
_mark_index(-1)
|
||||
_session(session)
|
||||
{
|
||||
_samples_decoded = 0;
|
||||
_sample_count = 0;
|
||||
_frame_complete = false;
|
||||
_decode_state = Stopped;
|
||||
_options_changed = false;
|
||||
_no_memory = false;
|
||||
_mark_index = -1;
|
||||
_decoder_status = decoder_status;
|
||||
_bThreadStop = false;
|
||||
_decode_thread = NULL;
|
||||
|
||||
connect(_session, SIGNAL(frame_began()),
|
||||
this, SLOT(on_new_frame()));
|
||||
connect(_session, SIGNAL(data_received()),
|
||||
@@ -72,32 +77,26 @@ DecoderStack::DecoderStack(pv::SigSession *session,
|
||||
|
||||
_stack.push_back(boost::shared_ptr<decode::Decoder>(
|
||||
new decode::Decoder(dec)));
|
||||
|
||||
_decoder_status = decoder_status;
|
||||
|
||||
|
||||
build_row();
|
||||
}
|
||||
|
||||
DecoderStack::~DecoderStack()
|
||||
{
|
||||
// if (_decode_thread.joinable()) {
|
||||
// _decode_thread.interrupt();
|
||||
// _decode_thread.join();
|
||||
// }
|
||||
{
|
||||
stop_decode();
|
||||
_stack.clear();
|
||||
|
||||
for (auto &kv : _rows)
|
||||
{
|
||||
delete kv.second;
|
||||
}
|
||||
_rows.clear();
|
||||
|
||||
_stack.clear();
|
||||
_rows_gshow.clear();
|
||||
_rows_lshow.clear();
|
||||
_class_rows.clear();
|
||||
}
|
||||
|
||||
const std::list< boost::shared_ptr<decode::Decoder> >&
|
||||
DecoderStack::stack() const
|
||||
{
|
||||
return _stack;
|
||||
}
|
||||
|
||||
|
||||
void DecoderStack::push(boost::shared_ptr<decode::Decoder> decoder)
|
||||
{
|
||||
assert(decoder);
|
||||
@@ -124,9 +123,15 @@ void DecoderStack::remove(boost::shared_ptr<Decoder> &decoder)
|
||||
|
||||
void DecoderStack::build_row()
|
||||
{
|
||||
//destory data
|
||||
for (auto &kv : _rows)
|
||||
{
|
||||
delete kv.second;
|
||||
}
|
||||
_rows.clear();
|
||||
|
||||
// Add classes
|
||||
BOOST_FOREACH (const boost::shared_ptr<decode::Decoder> &dec, _stack)
|
||||
for (auto &dec : _stack)
|
||||
{
|
||||
assert(dec);
|
||||
const srd_decoder *const decc = dec->decoder();
|
||||
@@ -135,7 +140,7 @@ void DecoderStack::build_row()
|
||||
// Add a row for the decoder if it doesn't have a row list
|
||||
if (!decc->annotation_rows) {
|
||||
const Row row(decc);
|
||||
_rows[row] = decode::RowData();
|
||||
_rows[row] = new decode::RowData();
|
||||
std::map<const decode::Row, bool>::const_iterator iter = _rows_gshow.find(row);
|
||||
if (iter == _rows_gshow.end()) {
|
||||
_rows_gshow[row] = true;
|
||||
@@ -159,7 +164,7 @@ void DecoderStack::build_row()
|
||||
const Row row(decc, ann_row, order);
|
||||
|
||||
// Add a new empty row data object
|
||||
_rows[row] = decode::RowData();
|
||||
_rows[row] = new decode::RowData();
|
||||
std::map<const decode::Row, bool>::const_iterator iter = _rows_gshow.find(row);
|
||||
if (iter == _rows_gshow.end()) {
|
||||
_rows_gshow[row] = true;
|
||||
@@ -184,7 +189,7 @@ void DecoderStack::build_row()
|
||||
|
||||
int64_t DecoderStack::samples_decoded() const
|
||||
{
|
||||
boost::lock_guard<boost::recursive_mutex> decode_lock(_output_mutex);
|
||||
std::lock_guard<std::mutex> decode_lock(_output_mutex);
|
||||
return _samples_decoded;
|
||||
}
|
||||
|
||||
@@ -192,51 +197,39 @@ void DecoderStack::get_annotation_subset(
|
||||
std::vector<pv::data::decode::Annotation> &dest,
|
||||
const Row &row, uint64_t start_sample,
|
||||
uint64_t end_sample) const
|
||||
{
|
||||
//lock_guard<mutex> lock(_output_mutex);
|
||||
|
||||
std::map<const Row, decode::RowData>::const_iterator iter =
|
||||
_rows.find(row);
|
||||
{
|
||||
auto iter = _rows.find(row);
|
||||
if (iter != _rows.end())
|
||||
(*iter).second.get_annotation_subset(dest,
|
||||
(*iter).second->get_annotation_subset(dest,
|
||||
start_sample, end_sample);
|
||||
}
|
||||
|
||||
|
||||
uint64_t DecoderStack::get_annotation_index(
|
||||
const Row &row, uint64_t start_sample) const
|
||||
{
|
||||
//lock_guard<mutex> lock(_output_mutex);
|
||||
|
||||
{
|
||||
uint64_t index = 0;
|
||||
std::map<const Row, decode::RowData>::const_iterator iter =
|
||||
_rows.find(row);
|
||||
auto iter = _rows.find(row);
|
||||
if (iter != _rows.end())
|
||||
index = (*iter).second.get_annotation_index(start_sample);
|
||||
index = (*iter).second->get_annotation_index(start_sample);
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
uint64_t DecoderStack::get_max_annotation(const Row &row)
|
||||
{
|
||||
//lock_guard<mutex> lock(_output_mutex);
|
||||
|
||||
std::map<const Row, decode::RowData>::const_iterator iter =
|
||||
_rows.find(row);
|
||||
{
|
||||
auto iter = _rows.find(row);
|
||||
if (iter != _rows.end())
|
||||
return (*iter).second.get_max_annotation();
|
||||
return (*iter).second->get_max_annotation();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint64_t DecoderStack::get_min_annotation(const Row &row)
|
||||
{
|
||||
//lock_guard<mutex> lock(_output_mutex);
|
||||
|
||||
std::map<const Row, decode::RowData>::const_iterator iter =
|
||||
_rows.find(row);
|
||||
{
|
||||
auto iter = _rows.find(row);
|
||||
if (iter != _rows.end())
|
||||
return (*iter).second.get_min_annotation();
|
||||
return (*iter).second->get_min_annotation();
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -278,13 +271,11 @@ void DecoderStack::set_rows_lshow(const decode::Row row, bool show)
|
||||
}
|
||||
|
||||
bool DecoderStack::has_annotations(const Row &row) const
|
||||
{
|
||||
//lock_guard<mutex> lock(_output_mutex);
|
||||
|
||||
std::map<const Row, decode::RowData>::const_iterator iter =
|
||||
{
|
||||
auto iter =
|
||||
_rows.find(row);
|
||||
if (iter != _rows.end())
|
||||
if(0 == (*iter).second.get_max_sample())
|
||||
if(0 == (*iter).second->get_max_sample())
|
||||
return false;
|
||||
else
|
||||
return true;
|
||||
@@ -294,29 +285,27 @@ bool DecoderStack::has_annotations(const Row &row) const
|
||||
|
||||
uint64_t DecoderStack::list_annotation_size() const
|
||||
{
|
||||
boost::lock_guard<boost::recursive_mutex> lock(_output_mutex);
|
||||
std::lock_guard<std::mutex> lock(_output_mutex);
|
||||
uint64_t max_annotation_size = 0;
|
||||
for (map<const Row, RowData>::const_iterator i = _rows.begin();
|
||||
for (auto i = _rows.begin();
|
||||
i != _rows.end(); i++) {
|
||||
map<const Row, bool>::const_iterator iter = _rows_lshow.find((*i).first);
|
||||
if (iter != _rows_lshow.end() && (*iter).second)
|
||||
max_annotation_size = max(max_annotation_size,
|
||||
(*i).second.get_annotation_size());
|
||||
(*i).second->get_annotation_size());
|
||||
}
|
||||
|
||||
return max_annotation_size;
|
||||
}
|
||||
|
||||
uint64_t DecoderStack::list_annotation_size(uint16_t row_index) const
|
||||
{
|
||||
//lock_guard<boost::recursive_mutex> lock(_output_mutex);
|
||||
//int row = 0;
|
||||
for (map<const Row, RowData>::const_iterator i = _rows.begin();
|
||||
{
|
||||
for (auto i = _rows.begin();
|
||||
i != _rows.end(); i++) {
|
||||
map<const Row, bool>::const_iterator iter = _rows_lshow.find((*i).first);
|
||||
if (iter != _rows_lshow.end() && (*iter).second)
|
||||
if (row_index-- == 0) {
|
||||
return (*i).second.get_annotation_size();
|
||||
return (*i).second->get_annotation_size();
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
@@ -324,14 +313,13 @@ uint64_t DecoderStack::list_annotation_size(uint16_t row_index) const
|
||||
|
||||
bool DecoderStack::list_annotation(pv::data::decode::Annotation &ann,
|
||||
uint16_t row_index, uint64_t col_index) const
|
||||
{
|
||||
//lock_guard<mutex> lock(_output_mutex);
|
||||
for (map<const Row, RowData>::const_iterator i = _rows.begin();
|
||||
{
|
||||
for (auto i = _rows.begin();
|
||||
i != _rows.end(); i++) {
|
||||
map<const Row, bool>::const_iterator iter = _rows_lshow.find((*i).first);
|
||||
if (iter != _rows_lshow.end() && (*iter).second) {
|
||||
if (row_index-- == 0) {
|
||||
return (*i).second.get_annotation(ann, col_index);
|
||||
return (*i).second->get_annotation(ann, col_index);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -341,9 +329,8 @@ bool DecoderStack::list_annotation(pv::data::decode::Annotation &ann,
|
||||
|
||||
|
||||
bool DecoderStack::list_row_title(int row, QString &title) const
|
||||
{
|
||||
//lock_guard<mutex> lock(_output_mutex);
|
||||
for (map<const Row, RowData>::const_iterator i = _rows.begin();
|
||||
{
|
||||
for (auto i = _rows.begin();
|
||||
i != _rows.end(); i++) {
|
||||
map<const Row, bool>::const_iterator iter = _rows_lshow.find((*i).first);
|
||||
if (iter != _rows_lshow.end() && (*iter).second) {
|
||||
@@ -357,8 +344,7 @@ bool DecoderStack::list_row_title(int row, QString &title) const
|
||||
}
|
||||
|
||||
QString DecoderStack::error_message()
|
||||
{
|
||||
//lock_guard<mutex> lock(_output_mutex);
|
||||
{
|
||||
return _error_message;
|
||||
}
|
||||
|
||||
@@ -374,24 +360,26 @@ void DecoderStack::init()
|
||||
_samples_decoded = 0;
|
||||
_error_message = QString();
|
||||
_no_memory = false;
|
||||
for (map<const Row, RowData>::iterator i = _rows.begin();
|
||||
i != _rows.end(); i++) {
|
||||
//_rows[(*i).first] = decode::RowData();
|
||||
(*i).second.clear();
|
||||
|
||||
for (auto i = _rows.begin();
|
||||
i != _rows.end(); i++) {
|
||||
(*i).second->clear();
|
||||
}
|
||||
|
||||
set_mark_index(-1);
|
||||
}
|
||||
|
||||
void DecoderStack::stop_decode()
|
||||
{
|
||||
//_snapshot.reset();
|
||||
if(_decode_state != Stopped) {
|
||||
if (_decode_thread.get()) {
|
||||
_decode_thread->interrupt();
|
||||
_decode_thread->join();
|
||||
_decode_state = Stopped;
|
||||
}
|
||||
_decode_thread.reset();
|
||||
{
|
||||
_bThreadStop = true;
|
||||
|
||||
if (_decode_thread && _decode_thread->joinable()) {
|
||||
_decode_thread->join();
|
||||
}
|
||||
DESTROY_OBJECT(_decode_thread);
|
||||
|
||||
if(_decode_state != Stopped) {
|
||||
_decode_state = Stopped;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -407,7 +395,7 @@ void DecoderStack::begin_decode()
|
||||
init();
|
||||
|
||||
// Check that all decoders have the required channels
|
||||
BOOST_FOREACH(const boost::shared_ptr<decode::Decoder> &dec, _stack)
|
||||
for(auto &dec : _stack)
|
||||
if (!dec->have_required_probes()) {
|
||||
_error_message = tr("One or more required channels "
|
||||
"have not been specified");
|
||||
@@ -417,9 +405,9 @@ void DecoderStack::begin_decode()
|
||||
// We get the logic data of the first channel in the list.
|
||||
// This works because we are currently assuming all
|
||||
// LogicSignals have the same data/snapshot
|
||||
BOOST_FOREACH (const boost::shared_ptr<decode::Decoder> &dec, _stack) {
|
||||
for (auto &dec : _stack) {
|
||||
if (dec && !dec->channels().empty()) {
|
||||
BOOST_FOREACH(boost::shared_ptr<view::Signal> sig, _session->get_signals()) {
|
||||
for(auto &sig : _session->get_signals()) {
|
||||
if((sig->get_index() == (*dec->channels().begin()).second) &&
|
||||
(logic_signal = dynamic_pointer_cast<view::LogicSignal>(sig)) &&
|
||||
(data = logic_signal->logic_data()))
|
||||
@@ -447,18 +435,23 @@ void DecoderStack::begin_decode()
|
||||
if (_samplerate == 0.0)
|
||||
return;
|
||||
|
||||
//_decode_thread = boost::thread(&DecoderStack::decode_proc, this);
|
||||
_decode_thread.reset(new boost::thread(&DecoderStack::decode_proc, this));
|
||||
if (_decode_thread && _decode_thread->joinable()) {
|
||||
_bThreadStop = true;
|
||||
_decode_thread->join();
|
||||
}
|
||||
DESTROY_OBJECT(_decode_thread);
|
||||
|
||||
_bThreadStop = false; //reset stop flag
|
||||
_decode_thread = new std::thread(&DecoderStack::decode_proc, this);
|
||||
}
|
||||
|
||||
uint64_t DecoderStack::get_max_sample_count() const
|
||||
{
|
||||
uint64_t max_sample_count = 0;
|
||||
|
||||
for (map<const Row, RowData>::const_iterator i = _rows.begin();
|
||||
i != _rows.end(); i++)
|
||||
max_sample_count = max(max_sample_count,
|
||||
(*i).second.get_max_sample());
|
||||
for (auto i = _rows.begin(); i != _rows.end(); i++){
|
||||
max_sample_count = max(max_sample_count, (*i).second->get_max_sample());
|
||||
}
|
||||
|
||||
return max_sample_count;
|
||||
}
|
||||
@@ -485,13 +478,13 @@ void DecoderStack::decode_data(
|
||||
uint64_t entry_cnt = 0;
|
||||
uint64_t i = decode_start;
|
||||
char *error = NULL;
|
||||
while(!boost::this_thread::interruption_requested() &&
|
||||
i < decode_end && !_no_memory)
|
||||
{
|
||||
//lock_guard<mutex> decode_lock(_global_decode_mutex);
|
||||
|
||||
while(!_bThreadStop && i < decode_end && !_no_memory)
|
||||
{
|
||||
std::vector<const uint8_t *> chunk;
|
||||
std::vector<uint8_t> chunk_const;
|
||||
uint64_t chunk_end = decode_end;
|
||||
|
||||
for (int j =0 ; j < logic_di->dec_num_channels; j++) {
|
||||
int sig_index = logic_di->dec_channelmap[j];
|
||||
if (sig_index == -1) {
|
||||
@@ -520,7 +513,7 @@ void DecoderStack::decode_data(
|
||||
i = chunk_end;
|
||||
|
||||
{
|
||||
boost::lock_guard<boost::recursive_mutex> lock(_output_mutex);
|
||||
std::lock_guard<std::mutex> lock(_output_mutex);
|
||||
_samples_decoded = i - decode_start + 1;
|
||||
}
|
||||
|
||||
@@ -536,8 +529,12 @@ void DecoderStack::decode_data(
|
||||
}
|
||||
|
||||
void DecoderStack::decode_proc()
|
||||
{
|
||||
boost::lock_guard<boost::mutex> decode_lock(_global_decode_mutex);
|
||||
{
|
||||
std::lock_guard<std::mutex> decode_lock(_global_decode_mutex);
|
||||
|
||||
if (_bThreadStop){
|
||||
return;
|
||||
}
|
||||
|
||||
optional<uint64_t> sample_count;
|
||||
srd_session *session;
|
||||
@@ -554,13 +551,10 @@ void DecoderStack::decode_proc()
|
||||
_decode_state = Running;
|
||||
|
||||
// Get the intial sample count
|
||||
{
|
||||
//unique_lock<mutex> input_lock(_input_mutex);
|
||||
sample_count = _sample_count = _snapshot->get_sample_count();
|
||||
}
|
||||
sample_count = _sample_count = _snapshot->get_sample_count();
|
||||
|
||||
// Create the decoders
|
||||
BOOST_FOREACH(const boost::shared_ptr<decode::Decoder> &dec, _stack)
|
||||
for(auto &dec : _stack)
|
||||
{
|
||||
srd_decoder_inst *const di = dec->create_decoder_inst(session);
|
||||
|
||||
@@ -588,6 +582,7 @@ void DecoderStack::decode_proc()
|
||||
|
||||
char *error = NULL;
|
||||
if (srd_session_start(session, &error) == SRD_OK)
|
||||
//need a lot time
|
||||
decode_data(decode_start, decode_end, session);
|
||||
else
|
||||
_error_message = QString::fromLocal8Bit(error);
|
||||
@@ -622,14 +617,16 @@ void DecoderStack::annotation_callback(srd_proto_data *pdata, void *decoder)
|
||||
|
||||
DecoderStack *const d = (DecoderStack*)decoder;
|
||||
assert(d);
|
||||
|
||||
//lock_guard<mutex> lock(d->_output_mutex);
|
||||
|
||||
|
||||
if (d->_no_memory) {
|
||||
return;
|
||||
}
|
||||
|
||||
const Annotation a(pdata, d->_decoder_status);
|
||||
Annotation *a = new Annotation(pdata, d->_decoder_status);
|
||||
if (a == NULL){
|
||||
d->_no_memory = true;
|
||||
return;
|
||||
}
|
||||
|
||||
// Find the row
|
||||
assert(pdata->pdo);
|
||||
@@ -637,11 +634,11 @@ void DecoderStack::annotation_callback(srd_proto_data *pdata, void *decoder)
|
||||
const srd_decoder *const decc = pdata->pdo->di->decoder;
|
||||
assert(decc);
|
||||
|
||||
map<const Row, decode::RowData>::iterator row_iter = d->_rows.end();
|
||||
auto row_iter = d->_rows.end();
|
||||
|
||||
// Try looking up the sub-row of this class
|
||||
const map<pair<const srd_decoder*, int>, Row>::const_iterator r =
|
||||
d->_class_rows.find(make_pair(decc, a.format()));
|
||||
d->_class_rows.find(make_pair(decc, a->format()));
|
||||
if (r != d->_class_rows.end())
|
||||
row_iter = d->_rows.find((*r).second);
|
||||
else
|
||||
@@ -653,14 +650,13 @@ void DecoderStack::annotation_callback(srd_proto_data *pdata, void *decoder)
|
||||
assert(row_iter != d->_rows.end());
|
||||
if (row_iter == d->_rows.end()) {
|
||||
qDebug() << "Unexpected annotation: decoder = " << decc <<
|
||||
", format = " << a.format();
|
||||
", format = " << a->format();
|
||||
assert(0);
|
||||
return;
|
||||
}
|
||||
|
||||
// Add the annotation
|
||||
boost::lock_guard<boost::recursive_mutex> lock(d->_output_mutex);
|
||||
if (!(*row_iter).second.push_annotation(a))
|
||||
// Add the annotation
|
||||
if (!(*row_iter).second->push_annotation(a))
|
||||
d->_no_memory = true;
|
||||
}
|
||||
|
||||
@@ -670,33 +666,19 @@ void DecoderStack::on_new_frame()
|
||||
}
|
||||
|
||||
void DecoderStack::on_data_received()
|
||||
{
|
||||
// {
|
||||
// unique_lock<mutex> lock(_input_mutex);
|
||||
// if (_snapshot)
|
||||
// _sample_count = _snapshot->get_sample_count();
|
||||
// }
|
||||
// _input_cond.notify_one();
|
||||
{
|
||||
}
|
||||
|
||||
void DecoderStack::on_frame_ended()
|
||||
{
|
||||
// {
|
||||
// unique_lock<mutex> lock(_input_mutex);
|
||||
// if (_snapshot)
|
||||
// _frame_complete = true;
|
||||
// }
|
||||
// _input_cond.notify_one();
|
||||
{
|
||||
_options_changed = true;
|
||||
begin_decode();
|
||||
}
|
||||
|
||||
int DecoderStack::list_rows_size()
|
||||
{
|
||||
//lock_guard<mutex> lock(_output_mutex);
|
||||
{
|
||||
int rows_size = 0;
|
||||
for (map<const Row, RowData>::const_iterator i = _rows.begin();
|
||||
i != _rows.end(); i++) {
|
||||
for (auto i = _rows.begin(); i != _rows.end(); i++) {
|
||||
map<const Row, bool>::const_iterator iter = _rows_lshow.find((*i).first);
|
||||
if (iter != _rows_lshow.end() && (*iter).second)
|
||||
rows_size++;
|
||||
|
||||
Reference in New Issue
Block a user