diff options
| author | Tom Tromey | 2012-08-23 13:58:38 -0600 |
|---|---|---|
| committer | Tom Tromey | 2012-08-23 13:58:38 -0600 |
| commit | 0ec3764d398add8c038b317201d139aaef4a594e (patch) | |
| tree | bbc0df921785cb065568657f9a6594ad5b10447d /doc | |
| parent | c6bb874290bb0d56d2caa106fc3989cf34a72c3e (diff) | |
| download | emacs-0ec3764d398add8c038b317201d139aaef4a594e.tar.gz emacs-0ec3764d398add8c038b317201d139aaef4a594e.zip | |
first draft of threads documentation
Diffstat (limited to 'doc')
| -rw-r--r-- | doc/lispref/Makefile.in | 1 | ||||
| -rw-r--r-- | doc/lispref/elisp.texi | 8 | ||||
| -rw-r--r-- | doc/lispref/objects.texi | 20 | ||||
| -rw-r--r-- | doc/lispref/threads.texi | 238 |
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 | ||
| 1273 | Threads | ||
| 1274 | |||
| 1275 | * Basic Thread Functions:: Basic thread functions. | ||
| 1276 | * Mutexes:: Mutexes allow exclusive access to data. | ||
| 1277 | * Condition Variables:: Inter-thread events. | ||
| 1278 | |||
| 1272 | Processes | 1279 | Processes |
| 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 | |||
| 1898 | This function returns a symbol naming the primitive type of | 1907 | This 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 | ||
| 13 | same memory. Concurrency in Emacs Lisp is ``mostly cooperative'', | ||
| 14 | meaning that Emacs will only switch execution between threads at | ||
| 15 | well-defined times. However, the Emacs thread support has been | ||
| 16 | designed in a way to later allow more fine-grained concurrency, and | ||
| 17 | correct 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 | ||
| 21 | output (e.g., during @code{accept-process-output}), or during blocking | ||
| 22 | operations relating to threads, such as mutex locking or | ||
| 23 | @code{thread-join}. | ||
| 24 | |||
| 25 | Emacs Lisp provides primitives to create and control threads, and | ||
| 26 | also to create and control mutexes and condition variables, useful for | ||
| 27 | thread synchronization. | ||
| 28 | |||
| 29 | While global variables are shared among all Emacs Lisp threads, | ||
| 30 | local variables are not---a dynamic @code{let} binding is local. | ||
| 31 | |||
| 32 | In the case of lexical bindings (@pxref{Variable Scoping}), a | ||
| 33 | closure is an object like any other in Emacs Lisp, and bindings in a | ||
| 34 | closure 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 | ||
| 46 | directly, but the current thread can be exited implicitly, and other | ||
| 47 | threads can be signaled. | ||
| 48 | |||
| 49 | @defun make-thread function &optional name | ||
| 50 | Create 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 | ||
| 54 | used for debugging and informational purposes only; it has no meaning | ||
| 55 | to Emacs. If @var{name} is provided, it must be a string. | ||
| 56 | |||
| 57 | This function returns the new thread. | ||
| 58 | @end defun | ||
| 59 | |||
| 60 | @defun threadp object | ||
| 61 | This function returns @code{t} if @var{object} represents an Emacs | ||
| 62 | thread, @code{nil} otherwise. | ||
| 63 | @end defun | ||
| 64 | |||
| 65 | @defun thread-join thread | ||
| 66 | Block until @var{thread} exits, or until the current thread is signaled. | ||
| 67 | @end defun | ||
| 68 | |||
| 69 | @defun thread-signal thread error-symbol data | ||
| 70 | Like @code{signal} (@pxref{Signaling Errors}), but the signal is | ||
| 71 | delivered in the thread @var{thread}. If @var{thread} is the current | ||
| 72 | thread, 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 | ||
| 78 | Yield execution to the next runnable thread. | ||
| 79 | @end defun | ||
| 80 | |||
| 81 | @defun thread-name thread | ||
| 82 | Return the name of @var{thread}, as specified to @code{make-thread}. | ||
| 83 | @end defun | ||
| 84 | |||
| 85 | @defun thread-alive-p thread | ||
| 86 | Return @code{t} if @var{thread} is alive, or @code{nil} if it is not. | ||
| 87 | A thread is alive as long as its function is still executing. | ||
| 88 | @end defun | ||
| 89 | |||
| 90 | @defun thread-blocker thread | ||
| 91 | Return the object that @var{thread} is waiting on. This function is | ||
| 92 | primarily intended for debugging. | ||
| 93 | |||
| 94 | If @var{thread} is blocked in @code{thread-join}, this returns the | ||
| 95 | thread for which it is waiting. | ||
| 96 | |||
| 97 | If @var{thread} is blocked in @code{mutex-lock}, this returns the mutex. | ||
| 98 | |||
| 99 | If @var{thread} is blocked in @code{condition-wait}, this returns the | ||
| 100 | condition variable. | ||
| 101 | |||
| 102 | Otherwise, this returns @code{nil}. | ||
| 103 | @end defun | ||
| 104 | |||
| 105 | @defun current-thread | ||
| 106 | Return the current thread. | ||
| 107 | @end defun | ||
| 108 | |||
| 109 | @defun all-threads | ||
| 110 | Return a list of all the live thread objects. A new list is returned | ||
| 111 | by 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 | ||
| 118 | threads may own a mutex. If a thread attempts to acquire a mutex, and | ||
| 119 | the mutex is already owned by some other thread, then the acquiring | ||
| 120 | thread will block until the mutex becomes available. | ||
| 121 | |||
| 122 | Emacs Lisp mutexes are of a type called @dfn{recursive}, which means | ||
| 123 | that a thread can re-acquire a mutex it owns any number of times. A | ||
| 124 | mutex keeps a count of how many times it has been acquired, and each | ||
| 125 | acquisition of a mutex must be paired with a release. The last | ||
| 126 | release by a thread of a mutex reverts it to the unowned state, | ||
| 127 | potentially allowing another thread to acquire the mutex. | ||
| 128 | |||
| 129 | @defun mutexp object | ||
| 130 | This function returns @code{t} if @var{object} represents an Emacs | ||
| 131 | mutex, @code{nil} otherwise. | ||
| 132 | @end defun | ||
| 133 | |||
| 134 | @defun make-mutex &optional name | ||
| 135 | Create a new mutex and return it. If @var{name} is specified, it is a | ||
| 136 | name given to the mutex. It must be a string. The name is for | ||
| 137 | debugging purposes only; it has no meaning to Emacs. | ||
| 138 | @end defun | ||
| 139 | |||
| 140 | @defun mutex-name mutex | ||
| 141 | Return the name of @var{mutex}, as specified to @code{make-mutex}. | ||
| 142 | @end defun | ||
| 143 | |||
| 144 | @defun mutex-lock mutex | ||
| 145 | This will block until this thread acquires @var{mutex}, or until this | ||
| 146 | thread is signaled using @code{thread-signal}. If @var{mutex} is | ||
| 147 | already owned by this thread, this simply returns. | ||
| 148 | @end defun | ||
| 149 | |||
| 150 | @defun mutex-unlock mutex | ||
| 151 | Release @var{mutex}. If @var{mutex} is not owned by this thread, this | ||
| 152 | will signal an error. | ||
| 153 | @end defun | ||
| 154 | |||
| 155 | @defmac with-mutex mutex body@dots{} | ||
| 156 | This macro is the simplest and safest way to evaluate forms while | ||
| 157 | holding a mutex. It acquires @var{mutex}, invokes @var{body}, and | ||
| 158 | then 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 | ||
| 165 | event occurs. A thread can wait on a condition variable, to be woken | ||
| 166 | up when some other thread notifies the condition. | ||
| 167 | |||
| 168 | A condition variable is associated with a mutex and, conceptually, | ||
| 169 | with some condition. For proper operation, the mutex must be | ||
| 170 | acquired, and then a waiting thread must loop, testing the condition | ||
| 171 | and 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 | ||
| 180 | may 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. | ||
| 184 | The typical, and best, approach is to acquire the mutex, make the | ||
| 185 | changes 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 | ||
| 194 | Make a new condition variable associated with @var{mutex}. If | ||
| 195 | @var{name} is specified, it is a name given to the condition variable. | ||
| 196 | It must be a string. The name is for debugging purposes only; it has | ||
| 197 | no meaning to Emacs. | ||
| 198 | @end defun | ||
| 199 | |||
| 200 | @defun condition-variable-p object | ||
| 201 | This function returns @code{t} if @var{object} represents a condition | ||
| 202 | variable, @code{nil} otherwise. | ||
| 203 | @end defun | ||
| 204 | |||
| 205 | @defun condition-wait cond | ||
| 206 | Wait for another thread to notify @var{cond}, a condition variable. | ||
| 207 | This function will block until the condition is notified, or until a | ||
| 208 | signal is delivered to this thread using @code{thread-signal}. | ||
| 209 | |||
| 210 | It is an error to call @code{condition-wait} without holding the | ||
| 211 | condition's associated mutex. | ||
| 212 | |||
| 213 | @code{condition-wait} releases the associated mutex while waiting. | ||
| 214 | This allows other threads to acquire the mutex in order to notify the | ||
| 215 | condition. | ||
| 216 | @end defun | ||
| 217 | |||
| 218 | @defun condition-notify cond &optional all | ||
| 219 | Notify @var{cond}. The mutex with @var{cond} must be held before | ||
| 220 | calling this. Ordinarily a single waiting thread is woken by | ||
| 221 | @code{condition-notify}; but if @var{all} is not @code{nil}, then all | ||
| 222 | threads waiting on @var{cond} are notified. | ||
| 223 | |||
| 224 | @code{condition-notify} releases the associated mutex while waiting. | ||
| 225 | This allows other threads to acquire the mutex in order to wait on the | ||
| 226 | condition. | ||
| 227 | @c why bother? | ||
| 228 | @end defun | ||
| 229 | |||
| 230 | @defun condition-name cond | ||
| 231 | Return the name of @var{cond}, as passed to | ||
| 232 | @code{make-condition-variable}. | ||
| 233 | @end defun | ||
| 234 | |||
| 235 | @defun condition-mutex cond | ||
| 236 | Return the mutex associated with @var{cond}. Note that the associated | ||
| 237 | mutex cannot be changed. | ||
| 238 | @end defun | ||