Usenet comp.unix.osf.misc (1992-2004)
 help / color / mirror / Atom feed
* More on threads and errno
@ 1994-11-17 18:57 David Wright
  1994-11-17 20:17 ` Alan Lehotsky
  0 siblings, 1 reply; 2+ messages in thread
From: David Wright @ 1994-11-17 18:57 UTC (permalink / raw)



My thanks to all who have responded to my questions about handing
errno in a threaded environment.  I figured I'd just respond to you
all at one time, and add some extra information describing what we
know and don't know so far, plus a bit more about the problem.  Sorry
for the length of this; I've tried to keep it as short as possible.
This will be posted to comp.unix.osf.misc and to dce-talk.

As a quick summary, here are the issues:

1)  errno is a global variable, so it can't be shared properly by
    multiple threads.

2)  new libraries and code can be written to use a per-thread errno,

3)  but that doesn't allow mixing of new and old libraries
    (communicating errno values), particularly 3rd-party libraries for
    which we may not have the source,

4)  and there is still the question of how to invoke non-thread-safe
    code in a multi-threaded environment.

I don't propose to do a lot to address this last point.  Unless some
sort of standard locking emerges, about all you can do is acquire a
single global lock before invoking a non-thread-safe routine.  The
pthread_lock_global_np() function might be useful here.

The rest of this post will be devoted to considering what to do about
errno.

I realize that it's sometimes possible to use the global errno, and
copy it into a per-thread storage location upon context switch.
However, this is only practical if you are using user threads, not
kernel threads.  We're using kernel threads, and we're also using
multiprocessors, so that solution won't work for us.

An email respondent from Transarc told me that SunSoft had changed all
their libraries to be thread-safe.  Well, that's one possibility.  But
it doesn't address the problem of 3rd-party libraries.  It's also a
lot of work, and we are not a large shop here.

lehotsky@max.tiac.net (Alan Lehotsky) wrote:

	Don't mix thread-safe and non-threadsafe code.  You will need
	to rebuild libm and libX using appropriate header files so
	that you get the special definition of errno.

I'd need to redo every single system library this way.  Also, the code
should presumably check whether it's multithreaded -- if it never has
been, it should just use the global variable (much cheaper).  And it
still doesn't address how to use 3rd-party libraries.

	Trying to maintain locks around this data will be a thankless
	chore.

Interestingly, the OSF/1 solution was to modify libc (creating libc_r)
to do exactly this; for example, the stdio functions had locking
introduced into them.

	And while you're at it, don't forget that there's a LOT of
	libc functions that aren't thread-safe.  In fact, just about
	everything that returns a pointer is suspect [excluding the
	str functions...]

Believe me, I *know* there are a lot of such functions.  I've done
considerable analysis of the code to see how many there are and what
their problems are.

Andrew Hastings of Transarc was (unfairly, IMO) drawn into a bit of a
flamefest; his follow-up is what I'll address here.  I've already
dealt with his points on user threads; on kernel threads, he said:

	If you have kernel threads, your kernel should arrange to 
	map errno separately for each thread.

Maybe.  You'd need to define errno as some special address, presumably
its own page, and mark that page absent, generating a SIGSEGV when
referencing it.  The SIGSEGV handler could recognize the situation and
access the per-thread errno.  This would work but it's expensive.  Not
all OS's support this, either.

Proposed solution:

I'm presuming that in thread-safe code, references to errno are
replaced (either explicitly or by macro) with references to a function
that returns a pointer to the desired errno location.  Usually, this
is the per-thread errno.

Now, let me tell you what we're proposing to do.  We'll add a pair of
wrapper functions, say ENTER() and EXIT() (not their real names), that
are invoked as follows:

     ENTER();
     non_thread_safe_function();
     EXIT();

The ENTER() function will cause the issuing thread's errno-location
function to reference the global errno, rather the per-thread errno.
The EXIT() function reverses this behavior.

Locking is still up to the application, and there are a few nuances,
like ENTER and EXIT need to nest (i.e. if you issue 

     ENTER(); ENTER(); ... ; EXIT(); EXIT();

the behavior doesn't revert until the second EXIT() function is
complete.  Also, you need to save the nesting level as part of setjmp
processing and restore it on longjmp (a la signal masks).

So, there's the idea.  You may now take potshots at it.

  -- David Wright, Hitachi Computer Products (America), Inc.  Waltham, MA
     wright@hi.com  ::  DCE Project Leader


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: More on threads and errno
  1994-11-17 18:57 More on threads and errno David Wright
@ 1994-11-17 20:17 ` Alan Lehotsky
  0 siblings, 0 replies; 2+ messages in thread
From: Alan Lehotsky @ 1994-11-17 20:17 UTC (permalink / raw)


In article <3ag928$47n@clam.hi.com>, David Wright <wright@clam.hi.com> wrote:
>
>
>	Trying to maintain locks around this data will be a thankless
>	chore.
>
>Interestingly, the OSF/1 solution was to modify libc (creating libc_r)
>to do exactly this; for example, the stdio functions had locking
>introduced into them.


	I knew that.  I was trying to say that managing the locks
	_outside_ of the library will be unacceptable.

>
>Now, let me tell you what we're proposing to do.  We'll add a pair of
>wrapper functions, say ENTER() and EXIT() (not their real names), that
>are invoked as follows:
>
>     ENTER();
>     non_thread_safe_function();
>     EXIT();
>
...
>  -- David Wright, Hitachi Computer Products (America), Inc.  Waltham, MA
>     wright@hi.com  ::  DCE Project Leader


Here's a shot across the bow.... :-)

	Other DCE vendors have fixed their libraries to be
	thread-safe.  Thus, DCE application code is

		- widely portable to other DCE implementations.

		- integratable into an existing application without
		  having to wrap each non-thread-safe function.

	Now, as a technique for dealing with 3rd party libraries where you
	can't recompile, this is probably okay.

That didn't slow you down?

	How about the fact that the existing __errno() function is
	invariant, it merely returns a lvalue for a 'slot' in the
	per-thread data area.  Your suggestion has to double indirect
	(e.g., the ENTER() and EXIT() guards have to change what
	__errno() returns.)

Finally, for the errno problem, since only system calls should be
setting errno, there shouldn't be any third party libraries that
aren't obeying the rules of accessing via the errno selector function.

-- Al Lehotsky
======================================================================
			Quality Software Management
			      8 Tucker Ave.
			   Lexington, MA 02173

   lehotsky@tiac.net	Voice: (617)862-5418	Fax: (617)674-1096

	   Software Process Improvement and Management Consulting





^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~1994-11-17 20:17 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1994-11-17 18:57 More on threads and errno David Wright
1994-11-17 20:17 ` Alan Lehotsky

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox