Restore buildability of #+ultrafutex
[sbcl.git] / doc / manual / threading.texinfo
blobeef26297d775407e25d002c4fc28b64b4f9748d7
1 @node  Threading
2 @comment  node-name,  next,  previous,  up
3 @chapter Threading
5 SBCL supports a fairly low-level threading interface that maps onto
6 the host operating system's concept of threads or lightweight
7 processes.  This means that threads may take advantage of hardware
8 multiprocessing on machines that have more than one CPU, but it does
9 not allow Lisp control of the scheduler.  This is found in the
10 SB-THREAD package.
12 Threads are part of the default build on x86[-64]/ARM64 Linux and Windows.
14 They are also supported on: x86[-64] Darwin (Mac OS X), x86[-64]
15 FreeBSD, x86 SunOS (Solaris), PPC Linux, ARM64 Linux, RISC-V Linux. On
16 these platforms threads must be explicitly enabled at build-time, see
17 @file{INSTALL} for directions.
19 @menu
20 * Threading basics::
21 * Special Variables::
22 * Atomic Operations::
23 * Mutex Support::
24 * Semaphores::
25 * Waitqueue/condition variables::
26 * Barriers::
27 * Sessions/Debugging::
28 * Foreign threads::
29 * Implementation (Linux x86/x86-64)::
30 @end menu
32 @node Threading basics
33 @comment  node-name,  next,  previous,  up
34 @section Threading basics
36 @lisp
37 (make-thread (lambda () (write-line "Hello, world")))
38 @end lisp
40 @subsection Thread Objects
42 @include struct-sb-thread-thread.texinfo
43 @include var-sb-thread-star-current-thread-star.texinfo
44 @include fun-sb-thread-list-all-threads.texinfo
45 @include fun-sb-thread-thread-alive-p.texinfo
46 @include fun-sb-thread-thread-name.texinfo
47 @include fun-sb-thread-main-thread-p.texinfo
48 @include fun-sb-thread-main-thread.texinfo
50 @subsection Making, Returning From, Joining, and Yielding Threads
52 @include fun-sb-thread-make-thread.texinfo
53 @include macro-sb-thread-return-from-thread.texinfo
54 @include fun-sb-thread-abort-thread.texinfo
55 @include fun-sb-thread-join-thread.texinfo
56 @include fun-sb-thread-thread-yield.texinfo
58 @subsection Asynchronous Operations
60 @include fun-sb-thread-interrupt-thread.texinfo
61 @include fun-sb-thread-terminate-thread.texinfo
63 @subsection Miscellaneous Operations
65 @include fun-sb-thread-symbol-value-in-thread.texinfo
67 @subsection Error Conditions
69 @include condition-sb-thread-thread-error.texinfo
70 @include fun-sb-thread-thread-error-thread.texinfo
72 @c @include condition-sb-thread-symbol-value-in-thread-error.texinfo
73 @include condition-sb-thread-interrupt-thread-error.texinfo
74 @include condition-sb-thread-join-thread-error.texinfo
76 @node Special Variables
77 @comment  node-name,  next,  previous,  up
78 @section Special Variables
80 The interaction of special variables with multiple threads is mostly
81 as one would expect, with behaviour very similar to other
82 implementations.
84 @itemize
85 @item
86 global special values are visible across all threads;
87 @item
88 bindings (e.g. using LET) are local to the thread;
89 @item
90 threads do not inherit dynamic bindings from the parent thread
91 @end itemize
93 The last point means that
95 @lisp
96 (defparameter *x* 0)
97 (let ((*x* 1))
98   (sb-thread:make-thread (lambda () (print *x*))))
99 @end lisp
101 prints @code{0} and not @code{1} as of 0.9.6.
103 @node Atomic Operations
104 @comment  node-name,  next,  previous,  up
105 @section Atomic Operations
107 Following atomic operations are particularly useful for implementing
108 lockless algorithms.
110 @include macro-sb-ext-atomic-decf.texinfo
111 @include macro-sb-ext-atomic-incf.texinfo
112 @include macro-sb-ext-atomic-pop.texinfo
113 @include macro-sb-ext-atomic-push.texinfo
114 @include macro-sb-ext-atomic-update.texinfo
115 @include macro-sb-ext-compare-and-swap.texinfo
117 @unnumberedsubsec CAS Protocol
119 Our @code{compare-and-swap} is user-extensible by defining functions
120 named (CAS place), allowing users to add CAS support to new
121 places.
123 @include macro-sb-ext-cas.texinfo
125 @node Mutex Support
126 @comment  node-name,  next,  previous,  up
127 @section Mutex Support
129 Mutexes are used for controlling access to a shared resource. One
130 thread is allowed to hold the mutex, others which attempt to take it
131 will be made to wait until it's free. Threads are woken in the order
132 that they go to sleep.
134 @lisp
135 (defpackage :demo (:use "CL" "SB-THREAD" "SB-EXT"))
137 (in-package :demo)
139 (defvar *a-mutex* (make-mutex :name "my lock"))
141 (defun thread-fn ()
142   (format t "Thread ~A running ~%" *current-thread*)
143   (with-mutex (*a-mutex*)
144     (format t "Thread ~A got the lock~%" *current-thread*)
145     (sleep (random 5)))
146   (format t "Thread ~A dropped lock, dying now~%" *current-thread*))
148 (make-thread #'thread-fn)
149 (make-thread #'thread-fn)
150 @end lisp
152 @include struct-sb-thread-mutex.texinfo
154 @include macro-sb-thread-with-mutex.texinfo
155 @include macro-sb-thread-with-recursive-lock.texinfo
157 @include fun-sb-thread-make-mutex.texinfo
158 @include fun-sb-thread-mutex-name.texinfo
159 @include fun-sb-thread-mutex-owner.texinfo
160 @include fun-sb-thread-mutex-value.texinfo
161 @include fun-sb-thread-grab-mutex.texinfo
162 @include fun-sb-thread-release-mutex.texinfo
164 @node Semaphores
165 @comment  node-name,  next,  previous,  up
166 @section Semaphores
168 Semaphores are among other things useful for keeping track of a
169 countable resource, e.g. messages in a queue, and sleep when the
170 resource is exhausted.
172 @include struct-sb-thread-semaphore.texinfo
173 @include fun-sb-thread-make-semaphore.texinfo
174 @include fun-sb-thread-signal-semaphore.texinfo
175 @include fun-sb-thread-wait-on-semaphore.texinfo
176 @include fun-sb-thread-try-semaphore.texinfo
177 @include fun-sb-thread-semaphore-count.texinfo
178 @include fun-sb-thread-semaphore-name.texinfo
180 @include struct-sb-thread-semaphore-notification.texinfo
181 @include fun-sb-thread-make-semaphore-notification.texinfo
182 @include fun-sb-thread-semaphore-notification-status.texinfo
183 @include fun-sb-thread-clear-semaphore-notification.texinfo
185 @node Waitqueue/condition variables
186 @comment  node-name,  next,  previous,  up
187 @section Waitqueue/condition variables
189 These are based on the POSIX condition variable design, hence the
190 annoyingly CL-conflicting name. For use when you want to check a
191 condition and sleep until it's true. For example: you have a shared
192 queue, a writer process checking ``queue is empty'' and one or more
193 readers that need to know when ``queue is not empty''. It sounds
194 simple, but is astonishingly easy to deadlock if another process runs
195 when you weren't expecting it to.
197 There are three components:
199 @itemize
200 @item
201 the condition itself (not represented in code)
203 @item
204 the condition variable (a.k.a. waitqueue) which proxies for it
206 @item
207 a lock to hold while testing the condition
208 @end itemize
210 Important stuff to be aware of:
212 @itemize
213 @item
214 when calling condition-wait, you must hold the mutex. condition-wait
215 will drop the mutex while it waits, and obtain it again before
216 returning for whatever reason;
218 @item
219 likewise, you must be holding the mutex around calls to
220 condition-notify;
222 @item
223 a process may return from condition-wait in several circumstances: it
224 is not guaranteed that the underlying condition has become true. You
225 must check that the resource is ready for whatever you want to do to
228 @end itemize
230 @lisp
231 (defvar *buffer-queue* (make-waitqueue))
232 (defvar *buffer-lock* (make-mutex :name "buffer lock"))
234 (defvar *buffer* (list nil))
236 (defun reader ()
237   (with-mutex (*buffer-lock*)
238     (loop
239      (condition-wait *buffer-queue* *buffer-lock*)
240      (loop
241       (unless *buffer* (return))
242       (let ((head (car *buffer*)))
243         (setf *buffer* (cdr *buffer*))
244         (format t "reader ~A woke, read ~A~%"
245                 *current-thread* head))))))
247 (defun writer ()
248   (loop
249    (sleep (random 5))
250    (with-mutex (*buffer-lock*)
251      (let ((el (intern
252                 (string (code-char
253                          (+ (char-code #\A) (random 26)))))))
254        (setf *buffer* (cons el *buffer*)))
255      (condition-notify *buffer-queue*))))
257 (make-thread #'writer)
258 (make-thread #'reader)
259 (make-thread #'reader)
260 @end lisp
262 @include struct-sb-thread-waitqueue.texinfo
263 @include fun-sb-thread-make-waitqueue.texinfo
264 @include fun-sb-thread-waitqueue-name.texinfo
265 @include fun-sb-thread-condition-wait.texinfo
266 @include fun-sb-thread-condition-notify.texinfo
267 @include fun-sb-thread-condition-broadcast.texinfo
269 @node Barriers
270 @comment  node-name,  next,  previous,  up
271 @section Barriers
273 These are based on the Linux kernel barrier design, which is in turn
274 based on the Alpha CPU memory model. They are presently implemented for
275 x86, x86-64, PPC, ARM64, and RISC-V systems, and behave as compiler
276 barriers on all other CPUs.
278 In addition to explicit use of the @code{sb-thread:barrier} macro, the
279 following functions and macros also serve as @code{:memory} barriers:
281 @itemize
282 @item
283 @code{sb-ext:atomic-decf}, @code{sb-ext:atomic-incf}, @code{sb-ext:atomic-push},
284 and @code{sb-ext:atomic-pop}.
285 @item
286 @code{sb-ext:compare-and-swap}.
287 @item
288 @code{sb-thread:grab-mutex}, @code{sb-thread:release-mutex},
289 @code{sb-thread:with-mutex} and @code{sb-thread:with-recursive-lock}.
290 @item
291 @code{sb-thread:signal-semaphore}, @code{sb-thread:try-semaphore} and
292 @code{sb-thread:wait-on-semaphore}.
293 @item
294 @code{sb-thread:condition-wait}, @code{sb-thread:condition-notify} and
295 @code{sb-thread:condition-broadcast}.
296 @end itemize
298 @include macro-sb-thread-barrier.texinfo
300 @node Sessions/Debugging
301 @comment  node-name,  next,  previous,  up
302 @section Sessions/Debugging
304 If the user has multiple views onto the same Lisp image (for example,
305 using multiple terminals, or a windowing system, or network access)
306 they are typically set up as multiple @dfn{sessions} such that each
307 view has its own collection of foreground/background/stopped threads.
308 A thread which wishes to create a new session can use
309 @code{sb-thread:with-new-session} to remove itself from the current
310 session (which it shares with its parent and siblings) and create a
311 fresh one.
312 # See also @code{sb-thread:make-listener-thread}.
314 Within a single session, threads arbitrate between themselves for the
315 user's attention.  A thread may be in one of three notional states:
316 foreground, background, or stopped.  When a background process
317 attempts to print a repl prompt or to enter the debugger, it will stop
318 and print a message saying that it has stopped.  The user at his
319 leisure may switch to that thread to find out what it needs.  If a
320 background thread enters the debugger, selecting any restart will put
321 it back into the background before it resumes.  Arbitration for the
322 input stream is managed by calls to @code{sb-thread:get-foreground}
323 (which may block) and @code{sb-thread:release-foreground}.
325 @node Foreign threads
326 @comment  node-name,  next,  previous,  up
327 @section Foreign threads
329 Direct calls to @code{pthread_create} (instead of @code{MAKE-THREAD})
330 create threads that SBCL is not aware of, these are called foreign
331 threads. Currently, it is not possible to run Lisp code in such
332 threads. This means that the Lisp side signal handlers cannot work.
333 The best solution is to start foreign threads with signals blocked,
334 but since third party libraries may create threads, it is not always
335 feasible to do so. As a workaround, upon receiving a signal in a
336 foreign thread, SBCL changes the thread's sigmask to block all signals
337 that it wants to handle and resends the signal to the current process
338 which should land in a thread that does not block it, that is, a Lisp
339 thread.
341 The resignalling trick cannot work for synchronously triggered signals
342 (SIGSEGV and co), take care not to trigger any. Resignalling for
343 synchronously triggered signals in foreign threads is subject to
344 @code{--lose-on-corruption}, see @ref{Runtime Options}.
346 @node Implementation (Linux x86/x86-64)
347 @comment  node-name,  next,  previous,  up
348 @section Implementation (Linux x86/x86-64)
350 Threading is implemented using pthreads and some Linux specific bits
351 like futexes.
353 On x86 the per-thread local bindings for special variables is achieved
354 using the %fs segment register to point to a per-thread storage area.
355 This may cause interesting results if you link to foreign code that
356 expects threading or creates new threads, and the thread library in
357 question uses %fs in an incompatible way. On x86-64 the r12 register
358 has a similar role.
360 Queues require the @code{sys_futex()} system call to be available:
361 this is the reason for the NPTL requirement.  We test at runtime that
362 this system call exists.
364 Garbage collection is done with the existing Conservative Generational
365 GC.  Allocation is done in small (typically 8k) regions: each thread
366 has its own region so this involves no stopping. However, when a
367 region fills, a lock must be obtained while another is allocated, and
368 when a collection is required, all processes are stopped.  This is
369 achieved by sending them signals, which may make for interesting
370 behaviour if they are interrupted in system calls.  The streams
371 interface is believed to handle the required system call restarting
372 correctly, but this may be a consideration when making other blocking
373 calls e.g. from foreign library code.
375 Large amounts of the SBCL library have not been inspected for
376 thread-safety.  Some of the obviously unsafe areas have large locks
377 around them, so compilation and fasl loading, for example, cannot be
378 parallelized.  Work is ongoing in this area.
380 A new thread by default is created in the same POSIX process group and
381 session as the thread it was created by.  This has an impact on
382 keyboard interrupt handling: pressing your terminal's intr key
383 (typically @kbd{Control-C}) will interrupt all processes in the
384 foreground process group, including Lisp threads that SBCL considers
385 to be notionally `background'.  This is undesirable, so background
386 threads are set to ignore the SIGINT signal.
388 @code{sb-thread:make-listener-thread} in addition to creating a new
389 Lisp session makes a new POSIX session, so that pressing
390 @kbd{Control-C} in one window will not interrupt another listener -
391 this has been found to be embarrassing.