This is the mail archive of the newlib@sourceware.org mailing list for the newlib project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Reentrant functions & concurrency


Schwarz, Konrad wrote:
I have a question on the implementation of the functions in
newlib/libc/reent, which are the reentrant versions of Unix system
calls.

Taking writer.c as an example, we find the following function:

_ssize_t
_DEFUN (_write_r, (ptr, fd, buf, cnt),
struct _reent *ptr _AND
int fd _AND _CONST _PTR buf _AND
size_t cnt)
{
_ssize_t ret;


errno = 0;
if ((ret = (_ssize_t)_write (fd, buf, cnt)) == -1 && errno != 0)
ptr->_errno = errno;
return ret;
}


Of particular interest are the three lines starting with "errno = 0;".

What prevents an operating system from performing a context switch
between the call to _write() and the following assignment of errno to
the reentrancy structure?  Such a context switch could cause errno to
change, which would result in an incorrect value of errno to be returned
to the application.

Shouldn't there be a _LOCK_T __errno_lock defined somewhere, a call to
__lock_acquire (__errno_lock) just before the line "errno = 0;" and a
call to __lock_release (__errno_lock) just before the line "return
ret;"?

Regards,

Konrad Schwarz


This is a historical situation. If you look at libc/include/reent.h you will see the following comment which acknowledges that supplying just underscore versions of syscalls won't be truly reentrant at the syscall level:


*********************************************************************
   2) Define namespace clean versions of the system calls by prefixing
      them with '_' (eg: _open, _close, etc.).  Technically, there won't
      be true reentrancy at the syscall level, but the library will be
      namespace clean.
*********************************************************************

Now that there are locks in newlib, adding them as mentioned above is a sound idea. There is one modification required. x86-linux specifies MISSING_SYSCALL_NAMES and the syscalls themselves are reentrant (i.e. they use normal errno, not the external one provided). I am guessing that RTEMS would be in the same boat. It would be useful to be able to turn the errno locking off via a flag (e.g. SYSCALLS_USE_REENTRANT_ERRNO) when it is not needed. The locking macros would be indirectly specified via macros in a local.h header. Would you like me to whip up something or do you already have a trial patch to modify?

-- Jeff J.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]