-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replaces the unsafePerformIO solution, which is broken because of GHC bug #5558. This is the same idea used in the global-lock package.
- Loading branch information
1 parent
b29e134
commit 18a69c3
Showing
4 changed files
with
64 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
{-# LANGUAGE | ||
ForeignFunctionInterface #-} | ||
-- | Protect calls into DevIL with a global lock. | ||
module Propane.IO.Lock | ||
( lock | ||
) where | ||
|
||
import Foreign | ||
import Foreign.C | ||
import Control.Monad | ||
import Control.Concurrent.MVar | ||
|
||
|
||
foreign import ccall "hs_propane_get_global" | ||
c_get_global :: IO (Ptr ()) | ||
|
||
foreign import ccall "hs_propane_set_global" | ||
c_set_global :: Ptr () -> IO CInt | ||
|
||
|
||
set :: IO () | ||
set = do | ||
mv <- newMVar () | ||
ptr <- newStablePtr mv | ||
ret <- c_set_global (castStablePtrToPtr ptr) | ||
when (ret == 0) $ | ||
freeStablePtr ptr | ||
|
||
get :: IO (MVar ()) | ||
get = do | ||
p <- c_get_global | ||
if p == nullPtr | ||
then set >> get | ||
else deRefStablePtr (castPtrToStablePtr p) | ||
|
||
lock :: IO a -> IO a | ||
lock act = get >>= flip withMVar (const act) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Based on the global-lock package. | ||
|
||
// Atomic builtins were added in GCC 4.1. | ||
#if !defined(__GNUC__) \ | ||
|| (__GNUC__ < 4) \ | ||
|| (__GNUC__ == 4 && __GNUC_MINOR__ < 1) | ||
#error global-lock requires GCC 4.1 or later. | ||
#endif | ||
|
||
static void* global = 0; | ||
|
||
void* hs_propane_get_global(void) { | ||
return global; | ||
} | ||
|
||
int hs_propane_set_global(void* new_global) { | ||
void* old = __sync_val_compare_and_swap(&global, 0, new_global); | ||
return (old == 0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters