Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
Chernov, Anton [VPE] committed Nov 23, 2021
2 parents a8335fb + e09f37a commit d1bc863
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 31 deletions.
18 changes: 10 additions & 8 deletions include/TimeManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@
#include <thread>
#include <functional>
/******************************** Definition **********************************/
#define TICKS 0
#define SECONDS 1
#define SYNC TICKS
#define BUF_SIZE 16
#define BUF_SIZE 16u
#define MILLISECONDS_PER_TICK 10u
#define TICKS_PER_SECOND 1000u / MILLISECONDS_PER_TICK
#define TICKS 0u
#define MILLISECONDS 1u
#define SYNC TICKS

typedef void* semaphore_t;

struct delay_t {
uint16_t cur_time;
uint16_t wait_time;
uint32_t cur_time;
uint32_t wait_time;
bool is_blocked;
};

Expand All @@ -45,15 +47,15 @@ class TimeManager {
@fn void TimeManager::Sync()
*/
void Sync();
#elif SYNC == SECONDS
#elif SYNC == MILLISECONDS
/**
@brief Synchronization function
@fn void TimeManager::Sync(uint32_t elapsed_time)
@param[in] elapsed_time passed argument of elapsed time
*/
void Sync(uint32_t);
#else
#error NOT implemented
#error Type of synchronization is unknown
#endif // SYNC

/**
Expand Down
32 changes: 20 additions & 12 deletions source/TimeManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ TimeManager::~TimeManager() {
void TimeManager::Sync() {
WaitForSingleObject(sync_tick_access_, INFINITE);
uint16_t curr_time = ++sync_tick_;
#elif SYNC == SECONDS
#elif SYNC == MILLISECONDS
void TimeManager::Sync(uint32_t elapsed_time) {
sync_tick_access.lock();
WaitForSingleObject(sync_tick_access_, INFINITE);
{
uint16_t remainder = UINT16_MAX - sync_tick_;
uint32_t remainder = UINT32_MAX - sync_tick_;
if (elapsed_time >= remainder) {
elapsed_time -= remainder;
sync_tick_ = elapsed_time;
Expand All @@ -49,9 +49,9 @@ TimeManager::~TimeManager() {
sync_tick_ += elapsed_time;
}
}
uint16_t curr_time = sync_tick_;
uint32_t curr_time = sync_tick_;
#else
#error NOT implemented
#error Type of synchronization is unknown
#endif // SYNC
ReleaseSemaphore(sync_tick_access_, 1, NULL);
for (uint8_t i = 0; i < BUF_SIZE; i++) {
Expand All @@ -62,7 +62,7 @@ TimeManager::~TimeManager() {
}
}
else {
uint16_t res = UINT32_MAX - prcs_list_[i].delay.cur_time + prcs_list_[i].delay.cur_time;
uint32_t res = UINT32_MAX - prcs_list_[i].delay.cur_time + prcs_list_[i].delay.cur_time;
if (res >= prcs_list_[i].delay.wait_time) {
ReleaseSemaphore(prcs_blocks_[i], 1, NULL);
}
Expand Down Expand Up @@ -106,7 +106,13 @@ void TimeManager::wait_in_ticks(uint32_t delay) {
uint8_t dscr = get_dscr_(std::this_thread::get_id());

prcs_list_[dscr].delay.cur_time = cur_time;
#if SYNC == TICKS
prcs_list_[dscr].delay.wait_time = delay;
#elif SYNC == MILLISECONDS
prcs_list_[dscr].delay.wait_time = delay * MILLISECONDS_PER_TICK;
#else
#error Type of synchronization is unknown
#endif // SYNC
prcs_list_[dscr].delay.is_blocked = true;
do {
dwWaitResult = WaitForSingleObject(
Expand All @@ -131,17 +137,19 @@ void TimeManager::wait_in_ticks(uint32_t delay) {
void TimeManager::Init_() {
prcs_id_[0] = std::this_thread::get_id();
for (uint8_t index = 0; index < BUF_SIZE; index++) {
prcs_list_[index].delay.is_blocked = true;
prcs_list_[index].delay.cur_time = 0u;
prcs_list_[index].delay.wait_time = 0u;
prcs_list_[index].delay.is_blocked = false;
}
}
/*----------------------------------------------------------------------------*/
semaphore_t TimeManager::CreateBinSemaphore_(bool blocked) {
uint8_t initial_count = (blocked) ? 0 : 1;
uint8_t INITIAL_COUNT = (blocked) ? 0 : 1;
semaphore_t link = CreateSemaphore(
NULL, // default security attributes
initial_count,
1, // maximum count
NULL // unnamed semaphore
NULL, // default security attributes
INITIAL_COUNT, // initial count
1, // maximum count
NULL // unnamed semaphore
);
if (link == NULL) printf("Error create semaphore\n");
return link;
Expand Down
40 changes: 29 additions & 11 deletions source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ enum descriptors {

class TEST {
public:
uint8_t virtual_time;
uint32_t virtual_time;
uint8_t virtual_seconds;

TEST();
~TEST();
void Sync(uint16_t);
void Sync(uint32_t);

private:
TimeManager *TM_;
Expand All @@ -32,13 +33,22 @@ class TEST {
};

int main() {
uint8_t timer = 10;
uint8_t timer = 10; //in seconds
TEST *test = new TEST;

while (timer--) {
#if SYNC == TICKS
for (uint8_t i = 0; i < TICKS_PER_SECOND; i++) {
std::this_thread::sleep_for(std::chrono::milliseconds(MILLISECONDS_PER_TICK));
test->Sync(MILLISECONDS_PER_TICK);
}
#elif SYNC == MILLISECONDS
const uint16_t delay = 1000;
std::this_thread::sleep_for(std::chrono::milliseconds(delay));
test->Sync(delay);
#else
#error Type of synchronization is unknown
#endif // SYNC
}
printf("Time is up\n");
getchar();
Expand All @@ -48,6 +58,7 @@ int main() {

TEST::TEST() {
virtual_time = 0;
virtual_seconds = 0;
TM_ = new TimeManager();
TM_->AddPrcs(THREAD_1, f_func_1);
TM_->AddPrcs(THREAD_2, f_func_2);
Expand All @@ -61,33 +72,40 @@ TEST::~TEST() {
delete TM_;
}

void TEST::Sync(uint16_t elapsed_time) {
printf("Synchronization! Virtual time equals %d\n", ++virtual_time);
void TEST::Sync(uint32_t elapsed_time) {
#if SYNC == TICKS
virtual_time += elapsed_time;
while (virtual_time >= 1000u) {
virtual_time -= 1000u;
printf("Synchronization! Virtual time equals %d\n", ++virtual_seconds);
}
TM_->Sync();
#elif SYNC == SECONDS
#elif SYNC == MILLISECONDS
printf("Synchronization! Virtual time equals %d\n", ++virtual_seconds);
TM_->Sync(elapsed_time);
#else
#error NOT implemented
#error Type of synchronization is unknown
#endif // SYNC
}

void TEST::thread_1_thr_func( ) {
printf("THR_1: Wait 1 sec\n");
this->TM_->wait_in_ticks(1);
this->TM_->wait_in_ticks(1 * TICKS_PER_SECOND);
printf("THR_1: Wait 6 sec\n");
this->TM_->wait_in_ticks(6);
this->TM_->wait_in_ticks(6 * TICKS_PER_SECOND);
printf("THR_1: Finish\n");
}

void TEST::thread_2_thr_func() {
printf("THR_2: Wait 5 sec\n");
this->TM_->wait_in_ticks(5);
this->TM_->wait_in_ticks(5 * TICKS_PER_SECOND);
printf("THR_2: Wait 3 sec\n");
this->TM_->wait_in_ticks(3 * TICKS_PER_SECOND);
printf("THR_2: Finish\n");
}

void TEST::thread_3_thr_func() {
printf("THR_3: Wait 9 sec\n");
this->TM_->wait_in_ticks(9);
this->TM_->wait_in_ticks(9 * TICKS_PER_SECOND);
printf("THR_3: Finish\n");
}

0 comments on commit d1bc863

Please sign in to comment.