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