3 This software is a copyrighted work licensed under the terms of the
4 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
8 #define USE_SYS_TYPES_FD_SET
15 #include "create_posix_thread.h"
18 #include "exception.h"
20 /* Two calls to get the stack right... */
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
28 _my_tls
.call2 (func
, arg
, buf
);
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
);
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
)
51 _cygtls::init_thread (void *x
, DWORD (*func
) (void *, void *))
55 memset (this, 0, sizeof (*this));
56 _REENT_INIT_PTR (&local_clib
);
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
)
73 cygheap
->add_tls (this);
77 _cygtls::fixup_after_fork ()
84 stacklock
= spinning
= 0;
85 signal_arrived
= NULL
;
86 locals
.select
.sockevt
= NULL
;
87 locals
.cw_timer
= NULL
;
88 locals
.pathbufs
.clear ();
92 #define free_local(x) \
100 _cygtls::remove (DWORD wait
)
103 if (exit_state
>= ES_FINAL
)
106 debug_printf ("wait %u", wait
);
108 HANDLE mutex
= cygheap
->remove_tls (this);
111 /* FIXME: Need some sort of atthreadexit function to allow things like
112 select to control this themselves. */
114 remove_pending_sigs ();
117 HANDLE h
= signal_arrived
;
118 signal_arrived
= NULL
;
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. */
139 NtClose (locals
.cw_timer
);
142 ReleaseMutex (mutex
);
148 _cygtls::cleanup_early (struct _reent
*)
155 /* Restore tls_pathbuf counters in case of error. */
156 _my_tls
.locals
.pathbufs
._counters
= _cnt
;
157 _my_tls
.andreas
= _clemente
;