libthr shared locks

Konstantin Belousov kostikbel at
Sat Dec 26 23:44:32 UTC 2015

On Sat, Dec 26, 2015 at 12:15:43PM -0500, Daniel Eischen wrote:
> I think libthr has always done that (postpoining until critical
> section is exited).  But there are libc (and perhaps other)
> locks that may be left locked on a fork().  Really (according
> to POSIX), a fork() from a threaded program can only be used
> to exec() something.
libthr only started to do the interception in r212076, which was
committed by David on Sep 1, 2010.

Posix left the behaviour undefined after the fork, but real world
rejects implementations which make C runtime broken after fork in mt
process. Rtld must work in the child anyway (and in sighandler as well).
We were forced to ensure also that malloc(3) and pthread_create(3) work
in the child after the fork, and any transient regressions in this area
where immediately reported. One of the biggest abusers of that are

> I guess I am just not that worried about the ABI change when combined
> with a library version bump.  We use to do this for every new major
> release, it isn't anything that hasn't been done before.  We never
> supported 2 thread libraries in the same executable, and it is
> obvious when that happens (I think we even instrumented libc_r,
> or was it rtld?, to emit a warning when that happened.  My memory
> is fuzzy).  With poudriere, our new package system, I would think
> we would be better off now than 10 years ago?

We did significant work to avoid requiring complete recompilation of all
user binaries on the major version updates. Part of it is the symbol
versioning, another part is the stronger discipline and future planning
when doing ABI-sensitive changes. At least starting with stable/8, when
you binary depends only on the basic C runtime (i.e. rtld + libc + libm
+ libthr), you are no longer required to recompile it, and you do not
need to install any compat libraries. Things just work.

We are not there yet, since some libraries are not managed that good,
e.g. libutil is not symver-ed but at least usually bumped correctly.
Some libraries are handled much worse, e.g. libcam _interface_ depends
on volatile kernel parts and libcam version is not always bumped when
needed.  Unfortunately, libcam is the dependency of high-profile user
applications, from Gnome and KDE.  This is what prevents us from
stopping declaring 'recompile ports on major upgrade', but it is a

Still, basic C runtime is in much better shape WRT ABI stability
than it was, say, in the 6.x-7.x time. This is why ino_t 64bit is moved
with so much caution, and should also explain why I am so nervous to
lock inlining.  Doing libthr bump would break things.

> I do note that I was initially wrong in thinking that port revision
> bumps could help alleviate any pain.  A port could be built in
> -stable with the revision bump just as well as in -current (or 11).
> What would be needed is for pkg to understand that any port built
> for 10 and previous could not run on 11+ (just to be on the safe
> side).
BTW, I tried to explain exactly this scenario in one of the previous

> > OTOH, we should be able to plan such change, which requires much more
> > drastic measures to be implementable. I started thinking about it, and
> > I noted that what is needed is solved by renaming libthr to something
> > else, e.g. libthr2. One of the issue which is not solved by the dso
> > version bump (not to even mention versioned symbols bump) is the
> > static linker bringing both and into the same
> > process, by the mere use of -lpthread by different objects at different
> > times. Then, libthr and hypothetical libthr2 should check and prevent
> > activation of image which loads both.
> >
> > But these are longtime to evaluate and implement. The feature at hand
> > does not require ABI change, as my patch demostrated. Yes, shared
> > mutexes would be more naturally implemented with the inline locks, and
> > avoidance of an indirection in the libthr is also good, but lets not mix
> > significant ABI change and some less ambitious feature. My approach does
> > not lock out future strategies.
> I agree, but the work that you are doing now would be basically
> thrown out later on.  I will not stand in your way and appreciate
> any work you do.  I would just rather that the struct change be
> made now for 11, even without any pshared or other changes.  For
> once the struct change is made, pshared or other additions can
> be made afterward, even in the 11 branch because they would not
> break the ABI.

Lock inlining was not done for ten years, now cost of doing it is
extremely high, as discussed above.  Who would drive the change, and
with what time frame ?  If me, I seriosly consider renaming libthr
to libthr2, but I had no time to think much about it.

Right now, I think that I want to commit my current patch and implement
robust mutexes as the next step, without ABI breakage. At least, this
seems to have fixed time-frame and can be made ready for 11.x. Lock
inlining might be not. Are there serious objections against the plan,
except that (lock inlining + pshared) is ideal situation, while the plan
is not (but more practical) ?

More information about the freebsd-threads mailing list