diff options
author | Sergey Sharybin <sergey.vfx@gmail.com> | 2013-01-24 12:14:05 +0400 |
---|---|---|
committer | Sergey Sharybin <sergey.vfx@gmail.com> | 2013-01-24 12:14:05 +0400 |
commit | 5e739ddae2cc5fdb30e62544bfdd5b55284962a5 (patch) | |
tree | f3a449a1d591513cd87a7671dbc0fa2bea556dc5 | |
parent | c65c1958bde6425cedb810590d64252adc2e56c3 (diff) |
Added some code which helps troubleshooting issues caused by
non-threadsafe usage of guarded allocator.
Also added small chunk of code to check consistency of begin/end
threaded malloc.
All this additional checks are commented and wouldn't affect on
builds, however found them helpful to troubleshoot issues so
decided to commit it to SVN.
-rw-r--r-- | intern/guardedalloc/intern/mallocn.c | 31 | ||||
-rw-r--r-- | source/blender/blenlib/intern/threads.c | 6 |
2 files changed, 37 insertions, 0 deletions
diff --git a/intern/guardedalloc/intern/mallocn.c b/intern/guardedalloc/intern/mallocn.c index 11fa477c2a4..559d1138ffb 100644 --- a/intern/guardedalloc/intern/mallocn.c +++ b/intern/guardedalloc/intern/mallocn.c @@ -68,6 +68,17 @@ //#define DEBUG_MEMCOUNTER +/* Only for debugging: + * defining DEBUG_THREADS will enable check whether memory manager + * is locked with a mutex when allocation is called from non-main + * thread. + * + * This helps troubleshooting memory issues caused by the fact + * guarded allocator is not thread-safe, however this check will + * fail to check allocations from openmp threads. + */ +//#define DEBUG_THREADS + #ifdef DEBUG_MEMCOUNTER /* set this to the value that isn't being freed */ # define DEBUG_MEMCOUNTER_ERROR_VAL 0 @@ -122,6 +133,12 @@ typedef struct MemHead { #endif #endif +#ifdef DEBUG_THREADS +# include <assert.h> +# include <pthread.h> +static pthread_t mainid; +#endif + typedef struct MemTail { int tag3, pad; } MemTail; @@ -206,6 +223,20 @@ static void print_error(const char *str, ...) static void mem_lock_thread(void) { +#ifdef DEBUG_THREADS + static int initialized = 0; + + if (initialized == 0) { + /* assume first allocation happens from main thread */ + mainid = pthread_self(); + initialized = 1; + } + + if (!pthread_equal(pthread_self(), mainid) && thread_lock_callback == NULL) { + assert(!"Memory function is called from non-main thread without lock"); + } +#endif + #ifdef DEBUG_OMP_MALLOC assert(omp_in_parallel() == 0); #endif diff --git a/source/blender/blenlib/intern/threads.c b/source/blender/blenlib/intern/threads.c index 146e1d531f1..686484ef1db 100644 --- a/source/blender/blenlib/intern/threads.c +++ b/source/blender/blenlib/intern/threads.c @@ -729,6 +729,9 @@ void BLI_thread_queue_wait_finish(ThreadQueue *queue) void BLI_begin_threaded_malloc(void) { + /* Used for debug only */ + /* BLI_assert(thread_levels >= 0); */ + if (thread_levels == 0) { MEM_set_lock_callback(BLI_lock_malloc_thread, BLI_unlock_malloc_thread); } @@ -737,6 +740,9 @@ void BLI_begin_threaded_malloc(void) void BLI_end_threaded_malloc(void) { + /* Used for debug only */ + /* BLI_assert(thread_levels >= 0); */ + thread_levels--; if (thread_levels == 0) MEM_set_lock_callback(NULL, NULL); |