From patchwork Wed Jan 9 06:18:10 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wayne Xia X-Patchwork-Id: 210629 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 621F12C00D2 for ; Wed, 9 Jan 2013 17:20:44 +1100 (EST) Received: from localhost ([::1]:58577 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Tsp1m-0000ot-G1 for incoming@patchwork.ozlabs.org; Wed, 09 Jan 2013 01:20:42 -0500 Received: from eggs.gnu.org ([208.118.235.92]:36910) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Tsp1Z-0000gb-JE for qemu-devel@nongnu.org; Wed, 09 Jan 2013 01:20:30 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Tsp1Y-0000bE-08 for qemu-devel@nongnu.org; Wed, 09 Jan 2013 01:20:29 -0500 Received: from e28smtp04.in.ibm.com ([122.248.162.4]:42320) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Tsp1X-0000aV-9e for qemu-devel@nongnu.org; Wed, 09 Jan 2013 01:20:27 -0500 Received: from /spool/local by e28smtp04.in.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Wed, 9 Jan 2013 11:49:12 +0530 Received: from d28dlp01.in.ibm.com (9.184.220.126) by e28smtp04.in.ibm.com (192.168.1.134) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Wed, 9 Jan 2013 11:49:10 +0530 Received: from d28relay03.in.ibm.com (d28relay03.in.ibm.com [9.184.220.60]) by d28dlp01.in.ibm.com (Postfix) with ESMTP id D01D3E004C for ; Wed, 9 Jan 2013 11:50:34 +0530 (IST) Received: from d28av03.in.ibm.com (d28av03.in.ibm.com [9.184.220.65]) by d28relay03.in.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id r096KIXO49020932 for ; Wed, 9 Jan 2013 11:50:19 +0530 Received: from d28av03.in.ibm.com (loopback [127.0.0.1]) by d28av03.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id r096KIMJ011853 for ; Wed, 9 Jan 2013 17:20:19 +1100 Received: from RH63Wenchao ([9.77.181.189]) by d28av03.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id r096IE80004003; Wed, 9 Jan 2013 17:20:15 +1100 From: Wenchao Xia To: qemu-devel@nongnu.org Date: Wed, 9 Jan 2013 14:18:10 +0800 Message-Id: <1357712290-16496-2-git-send-email-xiawenc@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1357712290-16496-1-git-send-email-xiawenc@linux.vnet.ibm.com> References: <1357712290-16496-1-git-send-email-xiawenc@linux.vnet.ibm.com> X-Content-Scanned: Fidelis XPS MAILER x-cbid: 13010906-5564-0000-0000-0000061F00A9 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] X-Received-From: 122.248.162.4 Cc: pbonzini@redhat.com, aliguori@us.ibm.com, quintela@redhat.com, Wenchao Xia , stefanha@gmail.com Subject: [Qemu-devel] [PATCH 2/2] oslib-win32: add lock for localtime_r() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Also switch calling of localtime() to localtime_r() in qemu code. Signed-off-by: Wenchao Xia --- block.c | 10 ---------- block/vvfat.c | 14 +++++--------- hw/omap1.c | 2 +- oslib-win32.c | 4 +++- savevm.c | 4 ++-- vl.c | 9 +++------ 6 files changed, 14 insertions(+), 29 deletions(-) diff --git a/block.c b/block.c index 4e28c55..60873ea 100644 --- a/block.c +++ b/block.c @@ -3338,11 +3338,7 @@ char *get_human_readable_size(char *buf, int buf_size, int64_t size) char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn) { char buf1[128], date_buf[128], clock_buf[128]; -#ifdef _WIN32 - struct tm *ptm; -#else struct tm tm; -#endif time_t ti; int64_t secs; @@ -3352,15 +3348,9 @@ char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn) "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK"); } else { ti = sn->date_sec; -#ifdef _WIN32 - ptm = localtime(&ti); - strftime(date_buf, sizeof(date_buf), - "%Y-%m-%d %H:%M:%S", ptm); -#else localtime_r(&ti, &tm); strftime(date_buf, sizeof(date_buf), "%Y-%m-%d %H:%M:%S", &tm); -#endif secs = sn->vm_clock_nsec / 1000000000; snprintf(clock_buf, sizeof(clock_buf), "%02d:%02d:%02d.%03d", diff --git a/block/vvfat.c b/block/vvfat.c index 83706ce..4a56fc6 100644 --- a/block/vvfat.c +++ b/block/vvfat.c @@ -527,15 +527,11 @@ static inline uint8_t fat_chksum(const direntry_t* entry) } /* if return_time==0, this returns the fat_date, else the fat_time */ -static uint16_t fat_datetime(time_t time,int return_time) { - struct tm* t; -#ifdef _WIN32 - t=localtime(&time); /* this is not thread safe */ -#else - struct tm t1; - t = &t1; - localtime_r(&time,t); -#endif +static uint16_t fat_datetime(time_t time, int return_time) +{ + struct tm *t, t1; + + t = localtime_r(&time, &t1); if(return_time) return cpu_to_le16((t->tm_sec/2)|(t->tm_min<<5)|(t->tm_hour<<11)); return cpu_to_le16((t->tm_mday)|((t->tm_mon+1)<<5)|((t->tm_year-80)<<9)); diff --git a/hw/omap1.c b/hw/omap1.c index 8536e96..e85f2e2 100644 --- a/hw/omap1.c +++ b/hw/omap1.c @@ -2830,7 +2830,7 @@ static void omap_rtc_tick(void *opaque) s->round = 0; } - memcpy(&s->current_tm, localtime(&s->ti), sizeof(s->current_tm)); + localtime_r(&s->ti, &s->current_tm); if ((s->interrupts & 0x08) && s->ti == s->alarm_ti) { s->status |= 0x40; diff --git a/oslib-win32.c b/oslib-win32.c index 74e0c52..d96d053 100644 --- a/oslib-win32.c +++ b/oslib-win32.c @@ -90,15 +90,17 @@ struct tm *gmtime_r(const time_t *timep, struct tm *result) return p; } -/* FIXME: add proper locking */ +/* FIXME: add proper locking in MinGW */ struct tm *localtime_r(const time_t *timep, struct tm *result) { + g_static_mutex_lock(&time_lock); struct tm *p = localtime(timep); memset(result, 0, sizeof(*result)); if (p) { *result = *p; p = result; } + g_static_mutex_unlock(&time_lock); return p; } diff --git a/savevm.c b/savevm.c index 529d60e..ce18762 100644 --- a/savevm.c +++ b/savevm.c @@ -2095,7 +2095,7 @@ void do_savevm(Monitor *mon, const QDict *qdict) uint64_t vm_state_size; #ifdef _WIN32 struct _timeb tb; - struct tm *ptm; + struct tm *ptm, tm; #else struct timeval tv; struct tm tm; @@ -2151,7 +2151,7 @@ void do_savevm(Monitor *mon, const QDict *qdict) } else { #ifdef _WIN32 time_t t = tb.time; - ptm = localtime(&t); + ptm = localtime_r(&t, &tm); strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", ptm); #else /* cast below needed for OpenBSD where tv_sec is still 'long' */ diff --git a/vl.c b/vl.c index 8d7864c..e5da31c 100644 --- a/vl.c +++ b/vl.c @@ -448,21 +448,18 @@ StatusInfo *qmp_query_status(Error **errp) void qemu_get_timedate(struct tm *tm, int offset) { time_t ti; - struct tm *ret; time(&ti); ti += offset; if (rtc_date_offset == -1) { if (rtc_utc) - ret = gmtime_r(&ti, tm); + gmtime_r(&ti, tm); else - ret = localtime(&ti); + localtime_r(&ti, tm); } else { ti -= rtc_date_offset; - ret = gmtime_r(&ti, tm); + gmtime_r(&ti, tm); } - - memcpy(tm, ret, sizeof(struct tm)); } int qemu_timedate_diff(struct tm *tm)