📄 async.3
字号:
'\"'\" Copyright (c) 1989-1993 The Regents of the University of California.'\" Copyright (c) 1994-1996 Sun Microsystems, Inc.'\"'\" See the file "license.terms" for information on usage and redistribution'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.'\" '\" SCCS: @(#) Async.3 1.14 96/08/26 12:59:41'\" .so man.macros.TH Tcl_AsyncCreate 3 7.0 Tcl "Tcl Library Procedures".BS.SH NAMETcl_AsyncCreate, Tcl_AsyncMark, Tcl_AsyncInvoke, Tcl_AsyncDelete \- handle asynchronous events.SH SYNOPSIS.nf\fB#include <tcl.h>\fR.spTcl_AsyncHandler\fBTcl_AsyncCreate\fR(\fIproc, clientData\fR).sp\fBTcl_AsyncMark\fR(\fIasync\fR).spint\fBTcl_AsyncInvoke\fR(\fIinterp, code\fR).sp\fBTcl_AsyncDelete\fR(\fIasync\fR).spint\fBTcl_AsyncReady\fR().SH ARGUMENTS.AS Tcl_AsyncHandler clientData.AP Tcl_AsyncProc *proc inProcedure to invoke to handle an asynchronous event..AP ClientData clientData inOne-word value to pass to \fIproc\fR..AP Tcl_AsyncHandler async inToken for asynchronous event handler..AP Tcl_Interp *interp inTcl interpreter in which command was being evaluated when handler wasinvoked, or NULL if handler was invoked when there was no interpreteractive..AP int code inCompletion code from command that just completed in \fIinterp\fR,or 0 if \fIinterp\fR is NULL..BE.SH DESCRIPTION.PPThese 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..PP\fBTcl_AsyncCreate\fR 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 \fBTcl_AsyncMark\fR with thetoken for the handler.\fBTcl_AsyncMark\fR will mark the handler as ready to execute, but itwill not invoke the handler immediately.Tcl will call the \fIproc\fR associated with the handler later, whenthe world is in a safe state, and \fIproc\fR can then carry outthe actions associated with the asynchronous event.\fIProc\fR should have arguments and result that match thetype \fBTcl_AsyncProc\fR:.CStypedef int Tcl_AsyncProc( ClientData \fIclientData\fR, Tcl_Interp *\fIinterp\fR, int \fIcode\fR);.CEThe \fIclientData\fR will be the same as the \fIclientData\fRargument passed to \fBTcl_AsyncCreate\fR when the handler wascreated.If \fIproc\fR is invoked just after a command has completedexecution in an interpreter, then \fIinterp\fR will identifythe interpreter in which the command was evaluated and\fIcode\fR will be the completion code returned by thatcommand.The command's result will be present in \fIinterp->result\fR.When \fIproc\fR returns, whatever it leaves in \fIinterp->result\fRwill be returned as the result of the command and the integervalue returned by \fIproc\fR will be used as the new completioncode for the command..PPIt is also possible for \fIproc\fR 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 \fIinterp\fR will be NULL and \fIcode\fR will be0, and the return value from \fIproc\fR will be ignored..PPThe procedure \fBTcl_AsyncInvoke\fR is called to invoke all of thehandlers that are ready.The procedure \fBTcl_AsyncReady\fR will return non-zero whenever anyasynchronous handlers are ready; it can be checked to avoid callsto \fBTcl_AsyncInvoke\fR when there are no ready handlers.Tcl calls \fBTcl_AsyncReady\fR after each command is evaluatedand calls \fBTcl_AsyncInvoke\fR if needed.Applications may also call \fBTcl_AsyncInvoke\fR at interestingtimes for that application.For example, Tcl's event handler calls \fBTcl_AsyncReady\fRafter each event and calls \fBTcl_AsyncInvoke\fR if needed.The \fIinterp\fR and \fIcode\fR arguments to \fBTcl_AsyncInvoke\fRhave the same meaning as for \fIproc\fR: they identify the activeinterpreter, if any, and the completion code from the commandthat just completed..PP\fBTcl_AsyncDelete\fR removes an asynchronous handler so thatits \fIproc\fR will never be invoked again.A handler can be deleted even when ready, and it will stillnot be invoked..PPIf multiple handlers become active at the same time, thehandlers are invoked in the order they were created (oldesthandler first).The \fIcode\fR and \fIinterp->result\fR 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,\fBTcl_AsyncInvoke\fR 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..SH WARNING.PPIt is almost always a bad idea for an asynchronous eventhandler to modify \fIinterp->result\fR or return a code differentfrom its \fIcode\fR 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 \fIinterp->result\fR plus the valuesof the variables \fBerrorInfo\fR and \fBerrorCode\fR (this canbe done, for example, by storing them in dynamic strings).When the asynchronous handler is finished it should restore\fIinterp->result\fR, \fBerrorInfo\fR, and \fBerrorCode\fR,and return the \fIcode\fR argument..SH KEYWORDSasynchronous event, handler, signal
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -