From 2fdf534c252d7343a4aa18896c4956b6115e4d41 Mon Sep 17 00:00:00 2001 From: Michael Schmid Date: Fri, 24 Mar 2017 12:51:45 +0100 Subject: [PATCH] edited time for more safety with overflow, and thread join method --- base_c/include/embb/base/c/internal/atomic/compare_and_swap.h | 2 +- base_c/src/thread.c | 11 ++--------- base_c/src/time.c | 14 ++++++++------ 3 files changed, 11 insertions(+), 16 deletions(-) diff --git a/base_c/include/embb/base/c/internal/atomic/compare_and_swap.h b/base_c/include/embb/base/c/internal/atomic/compare_and_swap.h index 065b08f..1da58ba 100644 --- a/base_c/include/embb/base/c/internal/atomic/compare_and_swap.h +++ b/base_c/include/embb/base/c/internal/atomic/compare_and_swap.h @@ -132,7 +132,7 @@ EMBB_DEFINE_COMPARE_AND_SWAP(4, "") EMBB_PLATFORM_INLINE int EMBB_CAT2(embb_internal__atomic_compare_and_swap_, \ EMBB_PARAMETER_SIZE_BYTE)(EMBB_CAT2(EMBB_BASE_BASIC_TYPE_SIZE_, EMBB_PARAMETER_SIZE_BYTE) volatile* pointer_to_value, EMBB_CAT2(EMBB_BASE_BASIC_TYPE_SIZE_, EMBB_PARAMETER_SIZE_BYTE) volatile* expected, \ EMBB_CAT2(EMBB_BASE_BASIC_TYPE_SIZE_, EMBB_PARAMETER_SIZE_BYTE) desired) { \ - register /*__extension__ */ uint64_t e_reg = ((uint64_t)*expected << 32U) | desired; \ + register uint64_t e_reg = ((uint64_t)*expected << 32U) | desired; \ __asm__ __volatile__ ("cmpswap" EMBB_ATOMIC_TC_SIZE_SUFFIX " [%1]0, %A0\n\t" \ : "+d"(e_reg) \ : "a"(pointer_to_value) \ diff --git a/base_c/src/thread.c b/base_c/src/thread.c index d67024a..7239262 100644 --- a/base_c/src/thread.c +++ b/base_c/src/thread.c @@ -387,15 +387,8 @@ int embb_thread_join(embb_thread_t* thread, int *result_code) { return EMBB_ERROR; } - TickType_t xTicksToWait = 0xFFFFFFFF; - TimeOut_t xTimeOut; - - vTaskSetTimeOutState( &xTimeOut ); - - while(thread->embb_internal_handle != NULL) { - if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) != pdFALSE ) { - break; - } + while(thread->embb_internal_handle != NULL && eTaskGetState(thread->embb_internal_handle) != eDeleted) { + taskYIELD(); } if (thread->embb_internal_arg != NULL) { diff --git a/base_c/src/time.c b/base_c/src/time.c index 37d5251..f49e3d3 100644 --- a/base_c/src/time.c +++ b/base_c/src/time.c @@ -105,20 +105,22 @@ int embb_time_in(embb_time_t* time, const embb_duration_t* duration) { #ifdef EMBB_PLATFORM_THREADING_RTOSTASKS +#include #include +#include int embb_time_in(embb_time_t* time, const embb_duration_t* duration) { if (time == NULL || duration == NULL) { return EMBB_ERROR; } - //scm34681: implement correctly for different ticks/second - //scm34681: check for overflow of tick count - uint32_t ticks_count = xTaskGetTickCount(); - time->seconds = ticks_count / 1000; - time->nanoseconds = (ticks_count % 1000) * 1000000; + uint64_t ticks_count = xTaskGetTickCount(); + time->seconds = ticks_count / configTICK_RATE_HZ; + time->nanoseconds = ((ticks_count % configTICK_RATE_HZ) * 1000000000ul)/configTICK_RATE_HZ; - if (time->seconds + duration->seconds > EMBB_TIME_MAX_SECONDS) { + ticks_count = (uint64_t)(duration->seconds *configTICK_RATE_HZ + (duration->nanoseconds * configTICK_RATE_HZ / 1000000000ul)); + + if (ticks_count > UINT32_MAX || time->seconds + duration->seconds > EMBB_TIME_MAX_SECONDS) { return EMBB_OVERFLOW; } -- libgit2 0.26.0