Tcl_AsyncCreate(3)
π Tcl_AsyncCreate(3) π
okay so bestie, we're in the Tcl Library Procedures corner of the world and today's main character is a whole family of functions that let you deal with asynchronous events (think: signals π) without your interpreter absolutely losing it. grab a boba, this one's important.
β¨ NAME β¨
Tcl_AsyncCreate, Tcl_AsyncMark, Tcl_AsyncMarkFromSignal, Tcl_AsyncInvoke, Tcl_AsyncDelete, Tcl_AsyncReady β handle asynchronous events π¦
β¨ SYNOPSIS β¨
#include <tcl.h>
Tcl_AsyncHandler
Tcl_AsyncCreate(proc, clientData)
void
Tcl_AsyncMark(async)
int
Tcl_AsyncMarkFromSignal(async, sigNumber)
int
Tcl_AsyncInvoke(interp, code)
void
Tcl_AsyncDelete(async)
int
Tcl_AsyncReady()
π ARGUMENTS π
Tcl_AsyncProc *proc(in) β the procedure you want to invoke to handle an asynchronous event. your little event-handling princess πvoid *clientData(in) β a one-word value that gets passed straight toproc. one word. that's it. that's the whole vibe.Tcl_AsyncHandler async(in) β the token for your asynchronous event handler. this is your handler's lil ID badge πint sigNumber(in) β POSIX signal number, for when you're using this in a signal context.Tcl_Interp *interp(in) β the Tcl interpreter in which the command was being evaluated when the handler got invoked, orNULLif the handler was invoked when there was no interpreter active. (empty room, no interp, we move.)int code(in) β the completion code from the command that just completed in interp, or0if interp isNULL.
πΈ DESCRIPTION πΈ
These procedures give you a safe mechanism for dealing with asynchronous events like signals. the tea here is: if an event (say, a signal) shows up while a Tcl script is being evaluated, it is not safe to take any substantive action to process it. not even a little. for example, it's not safe to evaluate a Tcl script, because the interpreter may already be in the middle of evaluating a script β she's busy. it may not even be safe to allocate memory, because a memory allocation could have been in progress when the event occurred. π
so the only safe approach is to set a flag saying "hey, the event happened" and then handle the event later, when the world has returned to a clean state β like, after the current Tcl command completes. periodt.
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_AsyncMarkFromSignal or Tcl_AsyncMark with this token will only yield the origin thread into the asynchronous handler. no cross-thread chaos, we love boundaries. β¨
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 β and no, it is not safe to create a handler at the time of an event. girl, plan ahead. π
When an asynchronous event occurs, the code that detects the event (such as a POSIX signal handler) should call Tcl_AsyncMarkFromSignal with the token for the handler and the POSIX signal number. the return value of this function is true when the handler will be marked, and false otherwise. for non-signal contexts, Tcl_AsyncMark serves the same purpose. Tcl_AsyncMarkFromSignal and Tcl_AsyncMark will mark the handler as ready to execute, but will not invoke the handler immediately β no jumping the queue. 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(
void *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. iconic behavior, honestly. π
It's 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. (she said "no thoughts, no interp, no consequences" π«Ά)
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. bye bestie, blocked. π
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. oldest first, always β grandma gets to speak first at dinner. π
π₯ WARNING π₯
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. not "kinda annoying" bugs. extremely difficult. π
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. save, do the thing, restore, return. that's the ritual. π―οΈβ¨
π¦ KEYWORDS π¦
asynchronous event, handler, signal, Tcl_SaveInterpState, thread
and that's the whole page, bestie!! this one shipped from the Tcl library, version 7.0 β so like, vintage but make it timeless π β¨ go forth and handle your async events safely, and remember: set the flag, handle it later, don't touch the result. mwah π«Άπ
π¦ where this page comes from
- alpine 3.23: tcl9-doc 9.0.3-r0 Β· TCL
- alpine 3.24: tcl9-doc 9.0.3-r2 Β· TCL
- fedora 43: tcl-doc 1:9.0.2-1.fc43 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- fedora 44: tcl-doc 1:9.0.2-1.fc44 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- nixos 26.05: tcl 9.0.3
- nixpkgs unstable: tcl 9.1.0
- Copyright (c) 1989-1993 The Regents of the University of California
- Copyright (c) 1994-1996 Sun Microsystems, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms