[Gambas-user] pigpio c library callback gives segmentation fault (11)

Benoît Minisini g4mba5 at gmail.com
Mon Nov 19 20:15:43 CET 2018


Le 19/11/2018 à 19:53, Steve a écrit :
> The basic principal with qsort is to call back for sorting information 
> while gambas is in the qsort call.
> 
> I have no problem with doing the equivalent code from the c module in 
> question.
> 
> This c module sets up a signal. When the signal is received it executes 
> the callback code in the c module.
> 
> I used stdout to display the called addresses and data during the signal 
> initiated callbaak which are the same as before. Nothing got corrupted 
> in the c execution and the thread id is the same.
> 
> The problem here seems to be that gambas is in its event loop at the 
> point when the signal occurs rather than executing something in the c 
> module.
> 

Ah ok, I didn't get it! I didn't see any signal code in the library you 
sent me, maybe I missed it.

Of course you cannot run the interpreter from a signal handler, directly 
or indirectly. You should even run almost nothing from a signal handler.

Same thing for a thread.

Components written in C or C++ get an API from the interpreter that 
allow them to deal with these sorts of things.

But in a library, it's more difficult.

You have to use the pipe system call to create two pipe file descriptors.

The first one will be used by the signal handler to write something 
(normally a single byte).

The second one will be used to read that byte to know that something 
happened in the signal handler.

The trick is to pass the second file descriptor to the main Gambas code 
so that it can watch it, using the hidden syntax:

hFile = Open "." & CStr(thePipeFileDescriptor) For Read Watch As 
"SignalHandler"

Then each time your signal handler writes to the pipe, you will get a 
Read event in the Gambas code with something to read on the pipe.

If you want to send a lot of data, you have to create a buffer for that, 
don't use the pipe.

Regards,

-- 
Benoît Minisini


More information about the User mailing list