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