This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc 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: [RFC] Make getenv O(1)


On Mon, Oct 21, 2013 at 08:59:55PM +0200, OndÅej BÃlka wrote:
> On Mon, Oct 21, 2013 at 02:04:53PM -0400, Rich Felker wrote:
> > On Mon, Oct 21, 2013 at 09:26:32PM +0400, Alexander Monakov wrote:
> > > 
> > > 
> > > On Mon, 21 Oct 2013, OndÅej BÃlka wrote:
> > > 
> > > > On Mon, Oct 21, 2013 at 08:55:37PM +0400, Alexander Monakov wrote:
> > > > > On Mon, 21 Oct 2013, OndÅej BÃlka wrote:
> > > > > > Why, following does not need synchronization.
> > > > > > 
> > > > > > #define GETENV(x) ({ \
> > > > > >   static char *__ret = NULL; \
> > > > > >   if (__builtin_constant_p (x) && !__cached) \
> > > > > >     __ret = getenv (x) + 1; \
> > > > > >   __builtin_constant_p (x) ? __ret - 1: getenv (x);})
> > > > > 
> > > > > This wouldn't compile as __cached is not defined (and I'll admit I don't see
> > > > > what approach you're trying to convey).
> > > > > 
> > > > s/__cached/__ret/ 
> > > 
> > > Ah, my bad.  While this would be ok in practice, I think incrementing a
> > > pointer containing a NULL value invokes undefined behavior, doesn't it?
> > 
> > It does. So does unsynchronized access to an object, even if you think
> > it's harmless like in this case. If you only care about the null UB,
> > you could however just use the address of a "static const char
> > __sentinel" instead of NULL as the flag for uninitialized. But I think
> > it's best to simply write this with clean, correct use of
> > synchronization rather than ugly hacks.
> > 
> If you want you could use conversion from/to uintptr_t for arithmetic
> and atomic write/exchange for storing.

That would also be an option.

> Or say that getenv is not required to be thread safe and problem is in
> user code.

getenv is required to be thread-safe, and more importantly, most of the
functions which depend on environment variables are required to be
thread-safe. It's only _changing_ the environment which is not
thread-safe.

Rich


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