aboutsummaryrefslogtreecommitdiffstats
path: root/doc
diff options
context:
space:
mode:
authorEli Zaretskii2016-12-04 19:59:17 +0200
committerEli Zaretskii2016-12-04 19:59:17 +0200
commitde4624c99ea5bbe38ad5aff7b6461cc5c740d0be (patch)
tree1b57de9e769cdb695cb2cecf157b50f7dea9cfe5 /doc
parenta486fabb41cdbaa5813c2687fd4008945297d71d (diff)
parente7bde34e939451d87fb42a36195086bdbe48b5e1 (diff)
downloademacs-de4624c99ea5bbe38ad5aff7b6461cc5c740d0be.tar.gz
emacs-de4624c99ea5bbe38ad5aff7b6461cc5c740d0be.zip
Merge branch 'concurrency'
Conflicts (resolved): configure.ac src/Makefile.in src/alloc.c src/bytecode.c src/emacs.c src/eval.c src/lisp.h src/process.c src/regex.c src/regex.h
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/processes.texi30
-rw-r--r--doc/lispref/threads.texi252
5 files changed, 306 insertions, 5 deletions
diff --git a/doc/lispref/Makefile.in b/doc/lispref/Makefile.in
index 7aadee7adea..5bf6e99d587 100644
--- a/doc/lispref/Makefile.in
+++ b/doc/lispref/Makefile.in
@@ -125,6 +125,7 @@ srcs = \
125 $(srcdir)/symbols.texi \ 125 $(srcdir)/symbols.texi \
126 $(srcdir)/syntax.texi \ 126 $(srcdir)/syntax.texi \
127 $(srcdir)/text.texi \ 127 $(srcdir)/text.texi \
128 $(srcdir)/threads.texi \
128 $(srcdir)/tips.texi \ 129 $(srcdir)/tips.texi \
129 $(srcdir)/variables.texi \ 130 $(srcdir)/variables.texi \
130 $(srcdir)/windows.texi \ 131 $(srcdir)/windows.texi \
diff --git a/doc/lispref/elisp.texi b/doc/lispref/elisp.texi
index 6983ab77c63..415dbe66fac 100644
--- a/doc/lispref/elisp.texi
+++ b/doc/lispref/elisp.texi
@@ -219,6 +219,7 @@ To view this manual in other formats, click
219* Syntax Tables:: The syntax table controls word and list parsing. 219* Syntax Tables:: The syntax table controls word and list parsing.
220* Abbrevs:: How Abbrev mode works, and its data structures. 220* Abbrevs:: How Abbrev mode works, and its data structures.
221 221
222* Threads:: Concurrency in Emacs Lisp.
222* Processes:: Running and communicating with subprocesses. 223* Processes:: Running and communicating with subprocesses.
223* Display:: Features for controlling the screen display. 224* Display:: Features for controlling the screen display.
224* System Interface:: Getting the user id, system type, environment 225* System Interface:: Getting the user id, system type, environment
@@ -1322,6 +1323,12 @@ Abbrevs and Abbrev Expansion
1322* Abbrev Table Properties:: How to read and set abbrev table properties. 1323* Abbrev Table Properties:: How to read and set abbrev table properties.
1323 Which properties have which effect. 1324 Which properties have which effect.
1324 1325
1326Threads
1327
1328* Basic Thread Functions:: Basic thread functions.
1329* Mutexes:: Mutexes allow exclusive access to data.
1330* Condition Variables:: Inter-thread events.
1331
1325Processes 1332Processes
1326 1333
1327* Subprocess Creation:: Functions that start subprocesses. 1334* Subprocess Creation:: Functions that start subprocesses.
@@ -1628,6 +1635,7 @@ Object Internals
1628@include searching.texi 1635@include searching.texi
1629@include syntax.texi 1636@include syntax.texi
1630@include abbrevs.texi 1637@include abbrevs.texi
1638@include threads.texi
1631@include processes.texi 1639@include processes.texi
1632 1640
1633@include display.texi 1641@include display.texi
diff --git a/doc/lispref/objects.texi b/doc/lispref/objects.texi
index 54894b8e24e..a76fbb1af7f 100644
--- a/doc/lispref/objects.texi
+++ b/doc/lispref/objects.texi
@@ -1925,6 +1925,15 @@ with references to further information.
1925 1925
1926@item string-or-null-p 1926@item string-or-null-p
1927@xref{Predicates for Strings, string-or-null-p}. 1927@xref{Predicates for Strings, string-or-null-p}.
1928
1929@item threadp
1930@xref{Basic Thread Functions, threadp}.
1931
1932@item mutexp
1933@xref{Mutexes, mutexp}.
1934
1935@item condition-variable-p
1936@xref{Condition Variables, condition-variable-p}.
1928@end table 1937@end table
1929 1938
1930 The most general way to check the type of an object is to call the 1939 The most general way to check the type of an object is to call the
@@ -1938,11 +1947,12 @@ types. In most cases, it is more convenient to use type predicates than
1938This function returns a symbol naming the primitive type of 1947This function returns a symbol naming the primitive type of
1939@var{object}. The value is one of the symbols @code{bool-vector}, 1948@var{object}. The value is one of the symbols @code{bool-vector},
1940@code{buffer}, @code{char-table}, @code{compiled-function}, 1949@code{buffer}, @code{char-table}, @code{compiled-function},
1941@code{cons}, @code{finalizer}, @code{float}, @code{font-entity}, 1950@code{condition-variable}, @code{cons}, @code{finalizer},
1942@code{font-object}, @code{font-spec}, @code{frame}, @code{hash-table}, 1951@code{float}, @code{font-entity}, @code{font-object},
1943@code{integer}, @code{marker}, @code{overlay}, @code{process}, 1952@code{font-spec}, @code{frame}, @code{hash-table}, @code{integer},
1944@code{string}, @code{subr}, @code{symbol}, @code{vector}, 1953@code{marker}, @code{mutex}, @code{overlay}, @code{process},
1945@code{window}, or @code{window-configuration}. 1954@code{string}, @code{subr}, @code{symbol}, @code{thread},
1955@code{vector}, @code{window}, or @code{window-configuration}.
1946 1956
1947@example 1957@example
1948(type-of 1) 1958(type-of 1)
diff --git a/doc/lispref/processes.texi b/doc/lispref/processes.texi
index 21e1429f59d..064934cc662 100644
--- a/doc/lispref/processes.texi
+++ b/doc/lispref/processes.texi
@@ -1400,6 +1400,7 @@ Emacs tries to read it.
1400* Filter Functions:: Filter functions accept output from the process. 1400* Filter Functions:: Filter functions accept output from the process.
1401* Decoding Output:: Filters can get unibyte or multibyte strings. 1401* Decoding Output:: Filters can get unibyte or multibyte strings.
1402* Accepting Output:: How to wait until process output arrives. 1402* Accepting Output:: How to wait until process output arrives.
1403* Processes and Threads:: How processes and threads interact.
1403@end menu 1404@end menu
1404 1405
1405@node Process Buffers 1406@node Process Buffers
@@ -1791,6 +1792,35 @@ got output from @var{process}, or from any process if @var{process} is
1791arrived. 1792arrived.
1792@end defun 1793@end defun
1793 1794
1795@node Processes and Threads
1796@subsection Processes and Threads
1797@cindex processes, threads
1798
1799 Because threads were a relatively late addition to Emacs Lisp, and
1800due to the way dynamic binding was sometimes used in conjunction with
1801@code{accept-process-output}, by default a process is locked to the
1802thread that created it. When a process is locked to a thread, output
1803from the process can only be accepted by that thread.
1804
1805 A Lisp program can specify to which thread a process is to be
1806locked, or instruct Emacs to unlock a process, in which case its
1807output can be processed by any thread. Only a single thread will wait
1808for output from a given process at one time---once one thread begins
1809waiting for output, the process is temporarily locked until
1810@code{accept-process-output} or @code{sit-for} returns.
1811
1812 If the thread exits, all the processes locked to it are unlocked.
1813
1814@defun process-thread process
1815Return the thread to which @var{process} is locked. If @var{process}
1816is unlocked, return @code{nil}.
1817@end defun
1818
1819@defun set-process-thread process thread
1820Set the locking thread of @var{process} to @var{thread}. @var{thread}
1821may be @code{nil}, in which case the process is unlocked.
1822@end defun
1823
1794@node Sentinels 1824@node Sentinels
1795@section Sentinels: Detecting Process Status Changes 1825@section Sentinels: Detecting Process Status Changes
1796@cindex process sentinel 1826@cindex process sentinel
diff --git a/doc/lispref/threads.texi b/doc/lispref/threads.texi
new file mode 100644
index 00000000000..6237392db3a
--- /dev/null
+++ b/doc/lispref/threads.texi
@@ -0,0 +1,252 @@
1@c -*-texinfo-*-
2@c This is part of the GNU Emacs Lisp Reference Manual.
3@c Copyright (C) 2012, 2013
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. Each
31thread also has its own current buffer (@pxref{Current Buffer}) and
32its own match data (@pxref{Match Data}).
33
34 Note that @code{let} bindings are treated specially by the Emacs
35Lisp implementation. There is no way to duplicate this unwinding and
36rewinding behavior other than by using @code{let}. For example, a
37manual implementation of @code{let} written using
38@code{unwind-protect} cannot arrange for variable values to be
39thread-specific.
40
41 In the case of lexical bindings (@pxref{Variable Scoping}), a
42closure is an object like any other in Emacs Lisp, and bindings in a
43closure are shared by any threads invoking the closure.
44
45@menu
46* Basic Thread Functions:: Basic thread functions.
47* Mutexes:: Mutexes allow exclusive access to data.
48* Condition Variables:: Inter-thread events.
49@end menu
50
51@node Basic Thread Functions
52@section Basic Thread Functions
53
54 Threads can be created and waited for. A thread cannot be exited
55directly, but the current thread can be exited implicitly, and other
56threads can be signaled.
57
58@defun make-thread function &optional name
59Create a new thread of execution which invokes @var{function}. When
60@var{function} returns, the thread exits.
61
62The new thread is created with no local variable bindings in effect.
63The new thread's current buffer is inherited from the current thread.
64
65@var{name} can be supplied to give a name to the thread. The name is
66used for debugging and informational purposes only; it has no meaning
67to Emacs. If @var{name} is provided, it must be a string.
68
69This function returns the new thread.
70@end defun
71
72@defun threadp object
73This function returns @code{t} if @var{object} represents an Emacs
74thread, @code{nil} otherwise.
75@end defun
76
77@defun thread-join thread
78Block until @var{thread} exits, or until the current thread is
79signaled. If @var{thread} has already exited, this returns
80immediately.
81@end defun
82
83@defun thread-signal thread error-symbol data
84Like @code{signal} (@pxref{Signaling Errors}), but the signal is
85delivered in the thread @var{thread}. If @var{thread} is the current
86thread, then this just calls @code{signal} immediately.
87@code{thread-signal} will cause a thread to exit a call to
88@code{mutex-lock}, @code{condition-wait}, or @code{thread-join}.
89@end defun
90
91@defun thread-yield
92Yield execution to the next runnable thread.
93@end defun
94
95@defun thread-name thread
96Return the name of @var{thread}, as specified to @code{make-thread}.
97@end defun
98
99@defun thread-alive-p thread
100Return @code{t} if @var{thread} is alive, or @code{nil} if it is not.
101A thread is alive as long as its function is still executing.
102@end defun
103
104@defun thread--blocker thread
105Return the object that @var{thread} is waiting on. This function is
106primarily intended for debugging, and is given a ``double hyphen''
107name to indicate that.
108
109If @var{thread} is blocked in @code{thread-join}, this returns the
110thread for which it is waiting.
111
112If @var{thread} is blocked in @code{mutex-lock}, this returns the mutex.
113
114If @var{thread} is blocked in @code{condition-wait}, this returns the
115condition variable.
116
117Otherwise, this returns @code{nil}.
118@end defun
119
120@defun current-thread
121Return the current thread.
122@end defun
123
124@defun all-threads
125Return a list of all the live thread objects. A new list is returned
126by each invocation.
127@end defun
128
129@node Mutexes
130@section Mutexes
131
132 A @dfn{mutex} is an exclusive lock. At any moment, zero or one
133threads may own a mutex. If a thread attempts to acquire a mutex, and
134the mutex is already owned by some other thread, then the acquiring
135thread will block until the mutex becomes available.
136
137 Emacs Lisp mutexes are of a type called @dfn{recursive}, which means
138that a thread can re-acquire a mutex it owns any number of times. A
139mutex keeps a count of how many times it has been acquired, and each
140acquisition of a mutex must be paired with a release. The last
141release by a thread of a mutex reverts it to the unowned state,
142potentially allowing another thread to acquire the mutex.
143
144@defun mutexp object
145This function returns @code{t} if @var{object} represents an Emacs
146mutex, @code{nil} otherwise.
147@end defun
148
149@defun make-mutex &optional name
150Create a new mutex and return it. If @var{name} is specified, it is a
151name given to the mutex. It must be a string. The name is for
152debugging purposes only; it has no meaning to Emacs.
153@end defun
154
155@defun mutex-name mutex
156Return the name of @var{mutex}, as specified to @code{make-mutex}.
157@end defun
158
159@defun mutex-lock mutex
160This will block until this thread acquires @var{mutex}, or until this
161thread is signaled using @code{thread-signal}. If @var{mutex} is
162already owned by this thread, this simply returns.
163@end defun
164
165@defun mutex-unlock mutex
166Release @var{mutex}. If @var{mutex} is not owned by this thread, this
167will signal an error.
168@end defun
169
170@defmac with-mutex mutex body@dots{}
171This macro is the simplest and safest way to evaluate forms while
172holding a mutex. It acquires @var{mutex}, invokes @var{body}, and
173then releases @var{mutex}. It returns the result of @var{body}.
174@end defmac
175
176@node Condition Variables
177@section Condition Variables
178
179 A @dfn{condition variable} is a way for a thread to block until some
180event occurs. A thread can wait on a condition variable, to be woken
181up when some other thread notifies the condition.
182
183 A condition variable is associated with a mutex and, conceptually,
184with some condition. For proper operation, the mutex must be
185acquired, and then a waiting thread must loop, testing the condition
186and waiting on the condition variable. For example:
187
188@example
189(with-mutex mutex
190 (while (not global-variable)
191 (condition-wait cond-var)))
192@end example
193
194 The mutex ensures atomicity, and the loop is for robustness---there
195may be spurious notifications.
196
197 Similarly, the mutex must be held before notifying the condition.
198The typical, and best, approach is to acquire the mutex, make the
199changes associated with this condition, and then notify it:
200
201@example
202(with-mutex mutex
203 (setq global-variable (some-computation))
204 (condition-notify cond-var))
205@end example
206
207@defun make-condition-variable mutex &optional name
208Make a new condition variable associated with @var{mutex}. If
209@var{name} is specified, it is a name given to the condition variable.
210It must be a string. The name is for debugging purposes only; it has
211no meaning to Emacs.
212@end defun
213
214@defun condition-variable-p object
215This function returns @code{t} if @var{object} represents a condition
216variable, @code{nil} otherwise.
217@end defun
218
219@defun condition-wait cond
220Wait for another thread to notify @var{cond}, a condition variable.
221This function will block until the condition is notified, or until a
222signal is delivered to this thread using @code{thread-signal}.
223
224It is an error to call @code{condition-wait} without holding the
225condition's associated mutex.
226
227@code{condition-wait} releases the associated mutex while waiting.
228This allows other threads to acquire the mutex in order to notify the
229condition.
230@end defun
231
232@defun condition-notify cond &optional all
233Notify @var{cond}. The mutex with @var{cond} must be held before
234calling this. Ordinarily a single waiting thread is woken by
235@code{condition-notify}; but if @var{all} is not @code{nil}, then all
236threads waiting on @var{cond} are notified.
237
238@code{condition-notify} releases the associated mutex while waiting.
239This allows other threads to acquire the mutex in order to wait on the
240condition.
241@c why bother?
242@end defun
243
244@defun condition-name cond
245Return the name of @var{cond}, as passed to
246@code{make-condition-variable}.
247@end defun
248
249@defun condition-mutex cond
250Return the mutex associated with @var{cond}. Note that the associated
251mutex cannot be changed.
252@end defun