OSDN Git Service

da72fc2ec0e08308e33e52d2d83c820f40ac4d71
[pf3gnuchains/pf3gnuchains4x.git] / winsup / cygwin / cygtls.cc
1 /* cygtls.cc
2
3    Copyright 2003, 2004, 2005 Red Hat, Inc.
4
5 This software is a copyrighted work licensed under the terms of the
6 Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
7 details. */
8
9 #include "winsup.h"
10 #include <sys/time.h>
11 #define USE_SYS_TYPES_FD_SET
12 #include <winsock.h>
13 #include "thread.h"
14 #include "cygtls.h"
15 #include "assert.h"
16 #include <syslog.h>
17 #include <signal.h>
18 #include <malloc.h>
19 #include "exceptions.h"
20 #include "sync.h"
21 #include "cygerrno.h"
22 #include "path.h"
23 #include "fhandler.h"
24 #include "dtable.h"
25 #include "cygheap.h"
26 #include "pinfo.h"
27 #include "sigproc.h"
28
29 class sentry
30 {
31   static muto lock;
32   int destroy;
33 public:
34   void init ();
35   bool acquired () {return lock.acquired ();}
36   sentry () {destroy = 0;}
37   sentry (DWORD wait) {destroy = lock.acquire (wait);}
38   ~sentry () {if (destroy) lock.release ();}
39   friend void _cygtls::init ();
40 };
41
42 muto NO_COPY sentry::lock;
43
44 static size_t NO_COPY nthreads;
45
46 #define THREADLIST_CHUNK 256
47
48 void
49 _cygtls::init ()
50 {
51   if (cygheap->threadlist)
52     memset (cygheap->threadlist, 0, cygheap->sthreads * sizeof (cygheap->threadlist[0]));
53   else
54     {
55       cygheap->sthreads = THREADLIST_CHUNK;
56       cygheap->threadlist = (_cygtls **) ccalloc (HEAP_TLS, cygheap->sthreads,
57                                                      sizeof (cygheap->threadlist[0]));
58     }
59   sentry::lock.init ("sentry_lock");
60 }
61
62 void
63 _cygtls::set_state (bool is_exception)
64 {
65   initialized = CYGTLS_INITIALIZED + is_exception;
66 }
67
68 void
69 _cygtls::reset_exception ()
70 {
71   if (initialized == CYGTLS_EXCEPTION)
72     {
73 #ifdef DEBUGGING
74       debug_printf ("resetting stack after an exception stack %p, stackptr %p", stack, stackptr);
75 #endif
76       set_state (false);
77     }
78 }
79
80 /* Two calls to get the stack right... */
81 void
82 _cygtls::call (DWORD (*func) (void *, void *), void *arg)
83 {
84   char buf[CYGTLS_PADSIZE];
85   call2 (func, arg, buf);
86 }
87
88 void
89 _cygtls::call2 (DWORD (*func) (void *, void *), void *arg, void *buf)
90 {
91   exception_list except_entry;
92   /* Initialize this thread's ability to respond to things like
93      SIGSEGV or SIGFPE. */
94   init_exceptions (&except_entry);
95   _my_tls.init_thread (buf, func);
96   DWORD res = func (arg, buf);
97   _my_tls.remove (INFINITE);
98   ExitThread (res);
99 }
100
101 void
102 _cygtls::init_thread (void *x, DWORD (*func) (void *, void *))
103 {
104   if (x)
105     {
106       memset (this, 0, CYGTLS_PADSIZE);
107       stackptr = stack;
108       if (_GLOBAL_REENT)
109         {
110           local_clib._stdin = _GLOBAL_REENT->_stdin;
111           local_clib._stdout = _GLOBAL_REENT->_stdout;
112           local_clib._stderr = _GLOBAL_REENT->_stderr;
113           local_clib.__sdidinit = _GLOBAL_REENT->__sdidinit ? -1 : 0;
114           local_clib.__cleanup = _GLOBAL_REENT->__cleanup;
115           local_clib.__sglue._niobs = 3;
116           local_clib.__sglue._iobs = &_GLOBAL_REENT->__sf[0];
117         }
118       local_clib._current_locale = "C";
119       locals.process_logmask = LOG_UPTO (LOG_DEBUG);
120     }
121
122   locals.exitsock = INVALID_SOCKET;
123   set_state (false);
124   errno_addr = &(local_clib._errno);
125
126   if ((void *) func == (void *) cygthread::stub
127       || (void *) func == (void *) cygthread::simplestub)
128     return;
129
130   if (wincap.has_security ())
131     cygheap->user.reimpersonate ();
132
133   sentry here (INFINITE);
134   if (nthreads >= cygheap->sthreads)
135     {
136       cygheap->threadlist = (_cygtls **)
137         crealloc (cygheap->threadlist, (cygheap->sthreads += THREADLIST_CHUNK)
138                  * sizeof (cygheap->threadlist[0]));
139       memset (cygheap->threadlist + nthreads, 0, THREADLIST_CHUNK * sizeof (cygheap->threadlist[0]));
140     }
141
142   cygheap->threadlist[nthreads++] = this;
143 }
144
145 void
146 _cygtls::fixup_after_fork ()
147 {
148   if (sig)
149     {
150       pop ();
151       sig = 0;
152     }
153   stacklock = spinning = 0;
154   locals.exitsock = INVALID_SOCKET;
155   wq.thread_ev = NULL;
156 }
157
158 #define free_local(x) \
159   if (locals.x) \
160     { \
161       free (locals.x); \
162       locals.x = NULL; \
163     }
164
165 void
166 _cygtls::remove (DWORD wait)
167 {
168   debug_printf ("wait %p", wait);
169   if (!locals.exitsock)
170     return;
171   if (wait)
172     {
173       /* FIXME: Need some sort of atthreadexit function to allow things like
174          select to control this themselves. */
175       if (locals.exitsock != INVALID_SOCKET)
176         {
177           closesocket (locals.exitsock);
178           locals.exitsock = (SOCKET) NULL;
179         }
180       free_local (process_ident);
181       free_local (ntoa_buf);
182       free_local (protoent_buf);
183       free_local (servent_buf);
184       free_local (hostent_buf);
185     }
186
187   do
188     {
189       sentry here (wait);
190       if (here.acquired ())
191         {
192           for (size_t i = 0; i < nthreads; i++)
193             if (this == cygheap->threadlist[i])
194               {
195                 if (i < --nthreads)
196                   cygheap->threadlist[i] = cygheap->threadlist[nthreads];
197                 debug_printf ("removed %p element %d", this, i);
198                 break;
199               }
200         }
201     } while (0);
202   remove_wq (wait);
203 }
204
205 void
206 _cygtls::push (__stack_t addr, bool exception)
207 {
208   if (exception)
209     lock ();
210   *stackptr++ = (__stack_t) addr;
211   if (exception)
212     unlock ();
213   set_state (exception);
214 }
215
216 #define BAD_IX ((size_t) -1)
217 static size_t NO_COPY threadlist_ix = BAD_IX;
218
219 _cygtls *
220 _cygtls::find_tls (int sig)
221 {
222   debug_printf ("sig %d\n", sig);
223   sentry here (INFINITE);
224   __asm__ volatile (".equ _threadlist_exception_return,.");
225   _cygtls *res = NULL;
226   for (threadlist_ix = 0; threadlist_ix < nthreads; threadlist_ix++)
227     if (sigismember (&(cygheap->threadlist[threadlist_ix]->sigwait_mask), sig))
228       {
229         res = cygheap->threadlist[threadlist_ix];
230         break;
231       }
232   threadlist_ix = BAD_IX;
233   return res;
234 }
235
236 void
237 _cygtls::set_siginfo (sigpacket *pack)
238 {
239   infodata = pack->si;
240 }
241
242 extern "C" DWORD __stdcall RtlUnwind (void *, void *, void *, DWORD);
243 static int
244 handle_threadlist_exception (EXCEPTION_RECORD *e, void *frame, CONTEXT *c, void *)
245 {
246   if (e->ExceptionCode != STATUS_ACCESS_VIOLATION)
247     {
248       system_printf ("unhandled exception %p at %p", e->ExceptionCode, c->Eip);
249       return 1;
250     }
251
252   sentry here;
253   if (threadlist_ix == BAD_IX)
254     {
255       system_printf ("called with threadlist_ix %d", BAD_IX);
256       return 1;
257     }
258
259   if (!here.acquired ())
260     {
261       system_printf ("couldn't aquire muto");
262       return 1;
263     }
264
265   extern void *threadlist_exception_return;
266   cygheap->threadlist[threadlist_ix]->remove (INFINITE);
267   threadlist_ix = 0;
268   RtlUnwind (frame, threadlist_exception_return, e, 0);
269   return 0;
270 }
271
272 void
273 _cygtls::init_threadlist_exceptions (exception_list *el)
274 {
275   extern void init_exception_handler (exception_list *, exception_handler *);
276   init_exception_handler (el, handle_threadlist_exception);
277 }