[comp.windows.x] SIGIO/SIGALRM in conflict with XLib ?

frodek@forit.forut.no (Frode Kileng) (04/18/91)

Configuration:
	Sparc Station 1
	Openwindows 2
	Olwm.

I have a program for displaying X-ray images.  The program has been up running
for a couple of months at this time.  I'm now working with an audio expansion to
the program.

The audio part is set up to receive SIGIO interrupts when it is time to
read/write from/to the audio device.  The program also receives 
SIGALRM interrupts that I use to display the peek level of the audio and
to show record/play time.  At each SIGALRM interrupt I send 1-2 draw requests
to the X server.  The program run

Both the imaging part and the audio part work if I run them one at a time.
BUT it crash every time when I'm recording/playing and at the same time use
the imaging part.  The program crashes with the different X error messages :
  
  X Error BadRequest		X Error:  BadValue
    Request Major code 254 ()	  Request Major code 89 ()
    Request Minor code 254	  Request Minor code 0
    ResourceID 0xa00028		  ResourceID 0xa0002a
    Error Serial #285		  Error Serial #310
    Current Serial #312		  Current Serial #312

  X Error:  BadValue		X Error BadLength
    Request Major code 89 ()	  ..............
    Request Minor code 0	  ..............
    ResourceID 0xa0002a
    Error Serial #310
    Current Serial #312

Some times the program crash with no error messages at all.

My guess is that the program is doing a XLib call from the imaging part
and at the same time receives a SIGALRM interrupt
generating another XLib request before the first request is carried 
through.


I will appreciate any help on this problem.


Frode Kileng
Frode.Kileng@fbt.tf.tele.no	(frodek@forit.forut.no)

mouse@lightning.mcrcim.mcgill.EDU (der Mouse) (05/02/91)

> I have a program for displaying X-ray images.  The program has been
> up running for a couple of months at this time.  I'm now working with
> an audio expansion to the program.

> The audio part is set up to receive SIGIO interrupts when it is time
> to read/write from/to the audio device.  The program also receives
> SIGALRM interrupts that I use to display the peek level of the audio
> and to show record/play time.  At each SIGALRM interrupt I send 1-2
> draw requests to the X server.

Danger, danger.

> Both the imaging part and the audio part work if I run them one at a
> time.  BUT it crash every time when I'm recording/playing and at the
> same time use the imaging part.

Yup.

> My guess is that the program is doing a XLib call from the imaging
> part and at the same time receives a SIGALRM interrupt generating
> another XLib request before the first request is carried through.

This is basically correct.

Almost all implementations of Xlib do not contain code to interlock
correctly with themselves when called both from signal handlers and
from the code that can be interrupted by the signal.

Basically, you have to make sure that you never make Xlib calls from
within a signal handler that might have interrupted an Xlib call.  (If
you want to live dangerously: you probably can get away with this
provided they do not share any Xlib data; in particular, they must not
use the same Display connection.)

How you arrange this is up to you.  You can have the signal handler
dump something in global variables for the main-line to notice and act
on, you can use sigblock() to block delivery of the relevant signals
while doing Xlib operations (I assume you have sigblock() because you
have SIGIO), or anything else with the same result.

Since you appear to be running under UNIX, you might consider forking
and splitting responsibilities.  But note that if you do that, the two
processes cannot share a single display connection, not even if you
take care that they don't both try to use it at once; each process that
needs to do X operations must use its own connection.

					der Mouse

			old: mcgill-vision!mouse
			new: mouse@larry.mcrcim.mcgill.edu

janzen@mprgate.mpr.ca (Martin Janzen) (05/04/91)

In article <9105021507.AA18962@lightning.McRCIM.McGill.EDU>,
mouse@lightning.mcrcim.mcgill.EDU (der Mouse) replies to a question which
has already expired from my system, so I don't know the author...

>> = ???
>  = der Mouse

>> [...]
>> The audio part is set up to receive SIGIO interrupts when it is time
>> to read/write from/to the audio device.  The program also receives
>> SIGALRM interrupts that I use to display the peek level of the audio
>> and to show record/play time.  At each SIGALRM interrupt I send 1-2
>> draw requests to the X server.
>
>Danger, danger.
>
>> Both the imaging part and the audio part work if I run them one at a
>> time.  BUT it crash every time when I'm recording/playing and at the
>> same time use the imaging part.
>
>Yup.
>
>["der Mouse" goes on to explain that Xlib won't work in signal handlers]

My suggestion would be to replace the signals and signal handlers entirely
if possible, by using the built-in XtAddInput and XtAddTimeOut functions.

At the least, you should be able to use XtAddTimeOut to set a timer which
calls a procedure to update the display at regular intervals.  You can use
any Xlib calls you want in this procedure, since there's no SIGALRM involved.

Also, you could try using XtAddInput to add a procedure which is called when-
ever reads or writes are possible on the audio device.  (It eventually calls
select(), including your file descriptors in the read- and write-masks.)
If your SIGIO handler just does buffering and doesn't need to make Xlib calls,
this probably isn't necessary.  Also, the response time requirements of the
audio device may force you to use an interrupt handler.  But if you can move all
the drawing code into the XtTimerCallbackProc, your SIGIO handler should work.

-- 
Martin Janzen                     janzen@mprgate.mpr.ca (134.87.131.13)
MPR Teltech Ltd.                  Phone: (604) 293-5309
8999 Nelson Way                   Fax: (604) 293-5787
Burnaby, BC, CANADA  V5A 4B5