aboutsummaryrefslogtreecommitdiffstats
path: root/doc
diff options
context:
space:
mode:
authorTom Tromey2012-08-23 13:58:38 -0600
committerTom Tromey2012-08-23 13:58:38 -0600
commit0ec3764d398add8c038b317201d139aaef4a594e (patch)
treebbc0df921785cb065568657f9a6594ad5b10447d /doc
parentc6bb874290bb0d56d2caa106fc3989cf34a72c3e (diff)
downloademacs-0ec3764d398add8c038b317201d139aaef4a594e.tar.gz
emacs-0ec3764d398add8c038b317201d139aaef4a594e.zip
first draft of threads documentation
Diffstat (limited to 'doc')
-rw-r--r--doc/lispref/Makefile.in1
-rw-r--r--doc/lispref/elisp.texi8
-rw-r--r--doc/lispref/objects.texi20
-rw-r--r--doc/lispref/threads.texi238
4 files changed, 262 insertions, 5 deletions
diff --git a/doc/lispref/Makefile.in b/doc/lispref/Makefile.in
index 32a241e2a2d..626efc5b0e2 100644
--- a/doc/lispref/Makefile.in
+++ b/doc/lispref/Makefile.in
@@ -94,6 +94,7 @@ srcs = \
94 $(srcdir)/symbols.texi \ 94 $(srcdir)/symbols.texi \
95 $(srcdir)/syntax.texi \ 95 $(srcdir)/syntax.texi \
96 $(srcdir)/text.texi \ 96 $(srcdir)/text.texi \
97 $(srcdir)/threads.texi \
97 $(srcdir)/tips.texi \ 98 $(srcdir)/tips.texi \
98 $(srcdir)/variables.texi \ 99 $(srcdir)/variables.texi \
99 $(srcdir)/windows.texi \ 100 $(srcdir)/windows.texi \
diff --git a/doc/lispref/elisp.texi b/doc/lispref/elisp.texi
index caa5185dec3..61828ef2d57 100644
--- a/doc/lispref/elisp.texi
+++ b/doc/lispref/elisp.texi
@@ -210,6 +210,7 @@ Cover art by Etienne Suvasa.
210* Syntax Tables:: The syntax table controls word and list parsing. 210* Syntax Tables:: The syntax table controls word and list parsing.
211* Abbrevs:: How Abbrev mode works, and its data structures. 211* Abbrevs:: How Abbrev mode works, and its data structures.
212 212
213* Threads:: Concurrency in Emacs Lisp.
213* Processes:: Running and communicating with subprocesses. 214* Processes:: Running and communicating with subprocesses.
214* Display:: Features for controlling the screen display. 215* Display:: Features for controlling the screen display.
215* System Interface:: Getting the user id, system type, environment 216* System Interface:: Getting the user id, system type, environment
@@ -1269,6 +1270,12 @@ Abbrevs and Abbrev Expansion
1269* Abbrev Table Properties:: How to read and set abbrev table properties. 1270* Abbrev Table Properties:: How to read and set abbrev table properties.
1270 Which properties have which effect. 1271 Which properties have which effect.
1271 1272
1273Threads
1274
1275* Basic Thread Functions:: Basic thread functions.
1276* Mutexes:: Mutexes allow exclusive access to data.
1277* Condition Variables:: Inter-thread events.
1278
1272Processes 1279Processes
1273 1280
1274* Subprocess Creation:: Functions that start subprocesses. 1281* Subprocess Creation:: Functions that start subprocesses.
@@ -1571,6 +1578,7 @@ Object Internals
1571@include searching.texi 1578@include searching.texi
1572@include syntax.texi 1579@include syntax.texi
1573@include abbrevs.texi 1580@include abbrevs.texi
1581@include threads.texi
1574@include processes.texi 1582@include processes.texi
1575 1583
1576@include display.texi 1584@include display.texi
diff --git a/doc/lispref/objects.texi b/doc/lispref/objects.texi
index 7d40f0ff934..9033681ed5a 100644
--- a/doc/lispref/objects.texi
+++ b/doc/lispref/objects.texi
@@ -1885,6 +1885,15 @@ with references to further information.
1885 1885
1886@item string-or-null-p 1886@item string-or-null-p
1887@xref{Predicates for Strings, string-or-null-p}. 1887@xref{Predicates for Strings, string-or-null-p}.
1888
1889@item threadp
1890@xref{Basic Thread Functions, threadp}.
1891
1892@item mutexp
1893@xref{Mutexes, mutexp}.
1894
1895@item condition-variable-p
1896@xref{Condition Variables, condition-variable-p}.
1888@end table 1897@end table
1889 1898
1890 The most general way to check the type of an object is to call the 1899 The most general way to check the type of an object is to call the
@@ -1898,11 +1907,12 @@ types. In most cases, it is more convenient to use type predicates than
1898This function returns a symbol naming the primitive type of 1907This function returns a symbol naming the primitive type of
1899@var{object}. The value is one of the symbols @code{bool-vector}, 1908@var{object}. The value is one of the symbols @code{bool-vector},
1900@code{buffer}, @code{char-table}, @code{compiled-function}, 1909@code{buffer}, @code{char-table}, @code{compiled-function},
1901@code{cons}, @code{float}, @code{font-entity}, @code{font-object}, 1910@code{condition-variable}, @code{cons}, @code{float},
1902@code{font-spec}, @code{frame}, @code{hash-table}, @code{integer}, 1911@code{font-entity}, @code{font-object}, @code{font-spec},
1903@code{marker}, @code{overlay}, @code{process}, @code{string}, 1912@code{frame}, @code{hash-table}, @code{integer}, @code{marker},
1904@code{subr}, @code{symbol}, @code{vector}, @code{window}, or 1913@code{mutex}, @code{overlay}, @code{process}, @code{string},
1905@code{window-configuration}. 1914@code{subr}, @code{symbol}, @code{thread}, @code{vector},
1915@code{window}, or @code{window-configuration}.
1906 1916
1907@example 1917@example
1908(type-of 1) 1918(type-of 1)
diff --git a/doc/lispref/threads.texi b/doc/lispref/threads.texi
new file mode 100644
index 00000000000..760452808cf
--- /dev/null
+++ b/doc/lispref/threads.texi
@@ -0,0 +1,238 @@
1@c -*-texinfo-*-
2@c This is part of the GNU Emacs Lisp Reference Manual.
3@c Copyright (C) 2012
4@c Free Software Foundation, Inc.
5@c See the file elisp.texi for copying conditions.
6@node Threads
7@chapter Threads
8@cindex threads
9@cindex concurrency
10
11 Emacs Lisp provides a limited form of concurrency, called
12@dfn{threads}. All the threads in a given instance of Emacs share the
13same memory. Concurrency in Emacs Lisp is ``mostly cooperative'',
14meaning that Emacs will only switch execution between threads at
15well-defined times. However, the Emacs thread support has been
16designed in a way to later allow more fine-grained concurrency, and
17correct programs should not rely on cooperative threading.
18
19 Currently, thread switching will occur upon explicit request via
20@code{thread-yield}, when waiting for keyboard input or for process
21output (e.g., during @code{accept-process-output}), or during blocking
22operations relating to threads, such as mutex locking or
23@code{thread-join}.
24
25 Emacs Lisp provides primitives to create and control threads, and
26also to create and control mutexes and condition variables, useful for
27thread synchronization.
28
29 While global variables are shared among all Emacs Lisp threads,
30local variables are not---a dynamic @code{let} binding is local.
31
32 In the case of lexical bindings (@pxref{Variable Scoping}), a
33closure is an object like any other in Emacs Lisp, and bindings in a
34closure are shared by any threads invoking the closure.
35
36@menu
37* Basic Thread Functions:: Basic thread functions.
38* Mutexes:: Mutexes allow exclusive access to data.
39* Condition Variables:: Inter-thread events.
40@end menu
41
42@node Basic Thread Functions
43@section Basic Thread Functions
44
45 Threads can be created and waited for. A thread cannot be exited
46directly, but the current thread can be exited implicitly, and other
47threads can be signaled.
48
49@defun make-thread function &optional name
50Create a new thread of execution which invokes @var{function}. When
51@var{function} returns, the thread exits.
52
53@var{name} can be supplied to give a name to the thread. The name is
54used for debugging and informational purposes only; it has no meaning
55to Emacs. If @var{name} is provided, it must be a string.
56
57This function returns the new thread.
58@end defun
59
60@defun threadp object
61This function returns @code{t} if @var{object} represents an Emacs
62thread, @code{nil} otherwise.
63@end defun
64
65@defun thread-join thread
66Block until @var{thread} exits, or until the current thread is signaled.
67@end defun
68
69@defun thread-signal thread error-symbol data
70Like @code{signal} (@pxref{Signaling Errors}), but the signal is
71delivered in the thread @var{thread}. If @var{thread} is the current
72thread, then this just calls @code{signal} immediately.
73@code{thread-signal} will cause a thread to exit a call to
74@code{mutex-lock}, @code{condition-wait}, or @code{thread-join}.
75@end defun
76
77@defun thread-yield
78Yield execution to the next runnable thread.
79@end defun
80
81@defun thread-name thread
82Return the name of @var{thread}, as specified to @code{make-thread}.
83@end defun
84
85@defun thread-alive-p thread
86Return @code{t} if @var{thread} is alive, or @code{nil} if it is not.
87A thread is alive as long as its function is still executing.
88@end defun
89
90@defun thread-blocker thread
91Return the object that @var{thread} is waiting on. This function is
92primarily intended for debugging.
93
94If @var{thread} is blocked in @code{thread-join}, this returns the
95thread for which it is waiting.
96
97If @var{thread} is blocked in @code{mutex-lock}, this returns the mutex.
98
99If @var{thread} is blocked in @code{condition-wait}, this returns the
100condition variable.
101
102Otherwise, this returns @code{nil}.
103@end defun
104
105@defun current-thread
106Return the current thread.
107@end defun
108
109@defun all-threads
110Return a list of all the live thread objects. A new list is returned
111by each invocation.
112@end defun
113
114@node Mutexes
115@section Mutexes
116
117 A @dfn{mutex} is an exclusive lock. At any moment, zero or one
118threads may own a mutex. If a thread attempts to acquire a mutex, and
119the mutex is already owned by some other thread, then the acquiring
120thread will block until the mutex becomes available.
121
122 Emacs Lisp mutexes are of a type called @dfn{recursive}, which means
123that a thread can re-acquire a mutex it owns any number of times. A
124mutex keeps a count of how many times it has been acquired, and each
125acquisition of a mutex must be paired with a release. The last
126release by a thread of a mutex reverts it to the unowned state,
127potentially allowing another thread to acquire the mutex.
128
129@defun mutexp object
130This function returns @code{t} if @var{object} represents an Emacs
131mutex, @code{nil} otherwise.
132@end defun
133
134@defun make-mutex &optional name
135Create a new mutex and return it. If @var{name} is specified, it is a
136name given to the mutex. It must be a string. The name is for
137debugging purposes only; it has no meaning to Emacs.
138@end defun
139
140@defun mutex-name mutex
141Return the name of @var{mutex}, as specified to @code{make-mutex}.
142@end defun
143
144@defun mutex-lock mutex
145This will block until this thread acquires @var{mutex}, or until this
146thread is signaled using @code{thread-signal}. If @var{mutex} is
147already owned by this thread, this simply returns.
148@end defun
149
150@defun mutex-unlock mutex
151Release @var{mutex}. If @var{mutex} is not owned by this thread, this
152will signal an error.
153@end defun
154
155@defmac with-mutex mutex body@dots{}
156This macro is the simplest and safest way to evaluate forms while
157holding a mutex. It acquires @var{mutex}, invokes @var{body}, and
158then releases @var{mutex}. It returns the result of @var{body}.
159@end defmac
160
161@node Condition Variables
162@section Condition Variables
163
164 A @dfn{condition variable} is a way for a thread to block until some
165event occurs. A thread can wait on a condition variable, to be woken
166up when some other thread notifies the condition.
167
168 A condition variable is associated with a mutex and, conceptually,
169with some condition. For proper operation, the mutex must be
170acquired, and then a waiting thread must loop, testing the condition
171and waiting on the condition variable. For example:
172
173@example
174(with-mutex mutex
175 (while (not global-variable)
176 (condition-wait cond-var)))
177@end example
178
179 The mutex ensures atomicity, and the loop is for robustness---there
180may be spurious notifications. Emacs Lisp provides a macro,
181@code{until-condition}, to do this automatically.
182
183 Similarly, the mutex must be held before notifying the condition.
184The typical, and best, approach is to acquire the mutex, make the
185changes associated with this condition, and then signal it:
186
187@example
188(with-mutex mutex
189 (setq global-variable (some-computation))
190 (condition-signal cond-var))
191@end example
192
193@defun make-condition-variable mutex &optional name
194Make a new condition variable associated with @var{mutex}. If
195@var{name} is specified, it is a name given to the condition variable.
196It must be a string. The name is for debugging purposes only; it has
197no meaning to Emacs.
198@end defun
199
200@defun condition-variable-p object
201This function returns @code{t} if @var{object} represents a condition
202variable, @code{nil} otherwise.
203@end defun
204
205@defun condition-wait cond
206Wait for another thread to notify @var{cond}, a condition variable.
207This function will block until the condition is notified, or until a
208signal is delivered to this thread using @code{thread-signal}.
209
210It is an error to call @code{condition-wait} without holding the
211condition's associated mutex.
212
213@code{condition-wait} releases the associated mutex while waiting.
214This allows other threads to acquire the mutex in order to notify the
215condition.
216@end defun
217
218@defun condition-notify cond &optional all
219Notify @var{cond}. The mutex with @var{cond} must be held before
220calling this. Ordinarily a single waiting thread is woken by
221@code{condition-notify}; but if @var{all} is not @code{nil}, then all
222threads waiting on @var{cond} are notified.
223
224@code{condition-notify} releases the associated mutex while waiting.
225This allows other threads to acquire the mutex in order to wait on the
226condition.
227@c why bother?
228@end defun
229
230@defun condition-name cond
231Return the name of @var{cond}, as passed to
232@code{make-condition-variable}.
233@end defun
234
235@defun condition-mutex cond
236Return the mutex associated with @var{cond}. Note that the associated
237mutex cannot be changed.
238@end defun