Re: Handling asynchronous hardware events



Christian Gollwitzer wrote:
Ralf Fassel wrote:

* Christian Gollwitzer <Christian.Gollwitzer@xxxxxxxxxxxxxxx>
| In C I can install an asynchronous handler, that is called when the
| SRQ happens. However, there are some restrictions. For example I'm
| not allowed to call stdio functions in this handler, so typically
| one needs to send a message with SetEvent() on Windows and checks
| with WaitForEvent() in the main loop to handle the request.

This sounds like the equivalent to signal() on Posix: you're allowed
to call only a small range of functions in the signal handler (listed
in the signal(2) man page).


It's not as bad as signal. I'm allowed to malloc() and free(), even to sprintf(), but printf and file I/O is forbidden. The exact requirements are listed on:

http://zone.ni.com/devzone/cda/tut/p/id/3458#2


| I'd like to use Tcl and events instead, e.g. fire an event and
| provide something similiar to fileevent to handle it, so I can get
| rid of all the synchronization hassle in C.

I would bet that you can't use TCL in the signal handler since TCL
could/will invoke 'unsafe' functions like allocating memory or
something like that.


Of course I can't call Tcl_Eval() in the handler. However, the thread safe Tcl provides functions to do that in some way. Concretely, there is

Tcl_AlertNotifier(), Tcl_ThreadAlert(), Tcl_QueueEvent(), Tcl_SetMaxBlockTime(), Tcl_AsyncCreate(), Tcl_AsyncMark(), Tcl_Asyncinvoke(), Tcl_CreateEventSource(), ...

I was able to beat together something for a different project by ripping off code from the Threads package. But it was very complicated (a C function has been called in the context of the second thread, which called Tcl_Eval("event generate ...")), and I bet there must be some straightforward way to insert an event asynchronously from the handler.

Sadly, the man pages have lots of very detailed description, but there is no simple understandable example, how to use these functions.


However, what you could try is to create a TCL channel/pipe, register
a fileevent for it, and in the signal handler write(2) something to
it. The fileevent proc then should take over as soon as the event
queue runs again.


I/O is forbidden.

Or just run some heartbeat proc every N milliseconds which checks a
global status variable which is set in the signal handler.


Well, then I can use gpib-tcl right away... continuous polling aka busy waiting is exactly what I'm trying to avoid.

Christian
what about using a pipe with your signal handler writing tclish strings
of the incoming events to the pipe and the tcl side using fileevent on this pipe?

had a look at menchan and similar stuff? http://wiki.tcl.tk/2083

uwe


.



Relevant Pages

  • Re: signals and wait for event.
    ... what countermeasures should be taken to safely add data to queue ... Can i block inside a signal handler, ... The malloc() function has partially updated its data ... I meant blocking signals inside a signal handler just after it executes ...
    (comp.unix.programmer)
  • Re: Handling asynchronous hardware events
    ... | In C I can install an asynchronous handler, ... I would bet that you can't use TCL in the signal handler since TCL ... a fileevent for it, and in the signal handler writesomething to ...
    (comp.lang.tcl)
  • Re: signals and wait for event.
    ... >> called from within an asynchronous interrupt handler. ... And malloc() isn't ... It's not clear what you mean by "block inside a signal handler". ...
    (comp.unix.programmer)
  • Re: Repost: Cant sys.exit() from SIGTERM handler?
    ... when you register a Python function via signal.signal, ... the Python signal handler is ... Entering exception handler ...
    (comp.lang.python)
  • Re: Handling asynchronous hardware events
    ... | In C I can install an asynchronous handler, ... I would bet that you can't use TCL in the signal handler since TCL ...
    (comp.lang.tcl)