Permalink
Browse files

Updated documentation

  • Loading branch information...
1 parent 0650a4c commit 523415541a4000b92f0c0cd700de1d2aeecb13d4 @sanketr committed Feb 26, 2012
Showing with 11 additions and 6 deletions.
  1. +1 −1 Makefile
  2. +1 −1 README
  3. +8 −3 T.hs
  4. +1 −1 test.c
View
@@ -6,4 +6,4 @@ install:
rm -rf *stub* *.hi
clean:
- rm -rf *.o c t T *.hi *stub*
+ rm -rf *.o T *.hi *stub*
View
2 README
@@ -2,7 +2,7 @@ This test reproduces an issue with C FFI I have seen - the number of worker thre
To reproduce the issue:
-- Set nThreads in T.hs (line 52) to a high number, for example, 10. That will kick off nThreads threads in C FFI
+- Set nThreads in T.hs (line 51) to a high number, for example, 10. That will kick off nThreads threads in C FFI
- Compile the code (need gcc too): make
View
11 T.hs
@@ -17,6 +17,8 @@ foreign import ccall "wrapper"
foreign import ccall safe "mt.h sendSignal"
sendSignal :: CShort -> IO()
+foreign import ccall safe "test.h initThreads"
+ initThreads :: CInt -> Ptr (FunPtr (IO())) -> IO()
syncWithC :: MVar CInt -> MVar CInt -> CInt -> IO ()
syncWithC m1 m2 x = do
@@ -41,19 +43,22 @@ timerevent m1 m2 t = run where
where
listOfThreads = [0..fromIntegral $ (length m1) - 1]
-foreign import ccall safe "test.h initThreads"
- initThreads :: CInt -> Ptr (FunPtr (IO())) -> IO()
-
getPtr :: (SV.Storable a) => SV.Vector a -> Ptr a
getPtr = unsafeForeignPtrToPtr . (\(x,_,_) -> x) . SV.unsafeToForeignPtr
main :: IO ()
main = do
let nThreads = 12
+ -- create two mvar lists for C FFI threads
m1 <- mapM (const newEmptyMVar) [1..nThreads] :: IO [MVar CInt]
m2 <- mapM (const newEmptyMVar) [1..nThreads] :: IO [MVar CInt]
+ -- create callback functions for each of C thread - it will call back syncWithC with no arguments
fnptrs <- mapM (\(x,y) -> syncWithCWrap $ syncWithC x y 0) (zip m1 m2)
+ -- create a storable vector of function ptrs - we will pass ptr to function ptrs to C FFI
let vfnptrs = SV.fromList fnptrs
+ -- kick off C FFI - fork in background
forkIO $ initThreads nThreads (getPtr vfnptrs)
+ -- kick off timer thread to coordinate with C FFI threads - every ~0.5 seconds, it
+ -- will sendSignal function in C FFI for each thread. sendSignal calls back syncWithC
timerevent m1 m2 500000
return ()
View
2 test.c
@@ -29,7 +29,7 @@ void initThreads(int nThreads, FnPtr* fns)
void* doSomething(void* threadid){
int i = *(int*) threadid;
- printf("ThreadId %i\n",i);
+ printf("Started thread %i\n",i);
int j;
//start filling the buffers here
for(j=0; j < MAXITERATIONS;j++){

0 comments on commit 5234155

Please sign in to comment.