[project @ 2000-03-09 06:14:38 by andy]
[ghc-hetmet.git] / ghc / lib / std / CPUTime.lhs
index 010f556..d1d7179 100644 (file)
@@ -4,40 +4,27 @@
 \section[CPUTime]{Haskell 1.4 CPU Time Library}
 
 \begin{code}
-{-# OPTIONS -fno-implicit-prelude -#include "cbits/stgio.h" #-}
+{-# OPTIONS -#include "cbits/stgio.h" #-}
 
 module CPUTime 
        (
          getCPUTime,       -- :: IO Integer
         cpuTimePrecision  -- :: Integer
         ) where
+\end{code}
 
-#ifdef __HUGS__
-import PreludeBuiltin
-#else
-import PrelBase
-import PrelArr         ( ByteArray(..), newIntArray, unsafeFreezeByteArray )
-import PrelMaybe
-import PrelNum
-import PrelNumExtra
-import PrelAddr
-import PrelIOBase
-import PrelST
-#endif
-import IO              ( fail )
-import Ratio
 
-#ifdef __HUGS__
-#define cat2(x,y)  x/**/y
-#define CCALL(fun) cat2(prim_,fun)
-#define stToIO id
-#define sizeof_int64 8
-#else
-#define CCALL(fun) _ccall_ fun
-#define const_BUFSIZ ``BUFSIZ''
-#define primPackString
-#endif
+#ifndef __HUGS__
 
+\begin{code}
+import Prelude         -- To generate the dependency
+import PrelGHC         ( indexIntArray# )
+import PrelBase                ( Int(..) )
+import PrelByteArr     ( ByteArray(..), newIntArray, unsafeFreezeByteArray )
+import PrelNum         ( fromInt )
+import PrelIOBase      ( IOError(..), IOErrorType( UnsupportedOperation ), 
+                         unsafePerformIO, stToIO )
+import Ratio
 \end{code}
 
 Computation @getCPUTime@ returns the number of picoseconds CPU time
@@ -49,56 +36,51 @@ in CPU time that the implementation can record, and is given as an
 integral number of picoseconds.
 
 \begin{code}
-#ifdef __HUGS__
-
 getCPUTime :: IO Integer
 getCPUTime = do
-    marr <- primNewByteArray (sizeof_int * 4)
-    ptr  <- CCALL(getCPUTime) marr
-    if (ptr /= nullAddr) then do
-        x0 <- primReadIntArray marr 0
-        x1 <- primReadIntArray marr 1
-        x2 <- primReadIntArray marr 2
-        x3 <- primReadIntArray marr 3
-        return ((fromIntegral x0 * 1000000000 + fromIntegral  x1 + 
-                fromIntegral x2 * 1000000000 + fromIntegral  x3)
-              * 1000)
-      else
-       fail (IOError Nothing UnsupportedOperation "getCPUTime"
-               "can't get CPU time")
-
-#else
-
-getCPUTime :: IO Integer
-getCPUTime = 
-    stToIO (newIntArray (0,3))         >>= \ marr ->
-    stToIO (unsafeFreezeByteArray marr)        >>= \ barr@(ByteArray _ frozen#) ->
-    _ccall_ getCPUTime barr            >>= \ ptr ->
-    if (ptr::Addr) /= ``NULL'' then
+    marr <- stToIO (newIntArray ((0::Int),3))
+    barr <- stToIO (unsafeFreezeByteArray marr)
+    rc   <- primGetCPUTime barr
+    if rc /= 0 then
+      case barr of
+       ByteArray _ _ frozen# -> -- avoid bounds checking
         return ((fromIntegral (I# (indexIntArray# frozen# 0#)) * 1000000000 + 
                  fromIntegral (I# (indexIntArray# frozen# 1#)) + 
                 fromIntegral (I# (indexIntArray# frozen# 2#)) * 1000000000 + 
                  fromIntegral (I# (indexIntArray# frozen# 3#))) * 1000)
-    else
-       fail (IOError Nothing UnsupportedOperation "getCPUTime"
-               "can't get CPU time")
-
-#endif
+     else
+       ioError (IOError Nothing UnsupportedOperation 
+                        "getCPUTime"
+                        "can't get CPU time")
 
 cpuTimePrecision :: Integer
 cpuTimePrecision = round ((1000000000000::Integer) % 
-                          fromInt (unsafePerformIO (CCALL(clockTicks) )))
+                          fromInt (unsafePerformIO clockTicks))
+
+foreign import "libHS_cbits" "getCPUTime" unsafe primGetCPUTime :: ByteArray Int -> IO Int
+foreign import "libHS_cbits" "clockTicks" clockTicks :: IO Int
+
 \end{code}
 
+#else
+
 \begin{code}
-#ifdef __HUGS__
+import Prelude
+import privileged Prelude ( nh_getCPUtime
+                         , nh_getCPUprec
+                         , unsafePerformIO
+                         )
 
-sizeof_int = 4
+getCPUTime :: IO Integer
+getCPUTime 
+   = do seconds <- nh_getCPUtime
+        return (round (seconds * 1.0e+12))
 
-foreign import stdcall "libHS_cbits.so" "getCPUTime" prim_getCPUTime :: Bytes -> IO Addr
-foreign import stdcall "libHS_cbits.so" "clockTicks" prim_clockTicks :: IO Int
+cpuTimePrecision :: Integer
+cpuTimePrecision
+   = unsafePerformIO (
+        do resolution <- nh_getCPUprec
+           return (round (resolution * 1.0e+12))
+     )
+\end{code} 
 #endif
-\end{code}
-
-