-- "errno" type
-- ------------
--- import of C function that gives address of errno
--- This function exists because errno is a variable on some systems, but on
--- Windows it is a macro for a function...
--- [yes, global variables and thread safety don't really go hand-in-hand. -- sof]
-#ifdef __NHC__
-foreign import ccall unsafe "errno.h &errno" _errno :: Ptr CInt
-#else
-foreign import ccall unsafe "HsBase.h ghcErrno" _errno :: Ptr CInt
-#endif
-
-- Haskell representation for "errno" values
--
newtype Errno = Errno CInt
-- yield the current thread's "errno" value
--
getErrno :: IO Errno
-getErrno = do e <- peek _errno; return (Errno e)
+
+-- We must call a C function to get the value of errno in general. On
+-- threaded systems, errno is hidden behind a C macro so that each OS
+-- thread gets its own copy.
+#ifdef __NHC__
+getErrno = do e <- peek _errno; return (Errno e)
+foreign import ccall unsafe "errno.h &errno" _errno :: IO (Ptr CInt)
+#else
+getErrno = do e <- get_errno; return (Errno e)
+foreign import ccall unsafe "HsBase.h __hscore_get_errno" get_errno :: IO CInt
+#endif
-- set the current thread's "errno" value to 0
--
resetErrno :: IO ()
-resetErrno = poke _errno 0
+-- Again, setting errno has to be done via a C function.
+#ifdef __NHC__
+resetErrno = poke _errno 0
+#else
+resetErrno = set_errno 0
+foreign import ccall unsafe "HsBase.h __hscore_set_errno" set_errno :: CInt -> IO ()
+#endif
-- throw current "errno" value
-- ---------------------------
+++ /dev/null
-/*
- * (c) The University of Glasgow, 2000-2001
- *
- * $Id: errno.c,v 1.5 2002/09/25 15:24:07 simonmar Exp $
- *
- * GHC Error Number Conversion
- */
-
-#include "HsBase.h"
-
-/* Raw errno */
-/* Covers up the fact that on Windows this is a function */
-
-int *ghcErrno(void) {
- return &errno;
-}
#include "timeUtils.h"
#endif
-/* in ghc_errno.c */
-int *ghcErrno(void);
-
/* in system.c */
HsInt systemCmd(HsAddr cmd);
# endif
#endif
+INLINE int __hscore_get_errno(void) { return errno; }
+INLINE int __hscore_set_errno(int e) { errno = e; }
+
#if !defined(_MSC_VER)
INLINE int __hscore_s_isreg(m) { return S_ISREG(m); }
INLINE int __hscore_s_isdir(m) { return S_ISDIR(m); }
+++ /dev/null
-/* -----------------------------------------------------------------------------
- * $Id: ghc_errno.h,v 1.1 2001/06/28 14:15:04 simonmar Exp $
- *
- * (c) The GHC Team 2001
- *
- * Haskell-usable version of errno
- *
- * ---------------------------------------------------------------------------*/
-
-#ifndef GHCERRNO_H
-#define GHCERRNO_H
-
-int *ghcErrno(void);
-
-#endif