From 16e092af3e50ebe529f06bb566e9b4e9b883f6da Mon Sep 17 00:00:00 2001 From: Georg Hopp Date: Wed, 24 Sep 2014 19:20:49 +0100 Subject: [PATCH] try to make the memory management thread save by using a mutex upon tree accesses --- src/Makefile.am | 2 +- src/memory.c | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Makefile.am b/src/Makefile.am index bd42cac..46d46f5 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -24,6 +24,6 @@ TR_CLASS = memory.c \ lib_LTLIBRARIES = libtrbase.la libtrbase_la_SOURCES = $(TR_CLASS) -libtrbase_la_CFLAGS = $(AM_CFLAGS) +libtrbase_la_CFLAGS = $(AM_CFLAGS) -lpthread libtrbase_la_LIBADD = libtrbase_la_LDFLAGS = -version-info 1:0:1 diff --git a/src/memory.c b/src/memory.c index 1d3d97f..4d109a8 100644 --- a/src/memory.c +++ b/src/memory.c @@ -48,6 +48,7 @@ #include +#include #include #include #include @@ -377,6 +378,8 @@ TR_reference(void * mem) return mem; } +pthread_mutex_t TR_memop_lock = PTHREAD_MUTEX_INITIALIZER; + /* * This tries to reflect the memory management behaviour of the * GNU version of malloc. For other versions this might need @@ -435,7 +438,9 @@ TR_malloc(size_t size) } #ifdef MEM_OPT + pthread_mutex_lock(&TR_memop_lock); seg = deleteElement(&segments, size); + pthread_mutex_unlock(&TR_memop_lock); #endif if (NULL == seg) { @@ -474,7 +479,9 @@ TR_free(void ** mem) seg->ref_count--; } else { #ifdef MEM_OPT + pthread_mutex_lock(&TR_memop_lock); insertElement(&segments, seg); + pthread_mutex_unlock(&TR_memop_lock); #else free(seg); #endif