Tcl_AsyncCreate, Tcl_AsyncMark, Tcl_AsyncInvoke, Tcl_AsyncDelete, Tcl_AsyncReady − handle asynchronous events


#include <tcl.h>

(proc, clientData)


(interp, code)




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 was invoked, or NULL if handler was invoked when there was no interpreter active.

int code (in)

Completion code from command that just completed in interp, or 0 if interp is NULL.



These procedures provide a safe mechanism for dealing with asynchronous events such as signals. If an event such as a signal occurs while a Tcl script is being evaluated then it is not safe to take any substantive action to process the event. For example, it is not safe to evaluate a Tcl script since the interpreter may already be in the middle of evaluating a script; it may not even be safe to allocate memory, since a memory allocation could have been in progress when the event occurred. The only safe approach is to set a flag indicating that the event occurred, then handle the event later when the world has returned to a clean state, such as after the current Tcl command completes.

Tcl_AsyncCreate, Tcl_AsyncDelete, and Tcl_AsyncReady are thread sensitive. They access and/or set a thread-specific data structure in the event of a core built with −−enable−threads. The token created by Tcl_AsyncCreate contains the needed thread information it was called from so that calling Tcl_AsyncMark(token) will only yield the origin thread into the asynchronous handler.

Tcl_AsyncCreate creates an asynchronous handler and returns a token for it. The asynchronous handler must be created before any occurrences of the asynchronous event that it is intended to handle (it is not safe to create a handler at the time of an event). When an asynchronous event occurs the code that detects the event (such as a signal handler) should call Tcl_AsyncMark with the token for the handler. Tcl_AsyncMark will mark the handler as ready to execute, but it will not invoke the handler immediately. Tcl will call the proc associated with the handler later, when the world is in a safe state, and proc can then carry out the actions associated with the asynchronous event. Proc should have arguments and result that match the type Tcl_AsyncProc:

typedef int Tcl_AsyncProc(
ClientData clientData,
Tcl_Interp *interp,
int code);

The clientData will be the same as the clientData argument passed to Tcl_AsyncCreate when the handler was created. If proc is invoked just after a command has completed execution in an interpreter, then interp will identify the interpreter in which the command was evaluated and code will be the completion code returned by that command. The command’s result will be present in the interpreter’s result. When proc returns, whatever it leaves in the interpreter’s result will be returned as the result of the command and the integer value returned by proc will be used as the new completion code for the command.

It is also possible for proc to be invoked when no interpreter is active. This can happen, for example, if an asynchronous event occurs while the application is waiting for interactive input or an X event. In this case interp will be NULL and code will be 0, and the return value from proc will be ignored.

The procedure Tcl_AsyncInvoke is called to invoke all of the handlers that are ready. The procedure Tcl_AsyncReady will return non-zero whenever any asynchronous handlers are ready; it can be checked to avoid calls to Tcl_AsyncInvoke when there are no ready handlers. Tcl calls Tcl_AsyncReady after each command is evaluated and calls Tcl_AsyncInvoke if needed. Applications may also call Tcl_AsyncInvoke at interesting times for that application. For example, Tcl’s event handler calls Tcl_AsyncReady after each event and calls Tcl_AsyncInvoke if needed. The interp and code arguments to Tcl_AsyncInvoke have the same meaning as for proc: they identify the active interpreter, if any, and the completion code from the command that just completed.

Tcl_AsyncDelete removes an asynchronous handler so that its proc will never be invoked again. A handler can be deleted even when ready, and it will still not be invoked.

If multiple handlers become active at the same time, the handlers are invoked in the order they were created (oldest handler first). The code and the interpreter’s result for later handlers reflect the values returned by earlier handlers, so that the most recently created handler has last say about the interpreter’s result and completion code. If new handlers become ready while handlers are executing, Tcl_AsyncInvoke will invoke them all; at each point it invokes the highest-priority (oldest) ready handler, repeating this over and over until there are no longer any ready handlers.


It is almost always a bad idea for an asynchronous event handler to modify the interpreter’s result or return a code different from its code argument. This sort of behavior can disrupt the execution of scripts in subtle ways and result in bugs that are extremely difficult to track down. If an asynchronous event handler needs to evaluate Tcl scripts then it should first save the interpreter’s state by calling Tcl_SaveInterpState, passing in the code argument. When the asynchronous handler is finished it should restore the interpreter’s state by calling Tcl_RestoreInterpState, and then returning the code argument.


asynchronous event, handler, signal, Tcl_SaveInterpState, thread

More Linux Commands

gvmap(1) - find clusters and create a geographical map highl
gvmap takes as input a graph in DOT format, finds node clusters and produces a rendering of the graph as a geographic-style map, with clusters highlighted as co

gnutls_x509_crt_get_extension_info(3) - API function........
gnutls_x509_crt_get_extension_info.3 - This function will return the requested extension OID in the certificate, and the critical flag for it. The extension OID

sem_wait(3) - lock a semaphore (Library - Linux man page)...
sem_wait() decrements (locks) the semaphore pointed to by sem. If the semaphores value is greater than zero, then the decrement proceeds, and the function retur

codepage(1) - extract a codepage from an MSDOS codepage file
The codepage command extracts codepages from an MSDOS codepage file. OPTIONS -c Input file is a single codepage -L Print header info (you dont want to see this)

SDL_EventState(3) - This function allows you to set the stat
This function allows you to set the state of processing certain event types. If state is set to SDL_IGNORE, that event type will be automatically dropped from t

dbus-monitor(1) - debug probe to print message bus messages
The dbus-monitor command is used to monitor messages going through a D-Bus message bus. See for more information about

XcmsQueryWhite(3) - obtain black, blue, green, red, and whit
The XcmsQueryBlack function returns the color specification in the specified target format for zero-intensity red, green, and blue. The XcmsQueryBlue function r

keyctl(2) - Manipulate the kernel's key management facility
keyctl() has a number of functions available: KEYCTL_GET_KEYRING_ID Ask for a keyrings ID. KEYCTL_JOIN_SESSION_KEYRING Join or start named session keyring. KEYC

memcpy(3) - copy memory area (Library - Linux man page).....
The memcpy() function copies n bytes from memory area src to memory area dest. The memory areas must not overlap. Use memmove(3) if the memory areas do overlap.

glDrawArrays(3gl) - render primitives from array data.......
glDrawArrays specifies multiple geometric primitives with very few subroutine calls. Instead of calling a GL procedure to pass each individual vertex, normal, t

glMultiTexCoord4iARB(3gl) - set the current texture coordina
glMultiTexCoordARB specifies texture coordinates in one, two, three, or four dimensions. glMultiTexCoord1ARB sets the current texture coordinates to (s, 0, 0, 1

gluDeleteNurbsRenderer(3gl) - destroy a NURBS object........
gluDeleteNurbsRenderer destroys the NURBS object (which was created with gluNewNurbsRenderer) and frees any memory it uses. Once gluDeleteNurbsRenderer has been

We can't live, work or learn in freedom unless the software we use is free.