diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/maybe_threads.cc | 14 | ||||
-rw-r--r-- | src/maybe_threads.h | 7 | ||||
-rw-r--r-- | src/static_vars.cc | 12 |
3 files changed, 27 insertions, 6 deletions
diff --git a/src/maybe_threads.cc b/src/maybe_threads.cc index 6dd0d8d..acfc99a 100644 --- a/src/maybe_threads.cc +++ b/src/maybe_threads.cc @@ -48,6 +48,7 @@ #include <string> #include "maybe_threads.h" #include "base/basictypes.h" +#include "base/logging.h" // __THROW is defined in glibc systems. It means, counter-intuitively, // "This function will never throw an exception." It's an optional @@ -68,6 +69,10 @@ extern "C" { __THROW ATTRIBUTE_WEAK; int pthread_once(pthread_once_t *, void (*)(void)) ATTRIBUTE_WEAK; + int pthread_atfork(void (*__prepare) (void), + void (*__parent) (void), + void (*__child) (void)) + __THROW ATTRIBUTE_WEAK; } #define MAX_PERTHREAD_VALS 16 @@ -155,3 +160,12 @@ int perftools_pthread_once(pthread_once_t *ctl, return 0; } } + +void perftools_pthread_atfork(void (*before)(), + void (*parent_after)(), + void (*child_after)()) { + if (pthread_atfork) { + int rv = pthread_atfork(before, parent_after, child_after); + CHECK(rv == 0); + } +} diff --git a/src/maybe_threads.h b/src/maybe_threads.h index b60f4ef..c6cfdf7 100644 --- a/src/maybe_threads.h +++ b/src/maybe_threads.h @@ -51,4 +51,11 @@ int perftools_pthread_setspecific(pthread_key_t key, void *val); int perftools_pthread_once(pthread_once_t *ctl, void (*init_routine) (void)); +// Our wrapper for pthread_atfork. Does _nothing_ when there are no +// threads. See static_vars.cc:SetupAtForkLocksHandler for only user +// of this. +void perftools_pthread_atfork(void (*before)(), + void (*parent_after)(), + void (*child_after)()); + #endif /* GOOGLE_MAYBE_THREADS_H_ */ diff --git a/src/static_vars.cc b/src/static_vars.cc index 09d2b59..79de97e 100644 --- a/src/static_vars.cc +++ b/src/static_vars.cc @@ -43,6 +43,7 @@ #include "sampler.h" // for Sampler #include "getenv_safe.h" // TCMallocGetenvSafe #include "base/googleinit.h" +#include "maybe_threads.h" namespace tcmalloc { @@ -107,16 +108,15 @@ void Static::InitStaticVars() { } -#if defined(HAVE_FORK) && defined(HAVE_PTHREAD) +#if defined(HAVE_FORK) && defined(HAVE_PTHREAD) && !defined(__APPLE__) static inline void SetupAtForkLocksHandler() { -#if !defined(__APPLE__) - pthread_atfork(CentralCacheLockAll, // parent calls before fork - CentralCacheUnlockAll, // parent calls after fork - CentralCacheUnlockAll); // child calls after fork -#endif + perftools_pthread_atfork( + CentralCacheLockAll, // parent calls before fork + CentralCacheUnlockAll, // parent calls after fork + CentralCacheUnlockAll); // child calls after fork } REGISTER_MODULE_INITIALIZER(tcmalloc_fork_handler, SetupAtForkLocksHandler()); |