[mercury-users] problem with signals

pieter.laeremans at student.kuleuven.ac.be pieter.laeremans at student.kuleuven.ac.be
Thu Mar 6 08:40:45 AEDT 2003


Hello,

Sorry if this mail arrives twice but my first one didn't get through,
I think (I didn't receive it).

I need to exchange data between a c-library (called liblnp) and a mercury
program throug signal handling.  

Data from the library to the mercury programme is passed
asynchronously.  But the intent is to write a primitive in mercury to
wait for the data.  When the library has datae it invokes a
callback handler.  So I wrote a callback handler and registered it
with the library.

:- pragma foreign_code("C", "

	void callbackhandler(const unsigned char* data, unsigned char length){
	
	....

         }
").

Now I want to have a mercury predicate like this :

:- pred wait_for_data(string::out, io__state::di, io__state::uo) is det.
:- pragma foreign_proc("C", wait_for_data(Data::out, IO0::di,IO::uo), [promise_pure], "
      sem_wait(sem);     
      IO = IO0;
      Data = (MR_String) buffer;
").

the intent was that the callbackhandler places it data into a shared
buffer and then performs a sem_post(sem) so that wait_for_data
predicate could continue it 's execution.

Eventually this worked fine. (Although this might be risky because I
never created a thread ).

Now I would like to prevent that the callbackhandler would be invoked
while I'm processing the data.  Frome the source code of liblnp I have
inferred that the supplied callback function is registered as signal
handler for the SIGIO signal.  

My first approach was to let the callbackhandler wait on the same
semaphore.  But it did not delivered the expected result.  Because the
suspended mercury code didn't awake while the callbackhandler was
still active.   So that leaves semaphores out as an option.

I envisaged a second approach, namely to block the SIGIO signal while
I'm processing the data.  I tested this on a prototype c-only
program.  That works fine however when I wanted to do exactly the same
in mercury by including signal.h and using statements such as this :

:- pragma foreign_proc("C", wait_for_data(Data::out, IO0::di,IO::uo), [promise_pure], "
		      sem_wait(sem);
		      sigemptyset(&blockset);
		      sigaddset(&blockset, SIGIO);
		      sigprocmask(SIG_BLOCK, &blockset, NULL); 		      
		      IO = IO0;
		      Data = (MR_String) buffer;
").


The mercury compiler issued the following errors :

legolibrary.m:174: warning: implicit declaration of function `sigemptyset'
legolibrary.m:175: warning: implicit declaration of function `sigaddset'
legolibrary.m:176: warning: implicit declaration of function `sigprocmask'
legolibrary.m:176: `SIG_BLOCK' undeclared (first use in this function)

Does anyone has an explanation for this ?
Or maybe an idea for an alternative to tackle the problem ?

thanks in advance,

Pieter

P.S. : sorry for my bad English.



--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list