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