From f7a561f2201a2ac7e3e3a472b76647f66b00f7ef Mon Sep 17 00:00:00 2001 From: "wtchang%redhat.com" Date: Thu, 30 Nov 2006 21:22:55 +0000 Subject: [PATCH] Bugzilla Bug 354593: fixed a race condition in the creation of the lock that protects localtime() by creating the lock during NSPR initialization. r=aleksey.sanin,nelson.bolyard. The patch is contributed by Mark Stevans . Modified files: primpl.h prinit.c prtime.c ptthread.c --- nsprpub/pr/include/private/primpl.h | 2 ++ nsprpub/pr/src/misc/prinit.c | 2 ++ nsprpub/pr/src/misc/prtime.c | 23 +++++++++++++++++------ nsprpub/pr/src/pthreads/ptthread.c | 1 + 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/nsprpub/pr/include/private/primpl.h b/nsprpub/pr/include/private/primpl.h index 1565d1842b8..ca9e7c7c0fc 100644 --- a/nsprpub/pr/include/private/primpl.h +++ b/nsprpub/pr/include/private/primpl.h @@ -1792,12 +1792,14 @@ extern void _PR_InitLinker(void); extern void _PR_InitAtomic(void); extern void _PR_InitCPUs(void); extern void _PR_InitDtoa(void); +extern void _PR_InitTime(void); extern void _PR_InitMW(void); extern void _PR_InitRWLocks(void); extern void _PR_NotifyCondVar(PRCondVar *cvar, PRThread *me); extern void _PR_CleanupThread(PRThread *thread); extern void _PR_CleanupCallOnce(void); extern void _PR_CleanupMW(void); +extern void _PR_CleanupTime(void); extern void _PR_CleanupDtoa(void); extern void _PR_ShutdownLinker(void); extern void _PR_CleanupEnv(void); diff --git a/nsprpub/pr/src/misc/prinit.c b/nsprpub/pr/src/misc/prinit.c index 3fb92f18586..9ab3ddc9958 100644 --- a/nsprpub/pr/src/misc/prinit.c +++ b/nsprpub/pr/src/misc/prinit.c @@ -241,6 +241,7 @@ static void _PR_InitStuff(void) _PR_InitLinker(); _PR_InitCallOnce(); _PR_InitDtoa(); + _PR_InitTime(); _PR_InitMW(); _PR_InitRWLocks(); @@ -420,6 +421,7 @@ PR_IMPLEMENT(PRStatus) PR_Cleanup() #endif _PR_CleanupMW(); + _PR_CleanupTime(); _PR_CleanupDtoa(); _PR_CleanupCallOnce(); _PR_ShutdownLinker(); diff --git a/nsprpub/pr/src/misc/prtime.c b/nsprpub/pr/src/misc/prtime.c index 57761970e40..ce1e0b5ca8d 100644 --- a/nsprpub/pr/src/misc/prtime.c +++ b/nsprpub/pr/src/misc/prtime.c @@ -547,6 +547,7 @@ PR_NormalizeTime(PRExplodedTime *time, PRTimeParamFn params) extern struct tm *Maclocaltime(const time_t * t); #endif +#define _PR_HAVE_LOCALTIME_MONITOR 1 static PRLock *monitor = NULL; static struct tm *MT_safe_localtime(const time_t *clock, struct tm *result) @@ -556,12 +557,7 @@ static struct tm *MT_safe_localtime(const time_t *clock, struct tm *result) * against NSPR threads only when the * NSPR thread system is activated. */ - if (needLock) { - if (monitor == NULL) { - monitor = PR_NewLock(); - } - PR_Lock(monitor); - } + if (needLock) PR_Lock(monitor); /* * Microsoft (all flavors) localtime() returns a NULL pointer if 'clock' @@ -606,6 +602,21 @@ static struct tm *MT_safe_localtime(const time_t *clock, struct tm *result) #endif /* definition of MT_safe_localtime() */ +void _PR_InitTime(void) +{ +#ifdef _PR_HAVE_LOCALTIME_MONITOR + monitor = PR_NewLock(); +#endif +} + +void _PR_CleanupTime(void) +{ +#ifdef _PR_HAVE_LOCALTIME_MONITOR + PR_DestroyLock(monitor); + monitor = NULL; +#endif +} + #if defined(XP_UNIX) || defined(XP_PC) || defined(XP_BEOS) PR_IMPLEMENT(PRTimeParameters) diff --git a/nsprpub/pr/src/pthreads/ptthread.c b/nsprpub/pr/src/pthreads/ptthread.c index 3a8303aec06..edefb443eda 100644 --- a/nsprpub/pr/src/pthreads/ptthread.c +++ b/nsprpub/pr/src/pthreads/ptthread.c @@ -938,6 +938,7 @@ PR_IMPLEMENT(PRStatus) PR_Cleanup(void) PR_Unlock(pt_book.ml); _PR_CleanupMW(); + _PR_CleanupTime(); _PR_CleanupDtoa(); _PR_CleanupCallOnce(); _PR_ShutdownLinker();