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