]> git.pond.sub.org Git - empserver/blob - doc/pthreads
client: Unbreak standalone build
[empserver] / doc / pthreads
1 Below is Sasha Mikheev's documentation on the pthreads support (POSIX threads)
2 which is valid on June 7th, 1996 (date of message.)  I have changed the
3 path names only since the tree has changed.  Otherwise, the docs are correct.
4
5 -- Wolfpack
6
7 ---  Begin message
8
9 Hi,
10
11 I have finished documenting pthreads implementation. Pls look at the
12 results and tell me what can be improved.
13
14 Implementation:
15  Empire now uses its own thread layer. It makes using another
16 threading library much easier. The interfase is modeled after
17 the LWP library which is what empire server uses by default.
18  The interface is specified in the include/empthread.h file. Implementations
19 should go into src/lib/empthread/xxx.c.
20  Empire assumes that thread can not be preempted while it is
21 running. The _only_ places where context switch between threads can
22 occur are:
23  empth_sleep(), empth_yield(), empth_select(), empth_sem_wait().
24 That is in sleep, in waiting for I/O on filedescriptor, semaphore or
25 voluntary yielding control or exiting. In empire empth_select is
26 called only on sockets so the context switch can occur only on the
27 network I/O.
28  Also each empire thread can have it is own context which is restored
29 automatically after the context switch. Currently it is used for each
30 thread to have player specic data. (struct player)
31  
32 LWP implementation:
33  Almost nothing has changed there. src/lib/empthread/lwp.c is a very
34 simple wrapper around the LWP library. The only change is that thread
35 inherits its flags if they are not explicitly set.
36  For more details on LWP internals read Dave Pare's description in
37 doc/threads.
38
39 POSIX threaads: (pthreads)
40  Unlike LWP pthreads are preemtive. The steps must be taken to insure
41 that thread is preempted only as described above. It is done by
42 creating a global mutex mtx_ctxsw. At the any given moment only one
43 thread can be running and have a lock on mtx_ctxsw. Every other thread
44 is waiting for the mtx_ctxsw to become avaiable. It is done by calling
45 pthread_mtx_lock(). When thread enters into empth_sleep(),
46 empth_yield(), empth_exit(), empth_sem_wait() empth_select() it
47 releases the mutex by calling pthread_mtx_unlock() and thus allowing
48 other thread to get a lock.
49  Thread context is implemented by using pthread_getspecific and
50 pthread_setspecific functions.
51  Killing other thread is done by setting victim thread state into
52 EMPTH_KILLED and sending alarm signal to the victim thread. The later
53 has to be done to wake up a thread waiting on I/O in
54 empth_select. Then the victim will examine its state and if it is set
55 to KILLED it will terminate its execution. Unfortunately pthread_kill
56 seems not to be working/exists in some pthread implementations. But
57 fortunately empire always does some I/O before doing
58 empth_terminate().
59  Semaphores are not available in pthreads thus mutexes and conditional
60 variables are used to implement them. See the code and or OS design
61 book for details.
62
63 Problems:
64  On linux with libc 2.1.18 mit pthread library does not wake up a thread upon
65 the delivery of a signal. Which means that server can not be stopped
66 short by kill -9 and empth_wakeup() is not working. Fortunately in
67 current server data are always written to player before the call to
68 empth_wakeup so players threads will never wait in select forever.
69
70 Probably it will be solved when linux libc gets upgraded to latest
71 pthread library.
72
73 In addition installing pthreads on Linux is difficult. By default
74 linux libc has pthread stubs in but pthread code is disabled. So you
75 have either recompile libc or grab mit pthreads and install them
76 manually. 
77  Meanwhile I do not recommend to use pthreads on linux unless you 
78 know what you are doing.
79
80 On solaris 2.5 select does not set errno to EINTR when select is broken
81 by soignal sent by pthread_kill. It doesnt affect anything.
82
83 On OSF/1 the older version of pthread is used. It has slightly changed
84 function semantics. Also it lacks pthread_kill funcion which means
85 that empth_wakeup() is not working. 
86  Calling pthread_exit() from the main thread crashes the
87 program. According to the manual program should not exit until all
88 threads exited.
89  The workaround is to send main into while(1) { sleep(60); } loop;
90
91
92 -- 
93  Sasha Mikheev                      There is a saying in Cairhienin,
94                                     though i have heard it as far away
95  NetVision Israel, Ltd              as Tarabon and Saldaea:
96  sasha@netvision.net.il               
97  Tel  +972-4-8550330                   Take what you want and pay for it.
98  http://aldan.netvision.net.il/~sasha