Anonymous avatar Anonymous committed 8871361

attempt at making malloc-hooks thread safe

Comments (0)

Files changed (3)

+2010-02-08  Sebastien Binet  <binet@farnsworth>
+
+	* tagging PerfMonEvent-00-07-07
+	* attempt at making malloc-hooks thread safe
+	* M src/MemStatsHooks.cxx
+	* M cmt/requirements
+
 2010-01-26  Sebastien Binet  <binet@farnsworth>
 
 	* tagging PerfMonEvent-00-07-06
 
 apply_pattern installed_library
 
-macro_append use_linkopts " -lrt" \
- Darwin ""
+macro_append use_linkopts " -lrt -lpthread" \
+ Darwin " -lpthread"
 
 ### dict generation
 private

src/MemStatsHooks.cxx

 // PerfMonEvent includes
 #include "PerfMonEvent/MemStatsHooks.h"
 
+// std includes
+#include <pthread.h>
+
+// mutex for the hooks
+static pthread_mutex_t pmon_mem_lock = PTHREAD_MUTEX_INITIALIZER; 
+
 // holders for original hooks
 static void* (*orig_malloc) (size_t size, const void* caller);
 static void* (*orig_realloc)(void* ptr, size_t size, const void* caller);
 
 void PerfMon::MemStats::start()
 {
+  pthread_mutex_init(&pmon_mem_lock, NULL);
+  pthread_mutex_lock(&pmon_mem_lock);
   saveHooks();
   if (PerfMon::MemStats::m_enabled) {
     installHooks();
+  } else {
+    // installHooks unlocks the mutex...
+    pthread_mutex_unlock(&pmon_mem_lock);
   }
 }
  
   // if (PerfMon::MemStats::m_enabled) {
   //   uninstallHooks();
   // }
+  pthread_mutex_destroy(&pmon_mem_lock);
 }
 
 void PerfMon::MemStats::installHooks()
   __free_hook    = pmon_mem_free;
   __realloc_hook = pmon_mem_realloc;
   __malloc_hook  = pmon_mem_malloc;
+  pthread_mutex_unlock(&pmon_mem_lock);
 }
 
 void PerfMon::MemStats::uninstallHooks()
 {
+  pthread_mutex_lock(&pmon_mem_lock);
   __free_hook    = orig_free;
   __realloc_hook = orig_realloc;
   __malloc_hook  = orig_malloc;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.