Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

better uniquify C symbol names

  • Loading branch information...
commit ded9cc234b10f93580d2a7cceb0135dfb3075b8f 1 parent 788eaca
@kmcallister authored
Showing with 23 additions and 10 deletions.
  1. +14 −8 System/GlobalLock/Internal.hs
  2. +9 −2 cbits/global.c
View
22 System/GlobalLock/Internal.hs
@@ -9,28 +9,34 @@ import Foreign.C
import Control.Monad
import Control.Concurrent.MVar
-{- Importing c_get_global_ptr with 'unsafe' decreases locking latency
- by about 50%. It's okay because that function just reads a C
- static global.
+{- Importing c_get_global with 'unsafe' decreases locking latency by
+ about 50%. It's okay because that function just reads a C static
+ global.
- c_set_global_ptr must not be imported 'unsafe' because it uses GCC
+ c_set_global must not be imported 'unsafe' because it uses GCC
atomic-operation builtins which, in the worst case, might call a
blocking library function. -}
-foreign import ccall unsafe c_get_global_ptr :: IO (Ptr ())
-foreign import ccall c_set_global_ptr :: Ptr () -> IO CInt
+{- If you are copying this file to your own Haskell project, see the
+ note in cbits/global.c regarding naming. -}
+
+foreign import ccall unsafe "hs_globalzmlock_get_global"
+ c_get_global :: IO (Ptr ())
+
+foreign import ccall "hs_globalzmlock_set_global"
+ c_set_global :: Ptr () -> IO CInt
set :: IO ()
set = do
mv <- newMVar ()
ptr <- newStablePtr mv
- ret <- c_set_global_ptr (castStablePtrToPtr ptr)
+ ret <- c_set_global (castStablePtrToPtr ptr)
when (ret == 0) $
freeStablePtr ptr
get :: IO (MVar ())
get = do
- p <- c_get_global_ptr
+ p <- c_get_global
if p /= nullPtr
then deRefStablePtr (castPtrToStablePtr p)
else set >> get
View
11 cbits/global.c
@@ -7,12 +7,19 @@
static void* global = 0;
+/*
+ If you are copying this file to your own Haskell project, you should
+ probably change these function names. I suggest replacing 'globalzmlock'
+ with your package's z-encoded name, following
+ <http://hackage.haskell.org/trac/ghc/wiki/Commentary/Compiler/SymbolNames>
+*/
+
// Imported 'unsafe' in Haskell code. Must not block!
-void* c_get_global_ptr(void) {
+void* hs_globalzmlock_get_global(void) {
return global;
}
-int c_set_global_ptr(void* new_global) {
+int hs_globalzmlock_set_global(void* new_global) {
// Set 'global', if it was previously zero.
void* old = __sync_val_compare_and_swap(&global, 0, new_global);
Please sign in to comment.
Something went wrong with that request. Please try again.