This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Document toke.c.
[perl5.git] / README.threads
CommitLineData
99ed61e6
GS
1NOTE
2
3Threading is a highly experimental feature. There are still a
4few race conditions that show up under high contention on SMP
5machines. Internal implementation is still subject to changes.
6It is not recommended for production use at this time.
7
effcca5c
JH
8---------------------------------------------------------------------------
9
72aaf631
MB
10Building
11
effcca5c 12If your system is in the following list you should be able to just:
e2198c6b 13
effcca5c
JH
14 ./Configure -Dusethreads -des
15 make
69ce17de 16
effcca5c
JH
17and ignore the rest of this "Building" section. If not, continue
18from the "Problems" section.
69ce17de 19
effcca5c
JH
20 * Linux 2.* (with the LinuxThreads library installed:
21 that's the linuxthreads and linuxthreads-devel RPMs
22 for RedHat)
3cec1e99 23
effcca5c
JH
24 * Tru64 UNIX (formerly Digital UNIX formerly DEC OSF/1)
25 (see additional note below)
69ce17de 26
effcca5c 27 * Solaris 2.* for recentish x (2.5 is OK)
69ce17de 28
effcca5c
JH
29 * IRIX 6.2 or newer. 6.2 will require a few OS patches.
30 IMPORTANT: Without patch 2401 (or its replacement),
31 a kernel bug in IRIX 6.2 will cause your machine to
32 panic and crash when running threaded perl.
33 IRIX 6.3 and up should be OK. See lower down for patch details.
e2198c6b 34
effcca5c
JH
35 * AIX 4.1.5 or newer.
36
37 * FreeBSD 2.2.8 or newer.
38
39 * OpenBSD
40
41 * NeXTstep, OpenStep (Rhapsody?)
e2198c6b 42
effcca5c
JH
43 * OS/2
44
45 * DOS DJGPP
46
47 * VM/ESA
48
49---------------------------------------------------------------------------
50
51Problems
52
53If the simple way doesn't work or you are using another platform which
54you believe supports POSIX.1c threads then read on. Additional
55information may be in a platform-specific "hints" file in the hints/
56subdirectory.
57
58First of all, because threads are such an experimentral feature
59there's a failsafe in Configure that stops unknown platforms
60from using threads. Search for "is not known to support threads".
61About five lines above that is a line that has a list of operating
62system names separated with |-signs. Append your operating system
63(perl -le 'print $^O') to that list.
e2198c6b 64
99ed61e6
GS
65On other platforms that use Configure to build perl, omit the -d
66from your ./Configure arguments. For example, use:
e2198c6b
AD
67
68 ./Configure -Dusethreads
69
70When Configure prompts you for ccflags, insert any other arguments in
71there that your compiler needs to use POSIX threads. When Configure
72prompts you for linking flags, include any flags required for
73threading (usually nothing special is required here). Finally, when
effcca5c 74Configure prompts you for libraries, include any necessary libraries
e2198c6b
AD
75(e.g. -lpthread). Pay attention to the order of libraries. It is
76probably necessary to specify your threading library *before* your
77standard C library, e.g. it might be necessary to have -lpthread -lc,
effcca5c
JH
78instead of -lc -lpthread. You may also need to use -lc_r instead
79of -lc.
e2198c6b
AD
80
81Once you have specified all your compiler flags, you can have Configure
82accept all the defaults for the remainder of the session by typing &-d
83at any Configure prompt.
84
85Some additional notes (some of these may be obsolete now, other items
86may be handled automatically):
87
72aaf631 88For Digital Unix 4.x:
e2198c6b 89 Add -pthread to ccflags
72aaf631 90 Add -pthread to ldflags
d81a1b93 91 Add -lpthread -lc_r to lddlflags
e2198c6b 92
72aaf631
MB
93 For some reason, the extra includes for pthreads make Digital UNIX
94 complain fatally about the sbrk() delcaration in perl's malloc.c
e2198c6b
AD
95 so use the native malloc, e.g. sh Configure -Uusemymalloc, or
96 manually edit your config.sh as follows:
97 Change usemymalloc to n
98 Zap mallocobj and mallocsrc (foo='')
99 Change d_mymalloc to undef
100
3cec1e99
GS
101For Digital Unix 3.x (Formerly DEC OSF/1):
102 Add -DOLD_PTHREADS_API to ccflags
effcca5c 103 If compiling with the GNU cc compiler, remove -threads from ccflags
3cec1e99
GS
104
105 (The following should be done automatically if you call Configure
106 with the -Dusethreads option).
107 Add -lpthread -lmach -lc_r to libs (in the order specified).
108
eb1cfdd6 109For IRIX:
e2198c6b 110 (This should all be done automatically by the hint file).
eb1cfdd6 111 Add -lpthread to libs
eb1cfdd6
MB
112 For IRIX 6.2, you have to have the following patches installed:
113 1404 Irix 6.2 Posix 1003.1b man pages
114 1645 IRIX 6.2 & 6.3 POSIX header file updates
115 2000 Irix 6.2 Posix 1003.1b support modules
116 2254 Pthread library fixes
69ce17de
MB
117 2401 6.2 all platform kernel rollup
118 IMPORTANT: Without patch 2401, a kernel bug in IRIX 6.2 will
119 cause your machine to panic and crash when running threaded perl.
120 IRIX 6.3 and up should be OK.
121
eb1cfdd6
MB
122 For IRIX 6.3 and 6.4 the pthreads should work out of the box.
123 Thanks to Hannu Napari <Hannu.Napari@hut.fi> for the IRIX
124 pthreads patches information.
effcca5c 125
ce637636 126For AIX:
e2198c6b 127 (This should all be done automatically by the hint file).
ce637636 128 Change cc to xlc_r or cc_r.
e2198c6b 129 Add -DNEED_PTHREAD_INIT to ccflags and cppflags
ce637636
JH
130 Add -lc_r to libswanted
131 Change -lc in lddflags to be -lpthread -lc_r -lc
72aaf631 132
99ed61e6
GS
133For Win32:
134 See README.win32, and the notes at the beginning of win32/Makefile
135 or win32/makefile.mk.
136
72aaf631 137Now you can do a
d81a1b93
MB
138 make
139
effcca5c
JH
140When you succeed in compiling and testing ("make test" after your
141build) a threaded Perl in a platform previosuly unknown to support
142threaded perl, please let perlbug@perl.com know about your victory.
143Explain what you did in painful detail.
144
145---------------------------------------------------------------------------
72aaf631 146
5756a3ac
MB
147O/S specific bugs
148
e2198c6b 149Irix 6.2: See the Irix warning above.
5756a3ac
MB
150
151LinuxThreads 0.5 has a bug which can cause file descriptor 0 to be
69ce17de
MB
152closed after a fork() leading to many strange symptoms. Version 0.6
153has this fixed but the following patch can be applied to 0.5 for now:
5756a3ac
MB
154
155----------------------------- cut here -----------------------------
156--- linuxthreads-0.5/pthread.c.ORI Mon Oct 6 13:55:50 1997
157+++ linuxthreads-0.5/pthread.c Mon Oct 6 13:57:24 1997
158@@ -312,8 +312,10 @@
159 free(pthread_manager_thread_bos);
160 pthread_manager_thread_bos = pthread_manager_thread_tos = NULL;
161 /* Close the two ends of the pipe */
162- close(pthread_manager_request);
163- close(pthread_manager_reader);
164+ if (pthread_manager_request >= 0) {
165+ close(pthread_manager_request);
166+ close(pthread_manager_reader);
167+ }
168 pthread_manager_request = pthread_manager_reader = -1;
169 /* Update the pid of the main thread */
170 self->p_pid = getpid();
171----------------------------- cut here -----------------------------
172
173
72aaf631
MB
174Building the Thread extension
175
5756a3ac
MB
176The Thread extension is now part of the main perl distribution tree.
177If you did Configure -Dusethreads then it will have been added to
178the list of extensions automatically.
72aaf631 179
5756a3ac
MB
180You can try some of the tests with
181 cd ext/Thread
69ce17de
MB
182 perl create.t
183 perl join.t
184 perl lock.t
185 perl io.t
186etc.
72aaf631
MB
187The io one leaves a thread reading from the keyboard on stdin so
188as the ping messages appear you can type lines and see them echoed.
189
190Try running the main perl test suite too. There are known
69ce17de
MB
191failures for some of the DBM/DB extensions (if their underlying
192libraries were not compiled to be thread-aware).
72aaf631 193
effcca5c 194---------------------------------------------------------------------------
72aaf631
MB
195
196Bugs
197
72aaf631 198* FAKE_THREADS should produce a working perl but the Thread
99ed61e6
GS
199extension won't build with it yet. (FAKE_THREADS has not been
200tested at all in recent times.)
72aaf631 201
5756a3ac 202* There may still be races where bugs show up under contention.
72aaf631 203
effcca5c 204---------------------------------------------------------------------------
72aaf631 205
1304aa9d
MB
206Debugging
207
8b73bbec 208Use the -DS command-line option to turn on debugging of the
1304aa9d
MB
209multi-threading code. Under Linux, that also turns on a quick
210hack I did to grab a bit of extra information from segfaults.
211If you have a fancier gdb/threads setup than I do then you'll
212have to delete the lines in perl.c which say
213 #if defined(DEBUGGING) && defined(USE_THREADS) && defined(__linux__)
8b73bbec 214 DEBUG_S(signal(SIGSEGV, (void(*)(int))catch_sigsegv););
1304aa9d
MB
215 #endif
216
effcca5c 217---------------------------------------------------------------------------
1304aa9d 218
43fe56be
MB
219Background
220
221Some old globals (e.g. stack_sp, op) and some old per-interpreter
222variables (e.g. tmps_stack, cxstack) move into struct thread.
5756a3ac
MB
223All fields of struct thread which derived from original perl
224variables have names of the form Tfoo. For example, stack_sp becomes
43fe56be
MB
225the field Tstack_sp of struct thread. For those fields which moved
226from original perl, thread.h does
227 #define foo (thr->Tfoo)
228This means that all functions in perl which need to use one of these
229fields need an (automatic) variable thr which points at the current
230thread's struct thread. For pp_foo functions, it is passed around as
231an argument, for other functions they do
232 dTHR;
233which declares and initialises thr from thread-specific data
234via pthread_getspecific. If a function fails to compile with an
235error about "no such variable thr", it probably just needs a dTHR
236at the top.
237
238
239Fake threads
240
241For FAKE_THREADS, thr is a global variable and perl schedules threads
242by altering thr in between appropriate ops. The next and prev fields
243of struct thread keep all fake threads on a doubly linked list and
244the next_run and prev_run fields keep all runnable threads on a
245doubly linked list. Mutexes are stubs for FAKE_THREADS. Condition
246variables are implemented as a list of waiting threads.
247
248
249Mutexes and condition variables
250
251The API is via macros MUTEX_{INIT,LOCK,UNLOCK,DESTROY} and
5756a3ac
MB
252COND_{INIT,WAIT,SIGNAL,BROADCAST,DESTROY}.
253
254A mutex is only required to be a simple, fast mutex (e.g. it does not
255have to be recursive). It is only ever held across very short pieces
256of code. Condition variables are only ever signalled/broadcast while
257their associated mutex is held. (This constraint simplifies the
258implementation of condition variables in certain porting situations.)
259For POSIX threads, perl mutexes and condition variables correspond to
260POSIX ones. For FAKE_THREADS, mutexes are stubs and condition variables
261are implmented as lists of waiting threads. For FAKE_THREADS, a thread
43fe56be
MB
262waits on a condition variable by removing itself from the runnable
263list, calling SCHEDULE to change thr to the next appropriate
264runnable thread and returning op (i.e. the new threads next op).
265This means that fake threads can only block while in PP code.
266A PP function which contains a COND_WAIT must be prepared to
267handle such restarts and can use the field "private" of struct
268thread to record its state. For fake threads, COND_SIGNAL and
269COND_BROADCAST work by putting back all the threads on the
270condition variables list into the run queue. Note that a mutex
271must *not* be held while returning from a PP function.
272
c7848ba1
MB
273Perl locks and condition variables are both implemented as a
274condpair_t structure, containing a mutex, an "owner" condition
275variable, an owner thread field and another condition variable).
276The structure is attached by 'm' magic to any SV. pp_lock locks
277such an object by waiting on the ownercond condition variable until
278the owner field is zero and then setting the owner field to its own
279thread pointer. The lock is semantically recursive so if the owner
280field already matches the current thread then pp_lock returns
281straight away. If the owner field has to be filled in then
282unlock_condpair is queued as an end-of-block destructor and
283that function zeroes out the owner field and signals the ownercond
284condition variable, thus waking up any other thread that wants to
285lock it. When used as a condition variable, the condpair is locked
286(involving the above wait-for-ownership and setting the owner field)
287and the spare condition variable field is used for waiting on.
288
289
290Thread states
291
292
293 $t->join
294R_JOINABLE ---------------------> R_JOINED >----\
295 | \ pthread_join(t) | ^ |
296 | \ | | join | pthread_join
297 | \ | | |
298 | \ | \------/
299 | \ |
300 | \ |
301 | $t->detach\ pthread_detach |
302 | _\| |
303ends| R_DETACHED ends | unlink
304 | \ |
305 | ends \ unlink |
306 | \ |
307 | \ |
308 | \ |
309 | \ |
310 | \ |
311 V join detach _\| V
312ZOMBIE ----------------------------> DEAD
313 pthread_join pthread_detach
314 and unlink and unlink
315
43fe56be
MB
316
317
72aaf631
MB
318Malcolm Beattie
319mbeattie@sable.ox.ac.uk
69ce17de 320Last updated: 27 November 1997
e2198c6b
AD
321
322Configure-related info updated 16 July 1998 by
323Andy Dougherty <doughera@lafayette.edu>
99ed61e6
GS
324
325Other minor updates 10 Feb 1999 by
326Gurusamy Sarathy
effcca5c
JH
327
328More platforms added 26 Jul 1999 by
329Jarkko Hietaniemi