But on windows, localtime and gmtime _are_ threadsafe.

svn:r3654
This commit is contained in:
Nick Mathewson 2005-02-22 07:09:17 +00:00
parent 70c3580f81
commit d37f4dd8a9

View File

@ -753,14 +753,18 @@ void tor_gettimeofday(struct timeval *timeval) {
return; return;
} }
#if defined(TOR_IS_MULTITHREADED) && !defined(MS_WINDOWS)
#define TIME_FNS_NEED_LOCKS
#endif
#ifndef HAVE_LOCALTIME_R #ifndef HAVE_LOCALTIME_R
#ifdef TIME_FNS_NEED_LOCKS
struct tm *tor_localtime_r(const time_t *timep, struct tm *result) struct tm *tor_localtime_r(const time_t *timep, struct tm *result)
{ {
struct tm *r; struct tm *r;
#ifdef TOR_IS_MULTITHREADED
static tor_mutex_t *m=NULL; static tor_mutex_t *m=NULL;
if (!m) { m=tor_mutex_new(); } if (!m) { m=tor_mutex_new(); }
#endif
tor_assert(result); tor_assert(result);
tor_mutex_acquire(m); tor_mutex_acquire(m);
r = localtime(timep); r = localtime(timep);
@ -768,16 +772,25 @@ struct tm *tor_localtime_r(const time_t *timep, struct tm *result)
tor_mutex_release(m); tor_mutex_release(m);
return result; return result;
} }
#else
struct tm *tor_localtime_r(const time_t *timep, struct tm *result)
{
struct tm *r;
tor_assert(result);
r = localtime(timep);
memcpy(result, r, sizeof(struct tm));
return result;
}
#endif
#endif #endif
#ifndef HAVE_GMTIME_R #ifndef HAVE_GMTIME_R
#ifdef TIME_FNS_NEED_LOCKS
struct tm *tor_gmtime_r(const time_t *timep, struct tm *result) struct tm *tor_gmtime_r(const time_t *timep, struct tm *result)
{ {
struct tm *r; struct tm *r;
#ifdef TOR_IS_MULTITHREADED
static tor_mutex_t *m=NULL; static tor_mutex_t *m=NULL;
if (!m) { m=tor_mutex_new(); } if (!m) { m=tor_mutex_new(); }
#endif
tor_assert(result); tor_assert(result);
tor_mutex_acquire(m); tor_mutex_acquire(m);
r = gmtime(timep); r = gmtime(timep);
@ -785,6 +798,16 @@ struct tm *tor_gmtime_r(const time_t *timep, struct tm *result)
tor_mutex_release(m); tor_mutex_release(m);
return result; return result;
} }
#else
struct tm *tor_gmtime_r(const time_t *timep, struct tm *result)
{
struct tm *r;
tor_assert(result);
r = gmtime(timep);
memcpy(result, r, sizeof(struct tm));
return result;
}
#endif
#endif #endif
#ifdef USE_WIN32_THREADS #ifdef USE_WIN32_THREADS