Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/doc/Async.3 @ 33

Last change on this file since 33 was 25, checked in by landauf, 16 years ago

added tcl to libs

File size: 6.6 KB
Line 
1'\"
2'\" Copyright (c) 1989-1993 The Regents of the University of California.
3'\" Copyright (c) 1994-1996 Sun Microsystems, Inc.
4'\"
5'\" See the file "license.terms" for information on usage and redistribution
6'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
7'\"
8'\" RCS: @(#) $Id: Async.3,v 1.12 2007/12/13 15:22:30 dgp Exp $
9'\"
10.so man.macros
11.TH Tcl_AsyncCreate 3 7.0 Tcl "Tcl Library Procedures"
12.BS
13.SH NAME
14Tcl_AsyncCreate, Tcl_AsyncMark, Tcl_AsyncInvoke, Tcl_AsyncDelete, Tcl_AsyncReady \- handle asynchronous events
15.SH SYNOPSIS
16.nf
17\fB#include <tcl.h>\fR
18.sp
19Tcl_AsyncHandler
20\fBTcl_AsyncCreate\fR(\fIproc, clientData\fR)
21.sp
22\fBTcl_AsyncMark\fR(\fIasync\fR)
23.sp
24int
25\fBTcl_AsyncInvoke\fR(\fIinterp, code\fR)
26.sp
27\fBTcl_AsyncDelete\fR(\fIasync\fR)
28.sp
29int
30\fBTcl_AsyncReady\fR()
31.SH ARGUMENTS
32.AS Tcl_AsyncHandler clientData
33.AP Tcl_AsyncProc *proc in
34Procedure to invoke to handle an asynchronous event.
35.AP ClientData clientData in
36One-word value to pass to \fIproc\fR.
37.AP Tcl_AsyncHandler async in
38Token for asynchronous event handler.
39.AP Tcl_Interp *interp in
40Tcl interpreter in which command was being evaluated when handler was
41invoked, or NULL if handler was invoked when there was no interpreter
42active.
43.AP int code in
44Completion code from command that just completed in \fIinterp\fR,
45or 0 if \fIinterp\fR is NULL.
46.BE
47
48.SH DESCRIPTION
49.PP
50These procedures provide a safe mechanism for dealing with
51asynchronous events such as signals.
52If an event such as a signal occurs while a Tcl script is being
53evaluated then it is not safe to take any substantive action to
54process the event.
55For example, it is not safe to evaluate a Tcl script since the
56interpreter may already be in the middle of evaluating a script;
57it may not even be safe to allocate memory, since a memory
58allocation could have been in progress when the event occurred.
59The only safe approach is to set a flag indicating that the event
60occurred, then handle the event later when the world has returned
61to a clean state, such as after the current Tcl command completes.
62.PP
63\fBTcl_AsyncCreate\fR, \fBTcl_AsyncDelete\fR, and \fBTcl_AsyncReady\fR
64are thread sensitive.  They access and/or set a thread-specific data
65structure in the event of a core built with \fI\-\-enable\-threads\fR.  The token
66created by \fBTcl_AsyncCreate\fR contains the needed thread information it
67was called from so that calling \fBTcl_AsyncMark\fR(\fItoken\fR) will only yield
68the origin thread into the asynchronous handler.
69.PP
70\fBTcl_AsyncCreate\fR creates an asynchronous handler and returns
71a token for it.
72The asynchronous handler must be created before
73any occurrences of the asynchronous event that it is intended
74to handle (it is not safe to create a handler at the time of
75an event).
76When an asynchronous event occurs the code that detects the event
77(such as a signal handler) should call \fBTcl_AsyncMark\fR with the
78token for the handler.
79\fBTcl_AsyncMark\fR will mark the handler as ready to execute, but it
80will not invoke the handler immediately.
81Tcl will call the \fIproc\fR associated with the handler later, when
82the world is in a safe state, and \fIproc\fR can then carry out
83the actions associated with the asynchronous event.
84\fIProc\fR should have arguments and result that match the
85type \fBTcl_AsyncProc\fR:
86.CS
87typedef int Tcl_AsyncProc(
88        ClientData \fIclientData\fR,
89        Tcl_Interp *\fIinterp\fR,
90        int \fIcode\fR);
91.CE
92The \fIclientData\fR will be the same as the \fIclientData\fR
93argument passed to \fBTcl_AsyncCreate\fR when the handler was
94created.
95If \fIproc\fR is invoked just after a command has completed
96execution in an interpreter, then \fIinterp\fR will identify
97the interpreter in which the command was evaluated and
98\fIcode\fR will be the completion code returned by that
99command.
100The command's result will be present in the interpreter's result.
101When \fIproc\fR returns, whatever it leaves in the interpreter's result
102will be returned as the result of the command and the integer
103value returned by \fIproc\fR will be used as the new completion
104code for the command.
105.PP
106It is also possible for \fIproc\fR to be invoked when no interpreter
107is active.
108This can happen, for example, if an asynchronous event occurs while
109the application is waiting for interactive input or an X event.
110In this case \fIinterp\fR will be NULL and \fIcode\fR will be
1110, and the return value from \fIproc\fR will be ignored.
112.PP
113The procedure \fBTcl_AsyncInvoke\fR is called to invoke all of the
114handlers that are ready.
115The procedure \fBTcl_AsyncReady\fR will return non-zero whenever any
116asynchronous handlers are ready;  it can be checked to avoid calls
117to \fBTcl_AsyncInvoke\fR when there are no ready handlers.
118Tcl calls \fBTcl_AsyncReady\fR after each command is evaluated
119and calls \fBTcl_AsyncInvoke\fR if needed.
120Applications may also call \fBTcl_AsyncInvoke\fR at interesting
121times for that application.
122For example, Tcl's event handler calls \fBTcl_AsyncReady\fR
123after each event and calls \fBTcl_AsyncInvoke\fR if needed.
124The \fIinterp\fR and \fIcode\fR arguments to \fBTcl_AsyncInvoke\fR
125have the same meaning as for \fIproc\fR:  they identify the active
126interpreter, if any, and the completion code from the command
127that just completed.
128.PP
129\fBTcl_AsyncDelete\fR removes an asynchronous handler so that
130its \fIproc\fR will never be invoked again.
131A handler can be deleted even when ready, and it will still
132not be invoked.
133.PP
134If multiple handlers become active at the same time, the
135handlers are invoked in the order they were created (oldest
136handler first).
137The \fIcode\fR and the interpreter's result for later handlers
138reflect the values returned by earlier handlers, so that
139the most recently created handler has last say about
140the interpreter's result and completion code.
141If new handlers become ready while handlers are executing,
142\fBTcl_AsyncInvoke\fR will invoke them all;  at each point it
143invokes the highest-priority (oldest) ready handler, repeating
144this over and over until there are no longer any ready handlers.
145.SH WARNING
146.PP
147It is almost always a bad idea for an asynchronous event
148handler to modify the interpreter's result or return a code different
149from its \fIcode\fR argument.
150This sort of behavior can disrupt the execution of scripts in
151subtle ways and result in bugs that are extremely difficult
152to track down.
153If an asynchronous event handler needs to evaluate Tcl scripts
154then it should first save the interpreter's state by calling
155\fBTcl_SaveInterpState\fR, passing in the \fIcode\fR argument.
156When the asynchronous handler is finished it should restore
157the interpreter's state by calling \fBTcl_RestoreInterpState\fR,
158and then returning the \fIcode\fR argument.
159
160.SH KEYWORDS
161asynchronous event, handler, signal, Tcl_SaveInterpState, thread
Note: See TracBrowser for help on using the repository browser.