b.a.t.m.a.n.lists.open-mesh.org archive mirror
 help / color / mirror / Atom feed
* [B.A.T.M.A.N.] [PATCH] Make batman timer functions thread safe
@ 2008-09-12 23:24 Sven Eckelmann
  2008-09-14  4:15 ` Marek Lindner
  0 siblings, 1 reply; 11+ messages in thread
From: Sven Eckelmann @ 2008-09-12 23:24 UTC (permalink / raw)
  To: b.a.t.m.a.n

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 */
-- 
1.6.0.1



^ permalink raw reply related	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2008-12-19 12:04 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-09-12 23:24 [B.A.T.M.A.N.] [PATCH] Make batman timer functions thread safe Sven Eckelmann
2008-09-14  4:15 ` Marek Lindner
2008-09-15  8:28   ` freifunk
2008-09-16 13:34     ` Marek Lindner
2008-09-17  9:00     ` Axel Neumann
2008-09-18  7:42       ` Stephan Enderlein (Freifunk Dresden)
2008-09-21 13:58         ` [B.A.T.M.A.N.] policy-routing-script issues Axel Neumann
2008-12-18 11:25           ` Stephan Enderlein (Freifunk Dresden)
2008-12-18 13:17             ` Bastian Bittorf
2008-12-19 10:08               ` Stephan Enderlein (Freifunk Dresden)
2008-12-19 12:04                 ` Gustavo Lindberg

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).