Usenet comp.unix.osf.misc (1992-2004)
 help / color / mirror / Atom feed
* errno and DCE and threads
@ 1994-11-07 18:45 David Wright
  1994-11-07 20:14 ` Alan Lehotsky
                   ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: David Wright @ 1994-11-07 18:45 UTC (permalink / raw)


We're running into some real problems with productizing DCE when it
comes to dealing with other, non-DCE, libraries.  (This is on an OSF/1
base, BTW.)

The single biggest problem is errno handling.  Errno is a single,
global variable in standard Unix.  (Granted, this was a really bad
design decision, but we're stuck with it.)  Since DCE applications are
multi-threaded, they need to be able to keep errno on a per-thread
basis (so they don't trip over each other's values).

Within DCE itself, this is not a big deal.  You can redefine errno to
be a function that points to a per-thread data item.  The problem
arises when you try to link against other libraries (libX, let's say,
or libm) that still use the global variable.

Actually, there are two problems.  One is that multiple threads might
try to call a non-thread-safe function that sets errno.  This you can
presumably handle within your application by doing your own locking.
The other problem lies in communicating errno values between threaded
and non-threaded code.

We've come up with some possible solutions for this problem, but
before we do anything rash, we wondered if anyone out there could tell
us what other vendors have done.

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


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

* Re: errno and DCE and threads
  1994-11-07 18:45 errno and DCE and threads David Wright
@ 1994-11-07 20:14 ` Alan Lehotsky
  1994-11-08 15:42 ` Andrew Hastings
  1994-11-09 13:27 ` Rich Salz
  2 siblings, 0 replies; 12+ messages in thread
From: Alan Lehotsky @ 1994-11-07 20:14 UTC (permalink / raw)


In article <39lsjk$lrf@clam.hi.com>, David Wright <wright@clam.hi.com> wrote:
>We're running into some real problems with productizing DCE when it
>comes to dealing with other, non-DCE, libraries.  (This is on an OSF/1
>base, BTW.)
>
>The single biggest problem is errno handling.  Errno is a single,
>global variable in standard Unix.  (Granted, this was a really bad
..
>Within DCE itself, this is not a big deal.  You can redefine errno to
>be a function that points to a per-thread data item.  The problem
>arises when you try to link against other libraries (libX, let's say,
>or libm) that still use the global variable.
>
..
>  -- David Wright, Hitachi Computer Products (America), Inc.  Waltham, MA
>     wright@hi.com  ::  DCE Project Leader


	Ah, this is easy.  You remember the joke:

		Man:  	Doctor, it hurts when I do this...[wiggles arm]
		Doctor:	Don't do that!

	Well, the solution is the same for DCE and errno (also DCE and
	any other static or global data maintained outside of the DCE
	libraries).

	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.

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

	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...]

-- Al Lehotsky (former) Director Cross-Product Integration, OSF
	presently "consultant to the stars..."

======================================================================
			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] 12+ messages in thread

* Re: errno and DCE and threads
  1994-11-07 18:45 errno and DCE and threads David Wright
  1994-11-07 20:14 ` Alan Lehotsky
@ 1994-11-08 15:42 ` Andrew Hastings
  1994-11-08 21:35   ` Simon Patience
  1994-11-09 13:27 ` Rich Salz
  2 siblings, 1 reply; 12+ messages in thread
From: Andrew Hastings @ 1994-11-08 15:42 UTC (permalink / raw)


In article <39lsjk$lrf@clam.hi.com> wright@clam.hi.com (David Wright) writes:
>The single biggest problem is errno handling.  Errno is a single,
>global variable in standard Unix.  (Granted, this was a really bad
>design decision, but we're stuck with it.)  Since DCE applications are
>multi-threaded, they need to be able to keep errno on a per-thread
>basis (so they don't trip over each other's values).

Since you're productizing DCE, you can change the DCE thread
implementation.  Record errno along with the hardware registers in your
thread context.  When you switch threads, switch errno values.

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

-Andrew Hastings
 Encina Development
 Transarc


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

* Re: errno and DCE and threads
  1994-11-08 15:42 ` Andrew Hastings
@ 1994-11-08 21:35   ` Simon Patience
  1994-11-09 20:05     ` Andrew Hastings
  0 siblings, 1 reply; 12+ messages in thread
From: Simon Patience @ 1994-11-08 21:35 UTC (permalink / raw)


In article <39o694$6a0@cantaloupe.srv.cs.cmu.edu>, Andrew Hastings <abh+@transarc.com> writes:
|> In article <39lsjk$lrf@clam.hi.com> wright@clam.hi.com (David Wright) writes:
|> >The single biggest problem is errno handling.  Errno is a single,
|> >global variable in standard Unix.  (Granted, this was a really bad
|> >design decision, but we're stuck with it.)  Since DCE applications are
|> >multi-threaded, they need to be able to keep errno on a per-thread
|> >basis (so they don't trip over each other's values).
|> 
|> Since you're productizing DCE, you can change the DCE thread
|> implementation.  Record errno along with the hardware registers in your
|> thread context.  When you switch threads, switch errno values.

Why you would want to waste a valuable register on errno is beyond me, keeping
the thread id in a register would be sensible as you access it frequently but
errno? Errno is best kept in thread private data. By thread private I don't mean
inaccessible to other threads, just not used by them.

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

This is daft too. This means address space manipulations switching between
threads. On some architectures this can be very expensive. So much for threads
being light weight.

Simon.

-- 
  Simon Patience				Phone: (415) 390-4644
  Silicon Graphics, Inc				FAX:   (415) 390-3542
  2011 N. Shoreline Boulevard				Email: sp@engr.sgi.com
  Mountain View, CA 94043


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

* Re: errno and DCE and threads
  1994-11-07 18:45 errno and DCE and threads David Wright
  1994-11-07 20:14 ` Alan Lehotsky
  1994-11-08 15:42 ` Andrew Hastings
@ 1994-11-09 13:27 ` Rich Salz
  1994-11-15 10:14   ` Casper H.S. Dik
  2 siblings, 1 reply; 12+ messages in thread
From: Rich Salz @ 1994-11-09 13:27 UTC (permalink / raw)


In article <39lsjk$lrf@clam.hi.com> wright@clam.hi.com (David Wright) writes:
>The single biggest problem is errno handling.  Errno is a single,
>global variable in standard Unix.  (Granted, this was a really bad
>design decision, but we're stuck with it.)  Since DCE applications are
>multi-threaded, they need to be able to keep errno on a per-thread
>basis (so they don't trip over each other's values).

If you read the definition of errno in the ANSI standard, you'll see that
errno can be a #define that evaluates to an lvalue.  This means you
can do things like this:
    /* Returns the address of the per-thread "errno" field in the current
     * thread.  Assumes each thread has an internal state struct. */
    extern int *pthread_errno_np(void);

    #define errno  (*pthread_errno_np())

In practice, however, it's probably easier to just store errno in the internal
per-thread state block and copy it in/out as part of your thread task
context switch.

For kernel threads, the kernel should just be doing the right thing.
	/r$


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

* Re: errno and DCE and threads
  1994-11-08 21:35   ` Simon Patience
@ 1994-11-09 20:05     ` Andrew Hastings
  0 siblings, 0 replies; 12+ messages in thread
From: Andrew Hastings @ 1994-11-09 20:05 UTC (permalink / raw)


In article <CyyxAo.4n@odin.corp.sgi.com> sp@albion.engr.sgi.com (Simon Patience) writes:
>In article <39o694$6a0@cantaloupe.srv.cs.cmu.edu>, Andrew Hastings <abh+@transarc.com> writes:
>|> Since you're productizing DCE, you can change the DCE thread
>|> implementation.  Record errno along with the hardware registers in your
>|> thread context.  When you switch threads, switch errno values.
>
>Why you would want to waste a valuable register on errno is beyond me, keeping
>the thread id in a register would be sensible as you access it frequently but
>errno? Errno is best kept in thread private data. By thread private I don't mean
>inaccessible to other threads, just not used by them.

Who said anything about putting errno in a register?

The original poster wanted to know how to maintain a thread-specific
errno for thread-naive libraries.

Permit me to spell out my solution in more detail:
1.  Add a field in your thread context block (tcb) for errno.
    (The tcb already has space for all the hardware registers.)
2.  When you switch from thread A to thread B.
    a. Save the registers in thread A's tcb.
    b. Save errno in thread A's tcb.
    c. Restore errno from thread B's tcb.
    d. Restore the registers from thread B's tcb.


>|> If you have kernel threads, your kernel should arrange to map errno
>|> separately for each thread.
>
>This is daft too. This means address space manipulations switching between
>threads. On some architectures this can be very expensive. So much for threads
>being light weight.

Kernel thread system designers have, of course, recognized that context
switching of kernel threads, while cheaper than process context
switching, may still be somewhat expensive.

So many systems with kernel threads (Solaris and UNICOS come to mind)
actually implement a hybrid approach, scheduling a large number of
user-level threads across a smaller number of kernel threads.

Thank you for your kind remarks.

-Andrew Hastings
 Encina Development
 Transarc


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

* Re: errno and DCE and threads
  1994-11-09 13:27 ` Rich Salz
@ 1994-11-15 10:14   ` Casper H.S. Dik
  1994-11-15 16:38     ` Rich Salz
  1994-11-15 22:09     ` Spike White
  0 siblings, 2 replies; 12+ messages in thread
From: Casper H.S. Dik @ 1994-11-15 10:14 UTC (permalink / raw)


rsalz@osf.org (Rich Salz) writes:

>In practice, however, it's probably easier to just store errno in the internal
>per-thread state block and copy it in/out as part of your thread task
>context switch.


>For kernel threads, the kernel should just be doing the right thing.

How can a the kernel do this?  If errno is a statica variable,
the kernel cannot move it about in a transparent way.
Multpiple threads on multiple processors will want to read
the variable so without #define errno (*(___errno())) I don't
see a way of doing it.

Casper


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

* Re: errno and DCE and threads
  1994-11-15 10:14   ` Casper H.S. Dik
@ 1994-11-15 16:38     ` Rich Salz
  1994-11-15 21:19       ` Casper H.S. Dik
  1994-11-15 21:40       ` Alan Lehotsky
  1994-11-15 22:09     ` Spike White
  1 sibling, 2 replies; 12+ messages in thread
From: Rich Salz @ 1994-11-15 16:38 UTC (permalink / raw)


In <3aa1mo$6b8@mail.fwi.uva.nl> casper@fwi.uva.nl (Casper H.S. Dik) writes:
>>For kernel threads, the kernel should just be doing the right thing.
>
>How can a the kernel do this?  If errno is a statica variable,
>the kernel cannot move it about in a transparent way.

Sure it can.  Each thread has its own errno, and the kernel need only
switch those values into and out of the processes global errno variable
each time it context switches a thread.

A simple matter of programming.
	/r$


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

* Re: errno and DCE and threads
  1994-11-15 16:38     ` Rich Salz
@ 1994-11-15 21:19       ` Casper H.S. Dik
  1994-11-15 21:40       ` Alan Lehotsky
  1 sibling, 0 replies; 12+ messages in thread
From: Casper H.S. Dik @ 1994-11-15 21:19 UTC (permalink / raw)


rsalz@osf.org (Rich Salz) writes:

>Sure it can.  Each thread has its own errno, and the kernel need only
>switch those values into and out of the processes global errno variable
>each time it context switches a thread.

On single processor machines, yes.  But on multiprocessor machines?

(There is one global context and > one active contexts)

Casper


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

* Re: errno and DCE and threads
  1994-11-15 16:38     ` Rich Salz
  1994-11-15 21:19       ` Casper H.S. Dik
@ 1994-11-15 21:40       ` Alan Lehotsky
  1 sibling, 0 replies; 12+ messages in thread
From: Alan Lehotsky @ 1994-11-15 21:40 UTC (permalink / raw)


In article <3aao5n$94n@paperboy.osf.org>, Rich Salz <rsalz@osf.org> wrote:
>In <3aa1mo$6b8@mail.fwi.uva.nl> casper@fwi.uva.nl (Casper H.S. Dik) writes:
>>>For kernel threads, the kernel should just be doing the right thing.
>>
>>How can a the kernel do this?  If errno is a statica variable,
>>the kernel cannot move it about in a transparent way.
>
>Sure it can.  Each thread has its own errno, and the kernel need only
>switch those values into and out of the processes global errno variable
>each time it context switches a thread.
>
>A simple matter of programming.
>	/r$


  Ahh, Rich.  Take a look at how most OS's fill in errno.  It requires
  in-process code to store values into errno.  The kernel is clueless
  about where errno is.  If you have real kernel threads, you'd have
  to put part of the context switch into user space.  This results in
  less than SMOP.  Of course, since it _is_ software, it's not
  impossible.

-- 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] 12+ messages in thread

* Re: errno and DCE and threads
  1994-11-15 10:14   ` Casper H.S. Dik
  1994-11-15 16:38     ` Rich Salz
@ 1994-11-15 22:09     ` Spike White
  1994-11-17 17:09       ` Alan Deehr
  1 sibling, 1 reply; 12+ messages in thread
From: Spike White @ 1994-11-15 22:09 UTC (permalink / raw)


Casper H.S. Dik (casper@fwi.uva.nl) wrote:
: rsalz@osf.org (Rich Salz) writes:

: >In practice, however, it's probably easier to just store errno in the internal
: >per-thread state block and copy it in/out as part of your thread task
: >context switch.


: >For kernel threads, the kernel should just be doing the right thing.

: How can a the kernel do this?  If errno is a statica variable,
: the kernel cannot move it about in a transparent way.
: Multpiple threads on multiple processors will want to read
: the variable so without #define errno (*(___errno())) I don't
: see a way of doing it.

In fact, at least one kernel level threads implementation (Solaris') does
use the '#define errno' trick.  Mainly, in /usr/include/errno.h, it has
the following:

extern int *___errno();
#define errno (*(___errno()))


So every time the process looks at errno, it's really looking at *(___errno()),
which, if you look at carefully, evaluates to a lhs.  Presumably, 
___errno() returns a pointer to the thread-specific errno and there's a errno
maintained by the kernel for each thread.

It seems like in this implementation, there's no notion of errno as a
process-wide global.

--
Spike White       | spike@hal.com               |  Biker Nerds From 
HaL Computer Corp | spike@rider.cactus.org      |     HaL
Austin, TX        | '87 BMW K75S, DoD #1347     |
Disclaimer:  HaL, do you want me to speak for you?  No, Dave... 


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

* Re: errno and DCE and threads
  1994-11-15 22:09     ` Spike White
@ 1994-11-17 17:09       ` Alan Deehr
  0 siblings, 0 replies; 12+ messages in thread
From: Alan Deehr @ 1994-11-17 17:09 UTC (permalink / raw)


Isn't errno returned in a register?  The thread can store the
register into thread-specific storage.


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

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

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1994-11-07 18:45 errno and DCE and threads David Wright
1994-11-07 20:14 ` Alan Lehotsky
1994-11-08 15:42 ` Andrew Hastings
1994-11-08 21:35   ` Simon Patience
1994-11-09 20:05     ` Andrew Hastings
1994-11-09 13:27 ` Rich Salz
1994-11-15 10:14   ` Casper H.S. Dik
1994-11-15 16:38     ` Rich Salz
1994-11-15 21:19       ` Casper H.S. Dik
1994-11-15 21:40       ` Alan Lehotsky
1994-11-15 22:09     ` Spike White
1994-11-17 17:09       ` Alan Deehr

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