aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorlloyd <[email protected]>2009-12-23 01:49:56 +0000
committerlloyd <[email protected]>2009-12-23 01:49:56 +0000
commitc6026d662ab26954460d5390934d6affa9c8127e (patch)
treef66561f6c4e9859f91a91a3064bb94f7607459f6 /src
parent887f2afa7628d88daef361ca00188edd4d882f6d (diff)
Fix compile of get_nanoseconds_clock for Windows.
Add macros for OS support of gmtime_r (Unix) and gmtime_s (Win32) to deal with thread-unsafety of std::gmtime. Only enable gmtime_r on Linux currently, but it's probably available pretty much everywhere (specified in pthreads, origininally, AFAICT).
Diffstat (limited to 'src')
-rw-r--r--src/build-data/os/linux.txt1
-rw-r--r--src/build-data/os/windows.txt1
-rw-r--r--src/utils/time.cpp14
3 files changed, 14 insertions, 2 deletions
diff --git a/src/build-data/os/linux.txt b/src/build-data/os/linux.txt
index b3c227533..2f59fb9d1 100644
--- a/src/build-data/os/linux.txt
+++ b/src/build-data/os/linux.txt
@@ -4,6 +4,7 @@ os_type unix
clock_gettime
gettimeofday
posix_mlock
+gmtime_r
</target_features>
# Is this correct?
diff --git a/src/build-data/os/windows.txt b/src/build-data/os/windows.txt
index 2e51afd0e..4d8879dd8 100644
--- a/src/build-data/os/windows.txt
+++ b/src/build-data/os/windows.txt
@@ -13,6 +13,7 @@ install_cmd_exec "copy"
<target_features>
win32_virtual_lock
win32_get_systemtime
+gmtime_s
</target_features>
<supports_shared>
diff --git a/src/utils/time.cpp b/src/utils/time.cpp
index 04c83c7bd..e1e7d1f20 100644
--- a/src/utils/time.cpp
+++ b/src/utils/time.cpp
@@ -64,10 +64,20 @@ std::tm time_t_to_tm(u64bit timer)
{
std::time_t time_val = static_cast<std::time_t>(timer);
+ std::tm tm;
+
+#if defined(BOTAN_TARGET_OS_HAS_GMTIME_S)
+ gmtime_s(&tm, &time_val); // Windows
+#elif defined(BOTAN_TARGET_OS_HAS_GMTIME_R)
+ gmtime_r(&time_val, &tm); // Unix/SUSv2
+#else
std::tm* tm_p = std::gmtime(&time_val);
if (tm_p == 0)
throw Encoding_Error("time_t_to_tm could not convert");
- return (*tm_p);
+ tm = *tm_p;
+#endif
+
+ return tm;
}
u64bit get_nanoseconds_clock()
@@ -85,7 +95,7 @@ u64bit get_nanoseconds_clock()
#elif defined(BOTAN_TARGET_OS_HAS_WIN32_GET_SYSTEMTIME)
// Returns time since January 1, 1601 in 100-ns increments
- struct FILETIME tv;
+ ::FILETIME tv;
::GetSystemTimeAsFileTime(&tv);
u64bit tstamp = (static_cast<u64bit>(tv.dwHighDateTime) << 32) |
tv.dwLowDateTime;