Commit 16e092af3e50ebe529f06bb566e9b4e9b883f6da

Authored by Georg Hopp
1 parent db5f9d43

try to make the memory management thread save by using a mutex upon tree accesses

@@ -24,6 +24,6 @@ TR_CLASS = memory.c \ @@ -24,6 +24,6 @@ TR_CLASS = memory.c \
24 lib_LTLIBRARIES = libtrbase.la 24 lib_LTLIBRARIES = libtrbase.la
25 25
26 libtrbase_la_SOURCES = $(TR_CLASS) 26 libtrbase_la_SOURCES = $(TR_CLASS)
27 -libtrbase_la_CFLAGS = $(AM_CFLAGS) 27 +libtrbase_la_CFLAGS = $(AM_CFLAGS) -lpthread
28 libtrbase_la_LIBADD = 28 libtrbase_la_LIBADD =
29 libtrbase_la_LDFLAGS = -version-info 1:0:1 29 libtrbase_la_LDFLAGS = -version-info 1:0:1
@@ -48,6 +48,7 @@ @@ -48,6 +48,7 @@
48 48
49 #include <stdio.h> 49 #include <stdio.h>
50 50
  51 +#include <pthread.h>
51 #include <stdlib.h> 52 #include <stdlib.h>
52 #include <string.h> 53 #include <string.h>
53 #include <search.h> 54 #include <search.h>
@@ -377,6 +378,8 @@ TR_reference(void * mem) @@ -377,6 +378,8 @@ TR_reference(void * mem)
377 return mem; 378 return mem;
378 } 379 }
379 380
  381 +pthread_mutex_t TR_memop_lock = PTHREAD_MUTEX_INITIALIZER;
  382 +
380 /* 383 /*
381 * This tries to reflect the memory management behaviour of the 384 * This tries to reflect the memory management behaviour of the
382 * GNU version of malloc. For other versions this might need 385 * GNU version of malloc. For other versions this might need
@@ -435,7 +438,9 @@ TR_malloc(size_t size) @@ -435,7 +438,9 @@ TR_malloc(size_t size)
435 } 438 }
436 439
437 #ifdef MEM_OPT 440 #ifdef MEM_OPT
  441 + pthread_mutex_lock(&TR_memop_lock);
438 seg = deleteElement(&segments, size); 442 seg = deleteElement(&segments, size);
  443 + pthread_mutex_unlock(&TR_memop_lock);
439 #endif 444 #endif
440 445
441 if (NULL == seg) { 446 if (NULL == seg) {
@@ -474,7 +479,9 @@ TR_free(void ** mem) @@ -474,7 +479,9 @@ TR_free(void ** mem)
474 seg->ref_count--; 479 seg->ref_count--;
475 } else { 480 } else {
476 #ifdef MEM_OPT 481 #ifdef MEM_OPT
  482 + pthread_mutex_lock(&TR_memop_lock);
477 insertElement(&segments, seg); 483 insertElement(&segments, seg);
  484 + pthread_mutex_unlock(&TR_memop_lock);
478 #else 485 #else
479 free(seg); 486 free(seg);
480 #endif 487 #endif
Please register or login to post a comment