MAN page from RedHat Other itcl-tcl-2.2p2-1.i386.rpm
Tcl_AsyncCreate
Section: Tcl Library Procedures (3)
Updated: 7.0
Index
NAME
Tcl_AsyncCreate, Tcl_AsyncMark, Tcl_AsyncInvoke, Tcl_AsyncDelete - handle asynchronous events
SYNOPSIS
#include <tcl.h>Tcl_AsyncHandlerTcl_AsyncCreate(proc, clientData)Tcl_AsyncMark(async)intTcl_AsyncInvoke(interp, code)Tcl_AsyncDelete(async)intTcl_AsyncReady()
ARGUMENTS
- Tcl_AsyncProc *proc (in)
Procedure to invoke to handle an asynchronous event. - ClientData clientData (in)
One-word value to pass to proc. - Tcl_AsyncHandler async (in)
Token for asynchronous event handler. - Tcl_Interp *interp (in)
Tcl interpreter in which command was being evaluated when handler wasinvoked, or NULL if handler was invoked when there was no interpreteractive. - int code (in)
Completion code from command that just completed in interp,or 0 if interp is NULL.
DESCRIPTION
These procedures provide a safe mechanism for dealing withasynchronous events such as signals.If an event such as a signal occurs while a Tcl script is beingevaluated then it isn't safe to take any substantive action toprocess the event.For example, it isn't safe to evaluate a Tcl script since theinterpreter may already be in the middle of evaluating a script;it may not even be safe to allocate memory, since a memoryallocation could have been in progress when the event occurred.The only safe approach is to set a flag indicating that the eventoccurred, then handle the event later when the world has returnedto a clean state, such as after the current Tcl command completes.
Tcl_AsyncCreate creates an asynchronous handler and returnsa token for it.The asynchronous handler must be created beforeany occurrences of the asynchronous event that it is intendedto handle (it is not safe to create a handler at the time ofan event).When an asynchronous event occurs the code that detects the event(such as a signal handler) should call Tcl_AsyncMark with thetoken for the handler.Tcl_AsyncMark will mark the handler as ready to execute, but itwill not invoke the handler immediately.Tcl will call the proc associated with the handler later, whenthe world is in a safe state, and proc can then carry outthe actions associated with the asynchronous event.Proc should have arguments and result that match thetype Tcl_AsyncProc:
typedef int Tcl_AsyncProc( ClientData clientData, Tcl_Interp *interp, int code);
The
clientData will be the same as the
clientDataargument passed to
Tcl_AsyncCreate when the handler wascreated.If
proc is invoked just after a command has completedexecution in an interpreter, then
interp will identifythe interpreter in which the command was evaluated and
code will be the completion code returned by thatcommand.The command's result will be present in
interp->result.When
proc returns, whatever it leaves in
interp->resultwill be returned as the result of the command and the integervalue returned by
proc will be used as the new completioncode for the command.
It is also possible for proc to be invoked when no interpreteris active.This can happen, for example, if an asynchronous event occurs whilethe application is waiting for interactive input or an X event.In this case interp will be NULL and code will be0, and the return value from proc will be ignored.
The procedure Tcl_AsyncInvoke is called to invoke all of thehandlers that are ready.The procedure Tcl_AsyncReady will return non-zero whenever anyasynchronous handlers are ready; it can be checked to avoid callsto Tcl_AsyncInvoke when there are no ready handlers.Tcl calls Tcl_AsyncReady after each command is evaluatedand calls Tcl_AsyncInvoke if needed.Applications may also call Tcl_AsyncInvoke at interestingtimes for that application.For example, Tcl's event handler calls Tcl_AsyncReadyafter each event and calls Tcl_AsyncInvoke if needed.The interp and code arguments to Tcl_AsyncInvokehave the same meaning as for proc: they identify the activeinterpreter, if any, and the completion code from the commandthat just completed.
Tcl_AsyncDelete removes an asynchronous handler so thatits proc will never be invoked again.A handler can be deleted even when ready, and it will stillnot be invoked.
If multiple handlers become active at the same time, thehandlers are invoked in the order they were created (oldesthandler first).The code and interp->result for later handlersreflect the values returned by earlier handlers, so thatthe most recently created handler has last say aboutthe interpreter's result and completion code.If new handlers become ready while handlers are executing,Tcl_AsyncInvoke will invoke them all; at each point itinvokes the highest-priority (oldest) ready handler, repeatingthis over and over until there are no longer any ready handlers.
WARNING
It is almost always a bad idea for an asynchronous eventhandler to modify interp->result or return a code differentfrom its code argument.This sort of behavior can disrupt the execution of scripts insubtle ways and result in bugs that are extremely difficultto track down.If an asynchronous event handler needs to evaluate Tcl scriptsthen it should first save interp->result plus the valuesof the variables errorInfo and errorCode (this canbe done, for example, by storing them in dynamic strings).When the asynchronous handler is finished it should restoreinterp->result, errorInfo, and errorCode,and return the code argument.
KEYWORDS
asynchronous event, handler, signal
Index
- NAME
- SYNOPSIS
- ARGUMENTS
- DESCRIPTION
- WARNING
- KEYWORDS
This document was created byman2html,using the manual pages.