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