Try to reproduce (PIByteArray << CAN_Raw) operator bug

This commit is contained in:
3 changed files with 38 additions and 63 deletions

View File

@@ -8,3 +8,6 @@ target_link_libraries(block_choice PIP)
add_executable(smbusdata_crash_test smbusdata_crash_test.cpp)
target_link_libraries(smbusdata_crash_test SMBricks_shared PIP::Crypt)
add_executable(pibytearray_template_spec template_specialization.cpp)
target_link_libraries(pibytearray_template_spec SMBricks_shared PIP::Crypt)

View File

@@ -0,0 +1,35 @@
//
// Created by Stepan on 16.10.2020.
//
#include "../../can/can_data.h"
#include <sm_base.h>
REGISTER_BUS_TYPE(CAN_Raw)
PICout operator <<(PICout s, const CAN_Raw & v) {
s.saveControl();
s.setControl(0);
s << "{id=0x" << PIString::fromNumber(v.Id, 16) << ", size=" << v.Size << ", data=" << PIByteArray(v.Data, v.Size);
s.restoreControl();
return s;
}
int main() {
CAN_Raw msg = { .Id = 0x22, .Size = 8, .Data = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 } };
piCout << "src: " << msg << "\n";
CAN_Raw newMsg;
PIByteArray ar;
ar << msg;
ar >> newMsg;
piCout << "after ByteArray: " << newMsg << "\n";
SMBusData busData;
busData = SMBusData::create(msg);
PIByteArray serializedBusData = busData.save();
SMBusData loadedBusData = SMBusData::fromByteArray(serializedBusData, busData.busType());
newMsg = loadedBusData.value<CAN_Raw>();
piCout << "after bus data:" << newMsg;
}

View File

@@ -1,63 +0,0 @@
#ifndef MULTITHREAD_EXPERIMENTS_BLOCK_H
#define MULTITHREAD_EXPERIMENTS_BLOCK_H
#include <pivector.h>
#include <iostream>
#include <thread>
#include <cmath>
namespace sm {
struct block {
PIVector<block*> input_blocks;
PIVector<block*> output_blocks;
std::atomic_flag barrier = ATOMIC_FLAG_INIT;
const int is_calc_idx;
const std::chrono::microseconds calc_time;
static std::chrono::microseconds random_time() {
float val = powf(rand() % 1000 / 1000.f, 20.f) * 30.f * 1000.f;
// std::cout << int(val) << std::endl;
return std::chrono::microseconds(int(val));
}
explicit block(const int is_calc_idx) : is_calc_idx(is_calc_idx), calc_time(random_time()) {}
void calc() {
std::this_thread::sleep_for(calc_time);
}
void unlock(int locks_count = -1) {
if (locks_count == -1) locks_count = this->input_blocks.size();
for (int i = 0; i < locks_count; ++i) {
this->input_blocks[i]->barrier.clear(std::memory_order_release);
}
this->barrier.clear(std::memory_order_release);
}
bool try_lock() {
if (this->barrier.test_and_set(std::memory_order_acquire)) return false;
int locks_count = 0;
for (auto & input_block : this->input_blocks) {
if (input_block->barrier.test_and_set(std::memory_order_acquire)) break;
locks_count++;
}
if (locks_count == this->input_blocks.size()) {
return true;
} else {
unlock(locks_count);
return false;
}
}
};
struct time_report {
double calc_time_ms;
double sync_time_ms;
};
}
#endif //MULTITHREAD_EXPERIMENTS_BLOCK_H