From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ciao.gmane.io (static.214.254.202.116.clients.your-server.de [116.202.254.214]) by sourceware.org (Postfix) with ESMTPS id 851423857801 for ; Fri, 16 Oct 2020 15:55:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 851423857801 Received: from list by ciao.gmane.io with local (Exim 4.92) (envelope-from ) id 1kTS4L-0009qH-TH for newlib@sourceware.org; Fri, 16 Oct 2020 17:55:01 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: newlib@sourceware.org From: Grant Edwards Subject: When/how is global "errno" variable from reent.c to be used? Date: Fri, 16 Oct 2020 15:50:30 -0000 (UTC) Message-ID: User-Agent: slrn/1.0.3 (Linux) X-Spam-Status: No, score=0.9 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, FORGED_GMAIL_RCVD, FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, NML_ADSP_CUSTOM_MED, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=no autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: newlib@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Newlib mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 16 Oct 2020 15:55:05 -0000 [Sorry for yet another errno vs. reentrancy thread.] I've been studying errno, __errno, _impure_ptr et alia, and I think I understand how that's supposed to work. If you don't define _REENT_ONLY, then errno is defined as *(__errno()), where __errno() returns &_REENT->errno, and _REENT is #defined as _impure_ptr, and thread context switches are supposed to set _impure_ptr to point to a per-thread struct _reent. If you define _REENT_ONLY, then there is no 'errno' declared in errno.h, and you've got to mannually pass a struct _reent pointer to the _r versions of various lib[cm] functions. But, in the application source I've been given, the LWIP stack does not include newlib's errno.h. Instead it declares "extern int errno" and then uses errno as usual. That external declaration is satisfied at link time by the global "errno" exported by libc/reent/reent.c. That seems to be non-thread-safe and won't interoperate correctly with newlib's library calls or with application code that includes newlib's errno.h. What is that global errno variable for and how is to be used? Is there a use case for single-threaded applications that I've missed where newlib's library calls will set/check the global 'int errno' variable instead of _impure_ptr->errno? Or is the LWIP configuration broken? Even if there is a single-threaded use case for newlib where everybody uses the global 'int errno' variable, the application in question is multi-threaded, so it's probably still broken. The RTOS in use does not set _impure_ptr on context switches, so I believe that switching to using the newlib errno.h which will define errno as *(__errno()) still won't work as it should. FWIW, the toolchain I'm using is gcc-arm-none-eabi-9-2020-q2-update-x86_64-linux.tar.bz2 from https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-rm/downloads It contains v3.3.0 newlib and newlib-nano built from git://sourceware.org/git/newlib-cygwin.git commit 6d79e0a58866548f435527798fbd4a6849d05bc7 The LWIP application I'm looking at uses newlib-nano. -- Grant