2004-01-14 23:45:37 +08:00
|
|
|
/* cygtls.cc
|
2003-12-24 00:43:45 +08:00
|
|
|
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
|
|
|
#include "winsup.h"
|
2005-07-03 10:40:30 +08:00
|
|
|
#define USE_SYS_TYPES_FD_SET
|
2003-12-24 00:43:45 +08:00
|
|
|
#include "cygtls.h"
|
|
|
|
#include <syslog.h>
|
2010-12-29 14:33:30 +08:00
|
|
|
#include <stdlib.h>
|
2004-01-14 23:45:37 +08:00
|
|
|
#include "path.h"
|
|
|
|
#include "fhandler.h"
|
|
|
|
#include "dtable.h"
|
2022-08-10 23:40:06 +08:00
|
|
|
#include "create_posix_thread.h"
|
2004-01-14 23:45:37 +08:00
|
|
|
#include "cygheap.h"
|
2004-01-19 13:46:54 +08:00
|
|
|
#include "sigproc.h"
|
2010-02-28 23:54:25 +08:00
|
|
|
#include "exception.h"
|
2004-01-14 23:45:37 +08:00
|
|
|
|
2003-12-24 00:43:45 +08:00
|
|
|
/* Two calls to get the stack right... */
|
|
|
|
void
|
2004-02-12 11:01:58 +08:00
|
|
|
_cygtls::call (DWORD (*func) (void *, void *), void *arg)
|
2003-12-24 00:43:45 +08:00
|
|
|
{
|
2022-01-27 19:39:57 +08:00
|
|
|
char buf[__CYGTLS_PADSIZE__];
|
2010-02-28 23:54:25 +08:00
|
|
|
/* Initialize this thread's ability to respond to things like
|
|
|
|
SIGSEGV or SIGFPE. */
|
|
|
|
exception protect;
|
2006-05-25 10:33:13 +08:00
|
|
|
_my_tls.call2 (func, arg, buf);
|
2003-12-24 00:43:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-02-12 11:01:58 +08:00
|
|
|
_cygtls::call2 (DWORD (*func) (void *, void *), void *arg, void *buf)
|
2003-12-24 00:43:45 +08:00
|
|
|
{
|
2014-11-28 20:10:12 +08:00
|
|
|
/* If func is pthread_wrapper, the final stack hasn't been set up yet.
|
|
|
|
This only happens in pthread_wrapper itself. Thus it doesn't make
|
|
|
|
sense to call init_thread or perform BLODA detection. pthread_wrapper
|
|
|
|
eventually calls init_thread by itself. */
|
|
|
|
if ((void *) func != (void *) pthread_wrapper)
|
2015-12-16 01:28:03 +08:00
|
|
|
init_thread (buf, func);
|
2012-02-27 19:55:27 +08:00
|
|
|
|
2004-01-14 23:45:37 +08:00
|
|
|
DWORD res = func (arg, buf);
|
2006-05-25 10:33:13 +08:00
|
|
|
remove (INFINITE);
|
2006-05-31 10:14:17 +08:00
|
|
|
/* Don't call ExitThread on the main thread since we may have been
|
|
|
|
dynamically loaded. */
|
2006-06-02 08:09:50 +08:00
|
|
|
if ((void *) func != (void *) dll_crt0_1
|
|
|
|
&& (void *) func != (void *) dll_dllcrt0_1)
|
2012-12-22 03:32:43 +08:00
|
|
|
ExitThread (res);
|
2003-12-24 00:43:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-02-12 11:01:58 +08:00
|
|
|
_cygtls::init_thread (void *x, DWORD (*func) (void *, void *))
|
2003-12-24 00:43:45 +08:00
|
|
|
{
|
|
|
|
if (x)
|
|
|
|
{
|
2006-05-16 11:14:24 +08:00
|
|
|
memset (this, 0, sizeof (*this));
|
2011-01-11 16:05:51 +08:00
|
|
|
_REENT_INIT_PTR (&local_clib);
|
2003-12-24 00:43:45 +08:00
|
|
|
stackptr = stack;
|
2015-06-19 21:58:23 +08:00
|
|
|
altstack.ss_flags = SS_DISABLE;
|
2022-02-03 17:18:53 +08:00
|
|
|
if (_REENT_CLEANUP(_GLOBAL_REENT))
|
2022-05-13 19:26:54 +08:00
|
|
|
local_clib.__cleanup = _cygtls::cleanup_early;
|
2003-12-24 00:43:45 +08:00
|
|
|
}
|
|
|
|
|
2006-02-07 02:24:11 +08:00
|
|
|
thread_id = GetCurrentThreadId ();
|
2005-12-06 04:20:18 +08:00
|
|
|
initialized = CYGTLS_INITIALIZED;
|
2003-12-24 00:43:45 +08:00
|
|
|
errno_addr = &(local_clib._errno);
|
2011-08-04 00:40:48 +08:00
|
|
|
locals.cw_timer = NULL;
|
Cygwin: tls_pathbuf: Use Windows heap
Rather than using malloc/free for the buffers, we're now using
HeapAlloc/HeapFree on a HEAP_NO_SERIALIZE heap created for this
thread.
Advantages:
- Less contention. Our malloc/free doesn't scale well in
multithreaded scenarios
- Even faster heap allocation by using a non serialized heap.
- Internal, local, temporary data not cluttering the user heap.
- Internal, local, temporary data not copied over to child process
at fork().
Disadvantage:
- A forked process has to start allocating temporary buffers from
scratch. However, this should be alleviated by the fact that
buffer allocation usually reaches its peak very early in process
runtime, so the longer the proceess runs, the less buffers have
to allocated, and, only few processes don't exec after fork
anyway.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2022-08-23 16:59:18 +08:00
|
|
|
locals.pathbufs.clear ();
|
2004-01-14 23:45:37 +08:00
|
|
|
|
|
|
|
if ((void *) func == (void *) cygthread::stub
|
|
|
|
|| (void *) func == (void *) cygthread::simplestub)
|
|
|
|
return;
|
|
|
|
|
2012-08-10 03:58:53 +08:00
|
|
|
cygheap->add_tls (this);
|
2003-12-24 00:43:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-02-12 11:01:58 +08:00
|
|
|
_cygtls::fixup_after_fork ()
|
|
|
|
{
|
2004-03-16 12:39:38 +08:00
|
|
|
if (sig)
|
|
|
|
{
|
|
|
|
pop ();
|
|
|
|
sig = 0;
|
|
|
|
}
|
2005-10-24 07:47:45 +08:00
|
|
|
stacklock = spinning = 0;
|
2012-07-22 06:58:20 +08:00
|
|
|
signal_arrived = NULL;
|
2009-07-06 23:42:01 +08:00
|
|
|
locals.select.sockevt = NULL;
|
2011-08-04 00:40:48 +08:00
|
|
|
locals.cw_timer = NULL;
|
Cygwin: tls_pathbuf: Use Windows heap
Rather than using malloc/free for the buffers, we're now using
HeapAlloc/HeapFree on a HEAP_NO_SERIALIZE heap created for this
thread.
Advantages:
- Less contention. Our malloc/free doesn't scale well in
multithreaded scenarios
- Even faster heap allocation by using a non serialized heap.
- Internal, local, temporary data not cluttering the user heap.
- Internal, local, temporary data not copied over to child process
at fork().
Disadvantage:
- A forked process has to start allocating temporary buffers from
scratch. However, this should be alleviated by the fact that
buffer allocation usually reaches its peak very early in process
runtime, so the longer the proceess runs, the less buffers have
to allocated, and, only few processes don't exec after fork
anyway.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2022-08-23 16:59:18 +08:00
|
|
|
locals.pathbufs.clear ();
|
2004-03-12 11:09:28 +08:00
|
|
|
wq.thread_ev = NULL;
|
2004-02-12 11:01:58 +08:00
|
|
|
}
|
|
|
|
|
2005-03-17 01:07:32 +08:00
|
|
|
#define free_local(x) \
|
|
|
|
if (locals.x) \
|
|
|
|
{ \
|
|
|
|
free (locals.x); \
|
|
|
|
locals.x = NULL; \
|
|
|
|
}
|
|
|
|
|
2004-02-12 11:01:58 +08:00
|
|
|
void
|
|
|
|
_cygtls::remove (DWORD wait)
|
2003-12-24 00:43:45 +08:00
|
|
|
{
|
2006-03-13 12:26:57 +08:00
|
|
|
initialized = 0;
|
2009-07-06 23:42:01 +08:00
|
|
|
if (exit_state >= ES_FINAL)
|
2005-03-03 08:36:49 +08:00
|
|
|
return;
|
2006-01-06 00:26:22 +08:00
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
debug_printf ("wait %u", wait);
|
2011-04-21 16:10:28 +08:00
|
|
|
|
2014-11-29 04:46:13 +08:00
|
|
|
HANDLE mutex = cygheap->remove_tls (this);
|
2014-11-28 20:10:12 +08:00
|
|
|
remove_wq (wait);
|
|
|
|
|
2011-04-21 16:10:28 +08:00
|
|
|
/* FIXME: Need some sort of atthreadexit function to allow things like
|
|
|
|
select to control this themselves. */
|
|
|
|
|
2015-10-23 20:30:40 +08:00
|
|
|
remove_pending_sigs ();
|
2012-07-22 06:58:20 +08:00
|
|
|
if (signal_arrived)
|
|
|
|
{
|
|
|
|
HANDLE h = signal_arrived;
|
|
|
|
signal_arrived = NULL;
|
|
|
|
CloseHandle (h);
|
|
|
|
}
|
|
|
|
|
2011-04-21 16:10:28 +08:00
|
|
|
/* Close handle and free memory used by select. */
|
2011-04-18 23:51:54 +08:00
|
|
|
if (locals.select.sockevt)
|
2005-04-06 01:13:35 +08:00
|
|
|
{
|
2011-04-21 16:10:28 +08:00
|
|
|
CloseHandle (locals.select.sockevt);
|
|
|
|
locals.select.sockevt = NULL;
|
|
|
|
free_local (select.ser_num);
|
|
|
|
free_local (select.w4);
|
2005-04-06 01:13:35 +08:00
|
|
|
}
|
2011-04-21 16:10:28 +08:00
|
|
|
/* Free memory used by network functions. */
|
|
|
|
free_local (ntoa_buf);
|
|
|
|
free_local (protoent_buf);
|
|
|
|
free_local (servent_buf);
|
|
|
|
free_local (hostent_buf);
|
2008-03-07 19:24:51 +08:00
|
|
|
/* Free temporary TLS path buffers. */
|
2014-08-25 22:53:49 +08:00
|
|
|
locals.pathbufs.destroy ();
|
2013-07-19 19:54:51 +08:00
|
|
|
/* Close timer handle. */
|
|
|
|
if (locals.cw_timer)
|
|
|
|
NtClose (locals.cw_timer);
|
2014-11-29 04:46:13 +08:00
|
|
|
if (mutex)
|
|
|
|
{
|
|
|
|
ReleaseMutex (mutex);
|
|
|
|
CloseHandle (mutex);
|
|
|
|
}
|
2003-12-24 00:43:45 +08:00
|
|
|
}
|
2014-08-26 03:47:44 +08:00
|
|
|
|
2022-02-22 18:18:38 +08:00
|
|
|
void
|
|
|
|
_cygtls::cleanup_early (struct _reent *)
|
|
|
|
{
|
|
|
|
/* Do nothing */
|
|
|
|
}
|
|
|
|
|
2014-08-26 03:47:44 +08:00
|
|
|
void san::leave ()
|
|
|
|
{
|
|
|
|
/* Restore tls_pathbuf counters in case of error. */
|
|
|
|
_my_tls.locals.pathbufs._counters = _cnt;
|
2015-07-08 02:45:06 +08:00
|
|
|
_my_tls.andreas = _clemente;
|
2014-08-26 03:47:44 +08:00
|
|
|
}
|