[Haskell-cafe] Haskell safe ccalls
Valentino Picotti
valentino.picotti at gmail.com
Thu Dec 17 15:02:31 UTC 2015
Hi everybody,
I’m new on this list and have a newbie question about Haskell FFI and safe calls.
I have written the following lines of C code that rely on the RTS:
Condition* init_cond(){
Condition* c = malloc(sizeof(Condition));
initCondition(c);
return c;
}
Mutex* init_mutex(){
Mutex* m = malloc(sizeof(Mutex));
initMutex(m);
return m;
}
void wait_condition(Condition* condition, Mutex* mutex) {
ACQUIRE_LOCK(mutex);
if(!waitCondition(condition, mutex)){
fprintf(stderr, "A: Wait failed!");
};
RELEASE_LOCK(mutex);
}
And the Haskell program is written as follow:
data Condition = Condition
data Mutex = Mutex
foreign import ccall "init_cond" init_cond :: IO (Ptr Condition)
foreign import ccall "init_mutex" init_mutex :: IO (Ptr Mutex)
foreign import ccall "wait_condition" wait_condition:: Ptr Condition -> Ptr Mutex -> IO ()
main :: IO ()
main = do
c <- init_cond
m <- init_mutex
wait_condition c m
putStrLn “Hello!”
The behaviour that i want from my application is the same as the following C program:
int main(int argc, char** argv){
pthread_cond_t c = PTHREAD_COND_INITIALIZER;
pthread_mutex_t m;
pthread_mutex_init(&m, 0);
pthread_mutex_lock(&m);
pthread_cond_wait(&c, &m);
printf(“I didn’t block!\n");
pthread_mutex_unlock(&m);
return 0;
}
What I expect is that the program doesn’t reach the putStrLn line, but it prints “Hello!” and terminates with exit code 0.
I can’t figure out what’s going on under the hood.
Can someone explain me what i’m missing? (I’m familiar with Capabilities, Tasks, Haskell threads)
Thanks,
Valentino P.
More information about the Haskell-Cafe
mailing list