diff --git a/experiments/sm/CMakeLists.txt b/experiments/sm/CMakeLists.txt index 5077c87..9854a09 100644 --- a/experiments/sm/CMakeLists.txt +++ b/experiments/sm/CMakeLists.txt @@ -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) \ No newline at end of file diff --git a/experiments/sm/template_specialization.cpp b/experiments/sm/template_specialization.cpp new file mode 100644 index 0000000..7a6d3a7 --- /dev/null +++ b/experiments/sm/template_specialization.cpp @@ -0,0 +1,35 @@ +// +// Created by Stepan on 16.10.2020. +// + +#include "../../can/can_data.h" +#include + +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(); + piCout << "after bus data:" << newMsg; +} \ No newline at end of file diff --git a/sm/block.h b/sm/block.h deleted file mode 100644 index 8525337..0000000 --- a/sm/block.h +++ /dev/null @@ -1,63 +0,0 @@ -#ifndef MULTITHREAD_EXPERIMENTS_BLOCK_H -#define MULTITHREAD_EXPERIMENTS_BLOCK_H - -#include -#include -#include -#include - -namespace sm { - - struct block { - PIVector input_blocks; - PIVector 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