Skip to content

Commit

Permalink
Fixed Monotic and Realtime clocks (#17).
Browse files Browse the repository at this point in the history
  • Loading branch information
hadronized committed Feb 8, 2015
1 parent 4c214d3 commit 20da622
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
19 changes: 9 additions & 10 deletions System/Clock.hsc
Expand Up @@ -13,11 +13,12 @@ module System.Clock
, diffTimeSpec
) where

import Control.Applicative
import Data.Int
import Data.Typeable (Typeable)
import Foreign.Ptr
import Foreign.Storable
import Foreign.Marshal.Alloc
import Data.Typeable (Typeable)
import GHC.Generics (Generic)

#if defined(_WIN32)
Expand Down Expand Up @@ -139,17 +140,15 @@ data TimeSpec = TimeSpec

#if defined(_WIN32)
instance Storable TimeSpec where
sizeOf _ = #{size long} * 2
alignment _ = #{alignment long}
sizeOf _ = sizeOf (undefined :: Int64) * 2
alignment _ = alignment (undefined :: Int64)
poke ptr ts = do
let xs :: #{type long} = fromIntegral $ sec ts
xn :: #{type long} = fromIntegral $ nsec ts
pokeByteOff ptr (0 * #size long) xs
pokeByteOff ptr (1 * #size long) xn
pokeByteOff ptr 0 (sec ts)
pokeByteOff ptr (sizeOf (undefined :: Int64)) (nsec ts)
peek ptr = do
xs :: #{type time_t} <- peekByteOff ptr (0 * #size long)
xn :: #{type long} <- peekByteOff ptr (1 * #size long)
return $ TimeSpec xs xn
TimeSpec
<$> peekByteOff ptr 0
<*> peekByteOff ptr (sizeOf (undefined :: Int64))
#else
instance Storable TimeSpec where
sizeOf _ = #{size struct timespec}
Expand Down
18 changes: 9 additions & 9 deletions cbits/hs_clock_win32.c
Expand Up @@ -22,13 +22,13 @@ static ULONGLONG to_quad_100ns(FILETIME ft)
return li.QuadPart;
}

static void to_timespec_from_100ns(ULONGLONG t_100ns, long *t)
static void to_timespec_from_100ns(ULONGLONG t_100ns, long long *t)
{
t[0] = (long)(t_100ns / 10000000UL);
t[1] = 100*(long)(t_100ns % 10000000UL);
}

void hs_clock_win32_gettime_monotonic(long* t)
void hs_clock_win32_gettime_monotonic(long long* t)
{
LARGE_INTEGER time;
LARGE_INTEGER frequency;
Expand All @@ -40,7 +40,7 @@ void hs_clock_win32_gettime_monotonic(long* t)
t[1] = ticks_to_nanos(time.QuadPart % frequency.QuadPart, frequency.QuadPart);
}

void hs_clock_win32_gettime_realtime(long* t)
void hs_clock_win32_gettime_realtime(long long* t)
{
FILETIME ft;
ULONGLONG tmp;
Expand All @@ -53,7 +53,7 @@ void hs_clock_win32_gettime_realtime(long* t)
to_timespec_from_100ns(tmp, t);
}

void hs_clock_win32_gettime_processtime(long* t)
void hs_clock_win32_gettime_processtime(long long* t)
{
FILETIME creation_time, exit_time, kernel_time, user_time;
ULONGLONG time;
Expand All @@ -65,7 +65,7 @@ void hs_clock_win32_gettime_processtime(long* t)
to_timespec_from_100ns(time, t);
}

void hs_clock_win32_gettime_threadtime(long* t)
void hs_clock_win32_gettime_threadtime(long long* t)
{
FILETIME creation_time, exit_time, kernel_time, user_time;
ULONGLONG time;
Expand All @@ -77,7 +77,7 @@ void hs_clock_win32_gettime_threadtime(long* t)
to_timespec_from_100ns(time, t);
}

void hs_clock_win32_getres_monotonic(long* t)
void hs_clock_win32_getres_monotonic(long long* t)
{
LARGE_INTEGER frequency;
QueryPerformanceFrequency(&frequency);
Expand All @@ -87,19 +87,19 @@ void hs_clock_win32_getres_monotonic(long* t)
t[1] = resolution % U64(1000000000);
}

void hs_clock_win32_getres_realtime(long* t)
void hs_clock_win32_getres_realtime(long long* t)
{
t[0] = 0;
t[1] = 100;
}

void hs_clock_win32_getres_processtime(long* t)
void hs_clock_win32_getres_processtime(long long* t)
{
t[0] = 0;
t[1] = 100;
}

void hs_clock_win32_getres_threadtime(long* t)
void hs_clock_win32_getres_threadtime(long long* t)
{
t[0] = 0;
t[1] = 100;
Expand Down

0 comments on commit 20da622

Please sign in to comment.