From patchwork Fri Sep 12 23:24:20 2008 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sven Eckelmann X-Patchwork-Id: 5347 Received: from john.hrz.tu-chemnitz.de (john.hrz.tu-chemnitz.de [134.109.132.2]) by open-mesh.net (8.13.4/8.13.4/Debian-3sarge3) with ESMTP id m8CNSfVQ014108 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT) for ; Sat, 13 Sep 2008 01:28:42 +0200 Received: from galba.hrz.tu-chemnitz.de ([134.109.133.156] helo=mailbox.hrz.tu-chemnitz.de) by john.hrz.tu-chemnitz.de with esmtps (TLSv1:AES256-SHA:256) (Exim 4.69) (envelope-from ) id 1KeHzt-0007yh-Tk for b.a.t.m.a.n@open-mesh.net; Sat, 13 Sep 2008 01:24:17 +0200 Received: from vpnclient-006.hrz.tu-chemnitz.de ([134.109.232.6] helo=sven-desktop) by mailbox.hrz.tu-chemnitz.de with smtp (Exim 4.69) (envelope-from ) id 1KeHzt-0001cC-L8 for b.a.t.m.a.n@open-mesh.net; Sat, 13 Sep 2008 01:24:17 +0200 Received: by sven-desktop (nbSMTP-1.00) for uid 1000 sven.eckelmann@gmx.de; Sat, 13 Sep 2008 01:24:20 +0200 (CEST) Date: Sat, 13 Sep 2008 01:24:20 +0200 From: Sven Eckelmann To: b.a.t.m.a.n@open-mesh.net Message-ID: <20080912232420.GA9669@sven-desktop.lazhur.ath.cx> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.18 (2008-05-17) X-Scan-Signature: 113af25641a082a643d611852bb712c6 Subject: [B.A.T.M.A.N.] [PATCH] Make batman timer functions thread safe X-BeenThere: b.a.t.m.a.n@open-mesh.net X-Mailman-Version: 2.1.5 Precedence: list Reply-To: The list for a Better Approach To Mobile Ad-hoc Networking List-Id: The list for a Better Approach To Mobile Ad-hoc Networking List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 12 Sep 2008 23:28:42 -0000 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 --- 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 */