| 1 |
@node Native threads |
| 2 |
@section Native threads |
| 3 |
@cindex Native threads |
| 4 |
@ftindex THREADS |
| 5 |
@cfindex --enable-threads [yes|no|AUTO] |
| 6 |
|
| 7 |
@menu |
| 8 |
* Introduction to native threads:: |
| 9 |
* Processes (native threads):: |
| 10 |
* Processes dictionary:: |
| 11 |
* Locks (mutexes):: |
| 12 |
* Locks dictionary:: |
| 13 |
* Readers-writer locks:: |
| 14 |
* Readers-writer locks dictionary:: |
| 15 |
* Condition variables:: |
| 16 |
* Condition variables dictionary:: |
| 17 |
* Semaphores:: |
| 18 |
* Semaphores dictionary:: |
| 19 |
* Barriers:: |
| 20 |
* Barriers dictionary:: |
| 21 |
* Atomic operations:: |
| 22 |
* Atomic operations dictionary:: |
| 23 |
@end menu |
| 24 |
|
| 25 |
@node Introduction to native threads |
| 26 |
@subsection Tasks, threads or processes |
| 27 |
|
| 28 |
On most platforms, ECL supports native multithreading. That means there |
| 29 |
can be several tasks executing lisp code on parallel and sharing memory, |
| 30 |
variables and files. The interface for multitasking in ECL, like those |
| 31 |
of most other implementations, is based on a set of functions and types |
| 32 |
that resemble the multiprocessing capabilities of old Lisp Machines. |
| 33 |
|
| 34 |
This backward compatibility is why tasks or threads are called |
| 35 |
"processes". However, they should not be confused with operating system |
| 36 |
processes, which are made of programs running in separate contexts and |
| 37 |
without access to each other's memory. |
| 38 |
|
| 39 |
The implementation of threads in ECL is purely native and based on Posix |
| 40 |
Threads wherever available. The use of native threads has |
| 41 |
advantages. For instance, they allow for non-blocking file operations, |
| 42 |
so that while one task is reading a file, a different one is performing |
| 43 |
a computation. |
| 44 |
|
| 45 |
As mentioned above, tasks share the same memory, as well as the set of |
| 46 |
open files and sockets. This manifests on two features. First of all, |
| 47 |
different tasks can operate on the same lisp objects, reading and |
| 48 |
writing their slots, or manipulating the same arrays. Second, while |
| 49 |
threads share global variables, constants and function definitions they |
| 50 |
can also have thread-local bindings to special variables that are not |
| 51 |
seen by other tasks. |
| 52 |
|
| 53 |
The fact that different tasks have access to the same set of data allows |
| 54 |
both for flexibility and a greater risk. In order to control access to |
| 55 |
different resources, ECL provides the user with locks, as explained in |
| 56 |
the next section. |
| 57 |
|
| 58 |
@include extensions/mp_ref_process.txi |
| 59 |
@include extensions/mp_ref_mutex.txi |
| 60 |
@include extensions/mp_ref_rwlock.txi |
| 61 |
@include extensions/mp_ref_cv.txi |
| 62 |
@include extensions/mp_ref_sem.txi |
| 63 |
@include extensions/mp_ref_barrier.txi |
| 64 |
@include extensions/mp_ref_atomic.txi |
| 65 |
@c @include extensions/mp_ref_mailbox.txi |