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