Externalize malloc's spinlock so that a thread library can take

it around an application's fork() call.  Our new thread libraries
(libthr, libpthread) can now have threads running while another
thread calls fork().  In this case, it is possible for malloc
to be left in an inconsistent state in the child.  Our thread
libraries, libpthread in particular, need to use malloc internally
after a fork (in the child).

Reviewed by:	davidxu
This commit is contained in:
deischen 2003-11-04 19:49:56 +00:00
parent 0792828951
commit 96918b9811
2 changed files with 9 additions and 0 deletions

View File

@ -38,6 +38,8 @@
#ifndef _LIBC_PRIVATE_H_
#define _LIBC_PRIVATE_H_
#include <spinlock.h>
/*
* This global flag is non-zero when a process has created one
* or more threads. It is used to avoid calling locking functions
@ -120,4 +122,10 @@ int _yp_check(char **);
*/
extern const char *__progname;
/*
* This is the lock to make malloc() thread-safe. It is externalized
* so that thread libraries can protect malloc across fork().
*/
extern spinlock_t *__malloc_lock;
#endif /* _LIBC_PRIVATE_H_ */

View File

@ -74,6 +74,7 @@ __FBSDID("$FreeBSD$");
# include "libc_private.h"
# include "spinlock.h"
static spinlock_t thread_lock = _SPINLOCK_INITIALIZER;
spinlock_t *__malloc_lock = &thread_lock;
# define _MALLOC_LOCK() if (__isthreaded) _SPINLOCK(&thread_lock);
# define _MALLOC_UNLOCK() if (__isthreaded) _SPINUNLOCK(&thread_lock);
#endif /* __FreeBSD__ */