Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed the pointer bug as pointed by Simon Marlow

  • Loading branch information...
commit 8d4e2a5c4454b37ac146f9c784c15abc2d7bbf9a 1 parent 4ee5192
authored

Showing 1 changed file with 4 additions and 3 deletions. Show diff stats Hide diff stats

  1. +4 3 T.hs
7 T.hs
@@ -7,6 +7,7 @@ import Foreign.Ptr (Ptr, FunPtr, castPtr)
7 7 import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
8 8 import Foreign.C.Types (CInt,CUInt,CShort, CFloat,CDouble,CChar)
9 9 import Foreign.C
  10 +import Foreign.Marshal.Array (newArray)
10 11 import qualified Data.Vector.Storable as SV (Storable, Vector, fromList, unsafeToForeignPtr)
11 12
12 13 -- a "wrapper" import is a converter for converting a Haskell
@@ -48,16 +49,16 @@ getPtr = unsafeForeignPtrToPtr . (\(x,_,_) -> x) . SV.unsafeToForeignPtr
48 49
49 50 main :: IO ()
50 51 main = do
51   - let nThreads = 30
  52 + let nThreads = 5
52 53 -- create two mvar lists for C FFI threads
53 54 m1 <- mapM (const newEmptyMVar) [1..nThreads] :: IO [MVar CInt]
54 55 m2 <- mapM (const newEmptyMVar) [1..nThreads] :: IO [MVar CInt]
55 56 -- create callback functions for each of C thread - it will call back syncWithC with no arguments
56 57 fnptrs <- mapM (\(x,y) -> syncWithCWrap $ syncWithC x y 0) (zip m1 m2)
57 58 -- create a storable vector of function ptrs - we will pass ptr to function ptrs to C FFI
58   - let vfnptrs = SV.fromList fnptrs
  59 + vfnptrs <- newArray fnptrs
59 60 -- kick off C FFI - fork in background
60   - forkIO $ initThreads nThreads (getPtr vfnptrs)
  61 + forkIO $ initThreads nThreads vfnptrs
61 62 -- kick off timer thread to coordinate with C FFI threads - every ~0.5 seconds, it
62 63 -- will sendSignal function in C FFI for each thread. sendSignal calls back syncWithC
63 64 timerevent m1 m2 500

0 comments on commit 8d4e2a5

Please sign in to comment.
Something went wrong with that request. Please try again.