[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [54066] trunk/blender: Added some code which helps troubleshooting issues caused by

Sergey Sharybin sergey.vfx at gmail.com
Thu Jan 24 09:14:09 CET 2013


Revision: 54066
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=54066
Author:   nazgul
Date:     2013-01-24 08:14:05 +0000 (Thu, 24 Jan 2013)
Log Message:
-----------
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.

Modified Paths:
--------------
    trunk/blender/intern/guardedalloc/intern/mallocn.c
    trunk/blender/source/blender/blenlib/intern/threads.c

Modified: trunk/blender/intern/guardedalloc/intern/mallocn.c
===================================================================
--- trunk/blender/intern/guardedalloc/intern/mallocn.c	2013-01-24 05:54:17 UTC (rev 54065)
+++ trunk/blender/intern/guardedalloc/intern/mallocn.c	2013-01-24 08:14:05 UTC (rev 54066)
@@ -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 @@
 #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 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

Modified: trunk/blender/source/blender/blenlib/intern/threads.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/threads.c	2013-01-24 05:54:17 UTC (rev 54065)
+++ trunk/blender/source/blender/blenlib/intern/threads.c	2013-01-24 08:14:05 UTC (rev 54066)
@@ -729,6 +729,9 @@
 
 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_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);




More information about the Bf-blender-cvs mailing list