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