malloc_debug: reset TrackData mutex after fork
Add a pthread_atfork handler to malloc_debug to lock the TrackData mutex
during fork and reset it in the child. Ensures that the TrackData is
consistent when forking from a multi-threaded process, and that the
mutex is in a defined state in the child.
Change-Id: I0dfed30045a28551217ceac227d33b2e18894932
diff --git a/libc/malloc_debug/TrackData.h b/libc/malloc_debug/TrackData.h
index 45e6892..dcf0ede 100644
--- a/libc/malloc_debug/TrackData.h
+++ b/libc/malloc_debug/TrackData.h
@@ -58,6 +58,10 @@
void DisplayLeaks(DebugData& debug);
+ void PrepareFork() { pthread_mutex_lock(&mutex_); }
+ void PostForkParent() { pthread_mutex_unlock(&mutex_); }
+ void PostForkChild() { pthread_mutex_init(&mutex_, NULL); }
+
private:
pthread_mutex_t mutex_ = PTHREAD_MUTEX_INITIALIZER;
std::unordered_set<Header*> headers_;