From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2155) id AB3DF385841B; Tue, 22 Feb 2022 11:39:07 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AB3DF385841B Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Corinna Vinschen To: cygwin-cvs@sourceware.org, newlib-cvs@sourceware.org Subject: [newlib-cygwin] Make __sdidinit unused X-Act-Checkin: newlib-cygwin X-Git-Author: Matt Joyce X-Git-Refname: refs/heads/master X-Git-Oldrev: 054b00d96a81ca1169a1dd2d63d79b7ac03919ac X-Git-Newrev: 44b60f0c4ba597c55dcac4f2d3119a3055c80ba1 Message-Id: <20220222113907.AB3DF385841B@sourceware.org> Date: Tue, 22 Feb 2022 11:39:07 +0000 (GMT) X-BeenThere: newlib-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Newlib GIT logs List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 22 Feb 2022 11:39:07 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dnewlib-cygwin.git;h=3D44b60f0c4ba= 597c55dcac4f2d3119a3055c80ba1 commit 44b60f0c4ba597c55dcac4f2d3119a3055c80ba1 Author: Matt Joyce Date: Tue Feb 22 11:18:38 2022 +0100 Make __sdidinit unused =20 Remove dependency on __sdidinit member of struct _reent to check object initialization. Like __sdidinit, the __cleanup member of struct _reent is initialized in the __sinit() function. Checking initialization against __cleanup serves the same purpose and will reduce overhead in the __sfp() function in a follow up patch. Diff: --- libgloss/aarch64/syscalls.c | 2 +- libgloss/arm/syscalls.c | 2 +- newlib/libc/include/sys/reent.h | 10 +++++++--- newlib/libc/machine/spu/c99ppe.h | 2 +- newlib/libc/machine/spu/stdio.c | 1 - newlib/libc/reent/reent.c | 2 +- newlib/libc/stdio/findfp.c | 11 ++--------- newlib/libc/stdio/local.h | 6 +++--- newlib/libc/stdio/setvbuf.c | 2 +- newlib/libc/sys/arm/syscalls.c | 2 +- winsup/cygwin/cygtls.cc | 10 ++++++++-- winsup/cygwin/cygtls.h | 1 + winsup/cygwin/dcrt0.cc | 10 ++++------ winsup/cygwin/thread.cc | 4 ++-- 14 files changed, 33 insertions(+), 32 deletions(-) diff --git a/libgloss/aarch64/syscalls.c b/libgloss/aarch64/syscalls.c index 7343cc61f..5b4071893 100644 --- a/libgloss/aarch64/syscalls.c +++ b/libgloss/aarch64/syscalls.c @@ -124,7 +124,7 @@ extern void __sinit (struct _reent *); #define CHECK_INIT(ptr) \ do \ { \ - if ((ptr) && !(ptr)->__sdidinit) \ + if ((ptr) && !(ptr)->__cleanup) \ __sinit (ptr); \ } \ while (0) diff --git a/libgloss/arm/syscalls.c b/libgloss/arm/syscalls.c index fc394f94b..710a741ee 100644 --- a/libgloss/arm/syscalls.c +++ b/libgloss/arm/syscalls.c @@ -89,7 +89,7 @@ extern void __sinit (struct _reent *); #define CHECK_INIT(ptr) \ do \ { \ - if ((ptr) && !(ptr)->__sdidinit) \ + if ((ptr) && !(ptr)->__cleanup) \ __sinit (ptr); \ } \ while (0) diff --git a/newlib/libc/include/sys/reent.h b/newlib/libc/include/sys/reen= t.h index e4e36170e..f8ecf8586 100644 --- a/newlib/libc/include/sys/reent.h +++ b/newlib/libc/include/sys/reent.h @@ -175,7 +175,7 @@ extern void __sinit (struct _reent *); # define _REENT_SMALL_CHECK_INIT(ptr) \ do \ { \ - if ((ptr) && !(ptr)->__sdidinit) \ + if ((ptr) && !(ptr)->__cleanup) \ __sinit (ptr); \ } \ while (0) @@ -389,7 +389,9 @@ struct _reent =20 char *_emergency; =20 - int __sdidinit; /* 1 means stdio has been init'd */ + /* No longer used, but member retained for binary compatibility. + Now, the __cleanup member is used to check initialization. */ + int _unused_sdidinit; =20 int _unspecified_locale_info; /* unused, reserved for locale stuff */ struct __locale_t *_locale;/* per-thread locale */ @@ -626,7 +628,9 @@ struct _reent int _unspecified_locale_info; /* unused, reserved for locale stuff */ struct __locale_t *_locale;/* per-thread locale */ =20 - int __sdidinit; /* 1 means stdio has been init'd */ + /* No longer used, but member retained for binary compatibility. + Now, the __cleanup member is used to check initialization. */ + int _unused_sdidinit; =20 void (*__cleanup) (struct _reent *); =20 diff --git a/newlib/libc/machine/spu/c99ppe.h b/newlib/libc/machine/spu/c99= ppe.h index ccd2d257c..12a2cb3fb 100644 --- a/newlib/libc/machine/spu/c99ppe.h +++ b/newlib/libc/machine/spu/c99ppe.h @@ -104,7 +104,7 @@ FILE *__sfp (struct _reent *); #define __sfp_free(fp) ( (fp)->_fp =3D 0 ) =20 #define CHECK_INIT(ptr) \ - do { if ((ptr) && !(ptr)->__sdidinit) __sinit (ptr); } while (0) + do { if ((ptr) && !(ptr)->__cleanup) __sinit (ptr); } while (0) #define CHECK_STD_INIT(ptr) /* currently, do nothing */ #define CHECK_STR_INIT(ptr) /* currently, do nothing */ #endif /* __ASSEMBLER__ */ diff --git a/newlib/libc/machine/spu/stdio.c b/newlib/libc/machine/spu/stdi= o.c index 2a308b8aa..87e4c406d 100644 --- a/newlib/libc/machine/spu/stdio.c +++ b/newlib/libc/machine/spu/stdio.c @@ -66,7 +66,6 @@ void __sinit (struct _reent *s) { s->__cleanup =3D __cleanup; - s->__sdidinit =3D 1; =20 s->_stdin =3D &s->__sf[0]; s->_stdin->_fp =3D SPE_STDIN; diff --git a/newlib/libc/reent/reent.c b/newlib/libc/reent/reent.c index 7c57e2019..5a04fcf62 100644 --- a/newlib/libc/reent/reent.c +++ b/newlib/libc/reent/reent.c @@ -118,7 +118,7 @@ _reclaim_reent (struct _reent *ptr) if (ptr->_sig_func) _free_r (ptr, ptr->_sig_func);*/ =20 - if (ptr->__sdidinit) + if (ptr->__cleanup) { /* cleanup won't reclaim memory 'coz usually it's run before the program exits, and who wants to wait for that? */ diff --git a/newlib/libc/stdio/findfp.c b/newlib/libc/stdio/findfp.c index 896be99e9..9877c1f2c 100644 --- a/newlib/libc/stdio/findfp.c +++ b/newlib/libc/stdio/findfp.c @@ -152,7 +152,7 @@ __sfp (struct _reent *d) =20 _newlib_sfp_lock_start (); =20 - if (!_GLOBAL_REENT->__sdidinit) + if (_GLOBAL_REENT->__cleanup =3D=3D NULL) __sinit (_GLOBAL_REENT); for (g =3D &_GLOBAL_REENT->__sglue;; g =3D g->_next) { @@ -246,7 +246,7 @@ __sinit (struct _reent *s) { __sinit_lock_acquire (); =20 - if (s->__sdidinit) + if (s->__cleanup) { __sinit_lock_release (); return; @@ -264,11 +264,6 @@ __sinit (struct _reent *s) #else s->__sglue._niobs =3D 0; s->__sglue._iobs =3D NULL; - /* Avoid infinite recursion when calling __sfp for _GLOBAL_REENT. The - problem is that __sfp checks for _GLOBAL_REENT->__sdidinit and calls - __sinit if it's 0. */ - if (s =3D=3D _GLOBAL_REENT) - s->__sdidinit =3D 1; # ifndef _REENT_GLOBAL_STDIO_STREAMS s->_stdin =3D __sfp(s); s->_stdout =3D __sfp(s); @@ -294,8 +289,6 @@ __sinit (struct _reent *s) stderr_init (s->_stderr); #endif /* _REENT_GLOBAL_STDIO_STREAMS */ =20 - s->__sdidinit =3D 1; - __sinit_lock_release (); } =20 diff --git a/newlib/libc/stdio/local.h b/newlib/libc/stdio/local.h index 84ff40b49..f63c5fb10 100644 --- a/newlib/libc/stdio/local.h +++ b/newlib/libc/stdio/local.h @@ -202,7 +202,7 @@ extern _READ_WRITE_RETURN_TYPE __swrite64 (struct _reen= t *, void *, do \ { \ struct _reent *_check_init_ptr =3D (ptr); \ - if ((_check_init_ptr) && !(_check_init_ptr)->__sdidinit) \ + if ((_check_init_ptr) && !(_check_init_ptr)->__cleanup) \ __sinit (_check_init_ptr); \ if ((fp) =3D=3D (FILE *)&__sf_fake_stdin) \ (fp) =3D _stdin_r(_check_init_ptr); \ @@ -217,7 +217,7 @@ extern _READ_WRITE_RETURN_TYPE __swrite64 (struct _reen= t *, void *, do \ { \ struct _reent *_check_init_ptr =3D (ptr); \ - if ((_check_init_ptr) && !(_check_init_ptr)->__sdidinit) \ + if ((_check_init_ptr) && !(_check_init_ptr)->__cleanup) \ __sinit (_check_init_ptr); \ } \ while (0) @@ -227,7 +227,7 @@ extern _READ_WRITE_RETURN_TYPE __swrite64 (struct _reen= t *, void *, do \ { \ struct _reent *_check_init_ptr =3D (ptr); \ - if ((_check_init_ptr) && !(_check_init_ptr)->__sdidinit) \ + if ((_check_init_ptr) && !(_check_init_ptr)->__cleanup) \ __sinit (_check_init_ptr); \ } \ while (0) diff --git a/newlib/libc/stdio/setvbuf.c b/newlib/libc/stdio/setvbuf.c index f9eeefd1c..46c58a7b8 100644 --- a/newlib/libc/stdio/setvbuf.c +++ b/newlib/libc/stdio/setvbuf.c @@ -174,7 +174,7 @@ nbf: * We're committed to buffering from here, so make sure we've * registered to flush buffers on exit. */ - if (!reent->__sdidinit) + if (!reent->__cleanup) __sinit(reent); =20 #ifdef _FSEEK_OPTIMIZATION diff --git a/newlib/libc/sys/arm/syscalls.c b/newlib/libc/sys/arm/syscalls.c index 1f7222980..4b9be701a 100644 --- a/newlib/libc/sys/arm/syscalls.c +++ b/newlib/libc/sys/arm/syscalls.c @@ -60,7 +60,7 @@ extern void __sinit (struct _reent *); #define CHECK_INIT(ptr) \ do \ { \ - if ((ptr) && !(ptr)->__sdidinit) \ + if ((ptr) && !(ptr)->__cleanup) \ __sinit (ptr); \ } \ while (0) diff --git a/winsup/cygwin/cygtls.cc b/winsup/cygwin/cygtls.cc index 1a2213d1f..c8352adf9 100644 --- a/winsup/cygwin/cygtls.cc +++ b/winsup/cygwin/cygtls.cc @@ -60,8 +60,8 @@ _cygtls::init_thread (void *x, DWORD (*func) (void *, voi= d *)) local_clib._stdin =3D _GLOBAL_REENT->_stdin; local_clib._stdout =3D _GLOBAL_REENT->_stdout; local_clib._stderr =3D _GLOBAL_REENT->_stderr; - local_clib.__sdidinit =3D _GLOBAL_REENT->__sdidinit ? -1 : 0; - local_clib.__cleanup =3D _GLOBAL_REENT->__cleanup; + if (_GLOBAL_REENT->__cleanup) + local_clib.__cleanup =3D _cygtls::cleanup_early; local_clib.__sglue._niobs =3D 3; local_clib.__sglue._iobs =3D &_GLOBAL_REENT->__sf[0]; } @@ -149,6 +149,12 @@ _cygtls::remove (DWORD wait) } } =20 +void +_cygtls::cleanup_early (struct _reent *) +{ + /* Do nothing */ +} + #ifdef __x86_64__ void san::leave () { diff --git a/winsup/cygwin/cygtls.h b/winsup/cygwin/cygtls.h index a2e3676fc..c2c4141bf 100644 --- a/winsup/cygwin/cygtls.h +++ b/winsup/cygwin/cygtls.h @@ -272,6 +272,7 @@ public: will_wait_for_signal =3D false; } void handle_SIGCONT (); + static void cleanup_early(struct _reent *); private: void __reg3 call2 (DWORD (*) (void *, void *), void *, void *); void remove_pending_sigs (); diff --git a/winsup/cygwin/dcrt0.cc b/winsup/cygwin/dcrt0.cc index f3d09c169..e757c47ec 100644 --- a/winsup/cygwin/dcrt0.cc +++ b/winsup/cygwin/dcrt0.cc @@ -824,15 +824,13 @@ main_thread_sinit () As soon as the main thread calls a stdio function, this would be rectified. But if another thread calls a stdio function on stdin/out/err before the main thread does, all the required - initialization of stdin/out/err will be done, but _REENT->__sdidinit - is *still* 0. This in turn will result in a call to __sinit in the + initialization of stdin/out/err will be done, but _REENT->__cleanup + is *still* NULL. This in turn will result in a call to __sinit in the wrong spot. The input or output buffer will be NULLed and nothing is read or written in the first stdio function call in the main thread. =20 - To fix this issue we have to copy over the relevant part of _GLOBAL_R= EENT - to _REENT here again. */ - _REENT->__sdidinit =3D -1; - _REENT->__cleanup =3D _GLOBAL_REENT->__cleanup; + To fix this issue we set __cleanup to _cygtls::cleanup_early here. */ + _REENT->__cleanup =3D _cygtls::cleanup_early; } =20 /* Take over from libc's crt0.o and start the application. Note the diff --git a/winsup/cygwin/thread.cc b/winsup/cygwin/thread.cc index fcfd75c79..b7da4d0c7 100644 --- a/winsup/cygwin/thread.cc +++ b/winsup/cygwin/thread.cc @@ -564,8 +564,8 @@ pthread::exit (void *value_ptr) mutex.unlock (); } =20 - if (_my_tls.local_clib.__sdidinit < 0) - _my_tls.local_clib.__sdidinit =3D 0; + if (_my_tls.local_clib.__cleanup =3D=3D _cygtls::cleanup_early) + _my_tls.local_clib.__cleanup =3D NULL; _reclaim_reent (_REENT); =20 if (InterlockedDecrement (&MT_INTERFACE->threadcount) =3D=3D 0)