Bug 17437

Summary: pthread_atfork() missing from libc_r
Product: Base System Reporter: mikko <mikko>
Component: binAssignee: Jason Evans <jasone>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: 5.0-CURRENT   
Hardware: Any   
OS: Any   
Attachments:
Description Flags
file.diff none

Description mikko 2000-03-17 18:30:02 UTC
While porting some code using pthreads to FreeBSD, I noticed to my
surprise that pthread_atfork() was nowhere to be found, and there
did not seem to be any PRs related to the function.

So either I am the only one using pthread_atfork(), or it has fallen
out of the standard...  Most other systems (Solaris, AIX, HP-UX, IRIX
and Linux) has the function, and it is documented at:

<http://www.opengroup.org/onlinepubs/007908799/xsh/pthread_atfork.html>

Enough complaining.  Unless I have seriously misunderstood the
workings of libc_r, the patch below should bring pthread_atfork() to
FreeBSD (applies against -CURRENT, March 16).  It works for me.

Someone may want to turn the man-page into proper English too...

	Regards,
	/Mikko

How-To-Repeat: 
Compile anything that uses pthread_atfork()...
Comment 1 Jason Evans freebsd_committer freebsd_triage 2000-03-17 22:21:22 UTC
Responsible Changed
From-To: freebsd-bugs->jasone

Over to maintainer. 

Comment 2 mikko 2000-03-18 09:36:51 UTC
(Following up on myself after further investigations):

Nope, the above patch is insufficient, it only serves to uncover
other bugs.

The problem is that libc_r does not do complete cleanup of the other
threads after the fork.  All deleted threads must be quietly removed
from any queues they happen to be waiting in, or they may later be
subject to "revival" after they have been freed.

Scenario (typical use of pthread_atfork()):
	 - forking thread aquires a number of mutexes.
	 - another thread waits on one of the mutexes
	 - after forking, the mutexes are released,
	   which will make any waiting threads
	   runnable -- but they have been deallocated!
	 - Bad Things(tm) happen...

All mutexes where this may happen can be conveniently located via the
"mutexq" field of the running thread, so this particular scenario is
easily fixed.

There is no similar way to handle threads queued on, for example,
condition variables, as there is no way to find the head of the "qe"
and "pqe" queue entries.  The "join_queue" should be emptied too.

Basically, right now libc_r does not work at all after a fork().

      Regards,
      /Mikko
Comment 3 Jason Evans freebsd_committer freebsd_triage 2000-05-23 18:03:29 UTC
State Changed
From-To: open->closed

There is no plan to add pthread_atfork(), due to the difficulty and limited 
usefulness of doing so.