4 ms·
There is no reason that `getenv()` should ever lock -- it should always be lock-free. `putenv()`/`setenv()`/`unsetenv()` can lock, of course, since there's no
by cryptonector 3y ago
There is no reason that `getenv()` should ever lock -- it should always be lock-free. `putenv()`/`setenv()`/`unsetenv()` can lock, of course, since there's no point allowing more than one writer at a time.
Don't believe me? Look at this work of art (the entire file!): https://src.illumos.org/source/xref/illumos-gate/usr/src/lib/libc/port/gen/getenv.c?r=60b81b86#48 https://src.illumos.org/source/xref/illumos-gate/usr/src/lib...
- kimixa 3y agoWhat are you trying to say using that example? The getenv() function shown calls initenv(), which can (clearly) take a lock?
- cryptonector 3y agoOne time. Also, I think that could be removed with some care.
- kimixa 3y agoBut it unconditionally calls a memory barrier, which is most of the cost of an un-contended futex-style lock already. And I'd be interested in your ideas on removing the lock - as I can't see any paths that don't change semantics (e.g. unconditionally doing the init work at process start time when you know there's not multiple threads, for example)
- cryptonector 3y ago> a memory barrier, which is most of the cost of an un-contended futex-style lock already Yes, but it's not a lock. > unconditionally doing the init work at process start time when you know there's not multiple threads That's the most obvious fix, yes. I was thinking (but I've not checked yet) that when `my_environ` is not set up yet then `getenv()` can use `_environ` directly.