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()...
Responsible Changed From-To: freebsd-bugs->jasone Over to maintainer.
(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
State Changed From-To: open->closed There is no plan to add pthread_atfork(), due to the difficulty and limited usefulness of doing so.