Update README.threads amd Thread/README
[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
4running Linux 2.x (with the LinuxThreads library installed:
5that's the linuxthreads and linuxthreads-devel RPMs for RedHat)
6or Digital UNIX 4.x or Solaris 2.x for recentish x (2.5 is OK)
7then you should be able to use
8 ./Configure -Dusethreads -Doptimize=-g -ders
9 make
10and ignore the rest of this "Building" section. If it doesn't
11work or you are using another platform which you believe supports
12POSIX.1c threads then read on.
13
72aaf631 14Omit the -e from your ./Configure arguments. For example, use
15 ./Configure -drs
16When it offers to let you change config.sh, do so. If you already
17have a config.sh then you can edit it and do
18 ./Configure -S
19to propagate the required changes.
20In ccflags, insert -DUSE_THREADS (and probably -DDEBUGGING since
21that's what I've been building with). Also insert any other
22arguments in there that your compiler needs to use POSIX threads.
23Change optimize to -g to give you better debugging information.
24Include any necessary explicit libraries in libs and change
25ldflags if you need any linker flags instead or as well.
26
27More explicitly, for Linux (when using the standard kernel-threads
28based LinuxThreads library):
29 Add -DUSE_THREADS -D_REENTRANT -DDEBUGGING to ccflags and cppflags
30 Add -lpthread to libs
31 Change optimize to -g
32For Digital Unix 4.x:
33 Add -pthread -DUSE_THREADS -DDEBUGGING to ccflags
34 Add -DUSE_THREADS -DDEBUGGING to cppflags
35 Add -pthread to ldflags
36 Change optimize to -g
d81a1b93 37 Add -lpthread -lc_r to lddlflags
72aaf631 38 For some reason, the extra includes for pthreads make Digital UNIX
39 complain fatally about the sbrk() delcaration in perl's malloc.c
40 so use the native malloc as follows:
41 Change usemymalloc to n
42 Zap mallocobj and mallocsrc (foo='')
43 Change d_mymalloc to undef
d81a1b93 44For Solaris, do the same as for Linux above.
72aaf631 45
46Now you can do a
d81a1b93 47 make
48
72aaf631 49
50Building the Thread extension
51
52Build it away from the perl tree in the usual way. Set your PATH
53environment variable to have your perl build directory first and
43fe56be 54set PERL5LIB to be /your/perl/build/directory/lib (without those,
55I had problems where the config information from the ordinary perl
56on the system would end up in the Makefile). Then
57 perl Makefile.PL PERL_SRC=/your/perl/build/directory
72aaf631 58 make
72aaf631 59
60Then you can try some of the tests with
61 perl -Mblib create.t
62 perl -Mblib join.t
63 perl -Mblib lock.t
64 perl -Mblib unsync.t
65 perl -Mblib unsync2.t
66 perl -Mblib unsync3.t
67 perl -Mblib io.t
d81a1b93 68 perl -Mblib queue.t
72aaf631 69The io one leaves a thread reading from the keyboard on stdin so
70as the ping messages appear you can type lines and see them echoed.
71
72Try running the main perl test suite too. There are known
73failures for po/misc test 45 (tries to do local(@_) but @_ is
74now lexical) and some tests involving backticks/system/fork
d81a1b93 75may or may not work. Under Linux, many tests may appear to fail
72aaf631 76when run under the test harness but work fine when invoked
77manually.
78
79
80Bugs
81
82* cond.t hasn't been redone since condition variable changed.
83
84* FAKE_THREADS should produce a working perl but the Thread
85extension won't build with it yet.
86
87* There's a known memory leak (curstack isn't freed at the end
88of each thread because it causes refcount problems that I
89haven't tracked down yet) and there are very probably others too.
90
72aaf631 91* There are still races where bugs show up under contention.
92
43fe56be 93* Need to document "lock", Thread.pm, Queue.pm, ...
94
72aaf631 95* Plenty of others
96
97
1304aa9d 98Debugging
99
100Use the -DL command-line option to turn on debugging of the
101multi-threading code. Under Linux, that also turns on a quick
102hack I did to grab a bit of extra information from segfaults.
103If you have a fancier gdb/threads setup than I do then you'll
104have to delete the lines in perl.c which say
105 #if defined(DEBUGGING) && defined(USE_THREADS) && defined(__linux__)
106 DEBUG_L(signal(SIGSEGV, (void(*)(int))catch_sigsegv););
107 #endif
108
109
43fe56be 110Background
111
112Some old globals (e.g. stack_sp, op) and some old per-interpreter
113variables (e.g. tmps_stack, cxstack) move into struct thread.
114All fields of struct thread (apart from a few only applicable to
115FAKE_THREADS) are of the form Tfoo. For example, stack_sp becomes
116the field Tstack_sp of struct thread. For those fields which moved
117from original perl, thread.h does
118 #define foo (thr->Tfoo)
119This means that all functions in perl which need to use one of these
120fields need an (automatic) variable thr which points at the current
121thread's struct thread. For pp_foo functions, it is passed around as
122an argument, for other functions they do
123 dTHR;
124which declares and initialises thr from thread-specific data
125via pthread_getspecific. If a function fails to compile with an
126error about "no such variable thr", it probably just needs a dTHR
127at the top.
128
129
130Fake threads
131
132For FAKE_THREADS, thr is a global variable and perl schedules threads
133by altering thr in between appropriate ops. The next and prev fields
134of struct thread keep all fake threads on a doubly linked list and
135the next_run and prev_run fields keep all runnable threads on a
136doubly linked list. Mutexes are stubs for FAKE_THREADS. Condition
137variables are implemented as a list of waiting threads.
138
139
140Mutexes and condition variables
141
142The API is via macros MUTEX_{INIT,LOCK,UNLOCK,DESTROY} and
143COND_{INIT,WAIT,SIGNAL,BROADCAST,DESTROY}. For POSIX threads,
144perl mutexes and condition variables correspond to POSIX ones.
145For FAKE_THREADS, mutexes are stubs and condition variables are
146implmented as lists of waiting threads. For FAKE_THREADS, a thread
147waits on a condition variable by removing itself from the runnable
148list, calling SCHEDULE to change thr to the next appropriate
149runnable thread and returning op (i.e. the new threads next op).
150This means that fake threads can only block while in PP code.
151A PP function which contains a COND_WAIT must be prepared to
152handle such restarts and can use the field "private" of struct
153thread to record its state. For fake threads, COND_SIGNAL and
154COND_BROADCAST work by putting back all the threads on the
155condition variables list into the run queue. Note that a mutex
156must *not* be held while returning from a PP function.
157
158Perl locks are a condpair_t structure (a triple of a mutex, a
159condtion variable and an owner thread field) attached by 'm'
160magic to any SV. pp_lock locks such an object by waiting on the
161condition variable until the owner field is zero and then setting
162the owner field to its own thread pointer. The lock is recursive
163so if the owner field already matches the current thread then
164pp_lock returns straight away. If the owner field has to be filled
165in then unlock_condpair is queued as an end-of-block destructor and
166that function zeroes out the owner field, releasing the lock.
167
168
72aaf631 169Malcolm Beattie
170mbeattie@sable.ox.ac.uk
d81a1b93 1712 October 1997