Subject: Re: standards, pthread symbols, and headers
To: None <nathanw@wasabisystems.com>
From: Ben Harris <bjh21@netbsd.org>
List: tech-userlevel
Date: 07/16/2003 23:55:32
[ Warning: I've got a headache and have just been trying to reconcile
  gwoods' view of the world with ISO/IEC 9899:1990.  Don't expect
  coherence, or all that much accuracy. ]

In article <mtuoezuuoro.fsf@scrubbing-bubbles.mit.edu> you write:
>The first one is that the pthread_*_t types are supposed to be defined
>in <sys/types.h>. Fortunately, the types are encapsulated into their
>own header already; would it be okay to add:
>
>#if !defined(_KERNEL)
>#include <pthread_types.h>
>#endif
>
>to <sys/types.h>?

That seems sensible to me.

> What level of feature-test protection should it get?

You might want to check some of the older standards, but a reasonable start
would be:

#if (_POSIX_C_SOURCE - 0L) >= 200111L || (_XOPEN_SOURCE - 0) >= 600 || \
    defined(_NETBSD_SOURCE)

>I'm also somewhat concerned that this exposes the symbols too widely,
>since lots of other headers include <sys/types.h>.

All versions of POSIX and XPG permit any name ending in "_t" to be exposed
by any header.  As such, as long as you wrap it in some kind of feature-test
so that pure ANSI source doesn't see it, you should be fine.

>Next, I was looking for a place to put PTHREAD_DESTRUCTOR_ITERATIONS
>and PTHREAD_KEYS_MAX such that the values end up in <limits.h>, and
>couldn't see a good spot of prior art for actual limit values, as
>opposed to minimum values...?

First, are they ever going to vary?  If new versions of libpthread might
have different values, you might like to leave them undefined and force
applications to use sysconf().  That way, you don't have to bump the major
version number of libpthread just because you changed a constant.

> along the way, I also noticed that
><limits.h> defines a lot of _POSIX_* symbols for minimum values. SUSv3
>claims those should be in <unistd.h>, and <limits.h> should have the
>actual limit, if any, rather than the minimums. Are they in the wrong
>place, or am I missing something?

I think you're confusing two types of _POSIX_* name.  Some of them (like
_POSIX_PTHREAD_KEYS_MAX) define minimum limits and should appear in
<limits.h>.  Others (like _POSIX_THREADS) indicate the presence of features,
and should appear in <unistd.h>.

>Finally, there are a bunch of pthread-related sysconf() variables
>relating to feature-testing and limits. Is the correct approach to
>define a CTL_USER sysctl for each _SC_* symbol and glue all the cases
>into sysconf.c and sysctl.c in libc?

I'm not sure I have a clear opinion on this.  My preference would be to have
a __pthread_sysconf() in libpthread that's used by sysconf() to get
pthread-specific values.  That way, you always get the values appropriate
the running libpthread.  CTL_USER seems like a horrible idea to me.

-- 
Ben Harris