The calculation inside of update_internal_clock aren't atomic and can lead to bogus time informations when many threads calling get_time_msec64 or get_time_msec.
Signed-off-by: Sven Eckelmann sven.eckelmann@gmx.de --- batman/posix/posix.c | 17 +++++++++++++++-- 1 files changed, 15 insertions(+), 2 deletions(-)
diff --git a/batman/posix/posix.c b/batman/posix/posix.c index dd24a65..042a2b9 100644 --- a/batman/posix/posix.c +++ b/batman/posix/posix.c @@ -49,6 +49,7 @@ static float system_tick;
uint8_t tunnel_running = 0;
+static pthread_mutex_t batman_clock_mutex = PTHREAD_MUTEX_INITIALIZER;
void update_internal_clock() { @@ -61,14 +62,26 @@ void update_internal_clock()
uint32_t get_time_msec() { + uint32_t time; + + pthread_mutex_lock(&batman_clock_mutex); update_internal_clock(); - return (uint32_t)(((float)(batman_clock_ticks) * 1000) / system_tick); + time = (uint32_t)(((float)(batman_clock_ticks) * 1000) / system_tick); + pthread_mutex_unlock(&batman_clock_mutex); + + return time; }
uint64_t get_time_msec64() { + uint64_t time; + + pthread_mutex_lock(&batman_clock_mutex); update_internal_clock(); - return (uint64_t)(((float)(batman_clock_ticks) * 1000) / system_tick); + time = (uint64_t)(((float)(batman_clock_ticks) * 1000) / system_tick); + pthread_mutex_unlock(&batman_clock_mutex); + + return time; }
/* batman animation */