Cygwin: (mostly) drop NT4 and Samba < 3.0 support
[newlib-cygwin.git] / winsup / cygwin / cygtls.cc
blobafaee8e977872e57d56eb6303d61ee2b55cbefe9
1 /* cygtls.cc
3 This software is a copyrighted work licensed under the terms of the
4 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
5 details. */
7 #include "winsup.h"
8 #define USE_SYS_TYPES_FD_SET
9 #include "cygtls.h"
10 #include <syslog.h>
11 #include <stdlib.h>
12 #include "path.h"
13 #include "fhandler.h"
14 #include "dtable.h"
15 #include "create_posix_thread.h"
16 #include "cygheap.h"
17 #include "sigproc.h"
18 #include "exception.h"
20 /* Two calls to get the stack right... */
21 void
22 _cygtls::call (DWORD (*func) (void *, void *), void *arg)
24 char buf[__CYGTLS_PADSIZE__];
25 /* Initialize this thread's ability to respond to things like
26 SIGSEGV or SIGFPE. */
27 exception protect;
28 _my_tls.call2 (func, arg, buf);
31 void
32 _cygtls::call2 (DWORD (*func) (void *, void *), void *arg, void *buf)
34 /* If func is pthread_wrapper, the final stack hasn't been set up yet.
35 This only happens in pthread_wrapper itself. Thus it doesn't make
36 sense to call init_thread or perform BLODA detection. pthread_wrapper
37 eventually calls init_thread by itself. */
38 if ((void *) func != (void *) pthread_wrapper)
39 init_thread (buf, func);
41 DWORD res = func (arg, buf);
42 remove (INFINITE);
43 /* Don't call ExitThread on the main thread since we may have been
44 dynamically loaded. */
45 if ((void *) func != (void *) dll_crt0_1
46 && (void *) func != (void *) dll_dllcrt0_1)
47 ExitThread (res);
50 void
51 _cygtls::init_thread (void *x, DWORD (*func) (void *, void *))
53 if (x)
55 memset (this, 0, sizeof (*this));
56 _REENT_INIT_PTR (&local_clib);
57 stackptr = stack;
58 altstack.ss_flags = SS_DISABLE;
59 if (_REENT_CLEANUP(_GLOBAL_REENT))
60 local_clib.__cleanup = _cygtls::cleanup_early;
63 thread_id = GetCurrentThreadId ();
64 initialized = CYGTLS_INITIALIZED;
65 errno_addr = &(local_clib._errno);
66 locals.cw_timer = NULL;
67 locals.pathbufs.clear ();
69 if ((void *) func == (void *) cygthread::stub
70 || (void *) func == (void *) cygthread::simplestub)
71 return;
73 cygheap->add_tls (this);
76 void
77 _cygtls::fixup_after_fork ()
79 if (sig)
81 pop ();
82 sig = 0;
84 stacklock = spinning = 0;
85 signal_arrived = NULL;
86 locals.select.sockevt = NULL;
87 locals.cw_timer = NULL;
88 locals.pathbufs.clear ();
89 wq.thread_ev = NULL;
92 #define free_local(x) \
93 if (locals.x) \
94 { \
95 free (locals.x); \
96 locals.x = NULL; \
99 void
100 _cygtls::remove (DWORD wait)
102 initialized = 0;
103 if (exit_state >= ES_FINAL)
104 return;
106 debug_printf ("wait %u", wait);
108 HANDLE mutex = cygheap->remove_tls (this);
109 remove_wq (wait);
111 /* FIXME: Need some sort of atthreadexit function to allow things like
112 select to control this themselves. */
114 remove_pending_sigs ();
115 if (signal_arrived)
117 HANDLE h = signal_arrived;
118 signal_arrived = NULL;
119 CloseHandle (h);
122 /* Close handle and free memory used by select. */
123 if (locals.select.sockevt)
125 CloseHandle (locals.select.sockevt);
126 locals.select.sockevt = NULL;
127 free_local (select.ser_num);
128 free_local (select.w4);
130 /* Free memory used by network functions. */
131 free_local (ntoa_buf);
132 free_local (protoent_buf);
133 free_local (servent_buf);
134 free_local (hostent_buf);
135 /* Free temporary TLS path buffers. */
136 locals.pathbufs.destroy ();
137 /* Close timer handle. */
138 if (locals.cw_timer)
139 NtClose (locals.cw_timer);
140 if (mutex)
142 ReleaseMutex (mutex);
143 CloseHandle (mutex);
147 void
148 _cygtls::cleanup_early (struct _reent *)
150 /* Do nothing */
153 void san::leave ()
155 /* Restore tls_pathbuf counters in case of error. */
156 _my_tls.locals.pathbufs._counters = _cnt;
157 _my_tls.andreas = _clemente;