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.
This commit is contained in:
Sergey Sharybin 2013-01-24 08:14:05 +00:00
parent c65c1958bd
commit 5e739ddae2
2 changed files with 37 additions and 0 deletions

@ -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

@ -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);