[Bf-blender-cvs] [c18849b] lib-link-rework-temp: Lock bmain around core part of linking code now, this is much safer imho!

Bastien Montagne noreply at git.blender.org
Mon Sep 21 17:47:56 CEST 2015


Commit: c18849bb1b14b0459090fde9278652068fd6cf88
Author: Bastien Montagne
Date:   Mon Sep 21 16:35:29 2015 +0200
Branches: lib-link-rework-temp
https://developer.blender.org/rBc18849bb1b14b0459090fde9278652068fd6cf88

Lock bmain around core part of linking code now, this is much safer imho!

This also means we need to ensure no code called from there tries to lock again bmain
(no reentrant spinlock, arg :/).

===================================================================

M	source/blender/blenkernel/BKE_library.h
M	source/blender/blenkernel/intern/library.c
M	source/blender/blenloader/intern/readfile.c
M	source/blender/windowmanager/intern/wm_operators.c

===================================================================

diff --git a/source/blender/blenkernel/BKE_library.h b/source/blender/blenkernel/BKE_library.h
index 5b12858..fa062b7 100644
--- a/source/blender/blenkernel/BKE_library.h
+++ b/source/blender/blenkernel/BKE_library.h
@@ -49,6 +49,7 @@ struct bContext;
 struct PointerRNA;
 struct PropertyRNA;
 
+void *BKE_libblock_alloc_notest(short type);
 void *BKE_libblock_alloc(struct Main *bmain, short type, const char *name) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
 void *BKE_libblock_copy_ex(struct Main *bmain, struct ID *id) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
 void *BKE_libblock_copy_nolib(struct ID *id, const bool do_action) ATTR_NONNULL();
diff --git a/source/blender/blenkernel/intern/library.c b/source/blender/blenkernel/intern/library.c
index 9fb0cb4..cc04984 100644
--- a/source/blender/blenkernel/intern/library.c
+++ b/source/blender/blenkernel/intern/library.c
@@ -647,7 +647,7 @@ int set_listbasepointers(Main *main, ListBase **lb)
  * Allocates and returns memory of the right size for the specified block type,
  * initialized to zero.
  */
-static ID *alloc_libblock_notest(short type)
+void *BKE_libblock_alloc_notest(short type)
 {
 	ID *id = NULL;
 	
@@ -769,7 +769,7 @@ void *BKE_libblock_alloc(Main *bmain, short type, const char *name)
 	ID *id = NULL;
 	ListBase *lb = which_libbase(bmain, type);
 	
-	id = alloc_libblock_notest(type);
+	id = BKE_libblock_alloc_notest(type);
 	if (id) {
 		BKE_main_lock(bmain);
 		BLI_addtail(lb, id);
@@ -837,7 +837,7 @@ void *BKE_libblock_copy_nolib(ID *id, const bool do_action)
 	ID *idn;
 	size_t idn_len;
 
-	idn = alloc_libblock_notest(GS(id->name));
+	idn = BKE_libblock_alloc_notest(GS(id->name));
 	assert(idn != NULL);
 
 	BLI_strncpy(idn->name, id->name, sizeof(idn->name));
diff --git a/source/blender/blenloader/intern/readfile.c b/source/blender/blenloader/intern/readfile.c
index 7e1e0e4..4cb4fc0 100644
--- a/source/blender/blenloader/intern/readfile.c
+++ b/source/blender/blenloader/intern/readfile.c
@@ -618,9 +618,21 @@ static Main *blo_find_main(FileData *fd, const char *filepath, const char *relab
 	m = BKE_main_new();
 	BLI_addtail(mainlist, m);
 	
-	/* Add library datablock itself to 'main' Main, since libraries are **never** linked data.
-	 * Fixes bug where you could end with all ID_LI datablocks having the same name... */
-	lib = BKE_libblock_alloc(mainlist->first, ID_LI, "Lib");
+	/* Adapted from BKE_libblock_alloc(), with no lock of main, it's most likely already locked by caller code. */
+	lib = BKE_libblock_alloc_notest(ID_LI);
+	if (lib) {
+		/* Add library datablock itself to 'main' Main, since libraries are **never** linked data.
+		 * Fixes bug where you could end with all ID_LI datablocks having the same name... */
+		ListBase *libraries = &((Main *)mainlist->first)->library;
+		ID *id = (ID *)lib;
+
+		BLI_addtail(libraries, id);
+		id->us = 1;
+		id->icon_id = 0;
+		*((short *)id->name) = ID_LI;
+		new_id(libraries, id, "Lib");
+	}
+
 	BLI_strncpy(lib->name, filepath, sizeof(lib->name));
 	BLI_strncpy(lib->filepath, name1, sizeof(lib->filepath));
 	
diff --git a/source/blender/windowmanager/intern/wm_operators.c b/source/blender/windowmanager/intern/wm_operators.c
index b8cb56d..06e1619 100644
--- a/source/blender/windowmanager/intern/wm_operators.c
+++ b/source/blender/windowmanager/intern/wm_operators.c
@@ -2862,9 +2862,12 @@ static int wm_link_append_exec(bContext *C, wmOperator *op)
 			if (BLO_library_path_explode(path, libname, &group, &name)) {
 				WMLinkAppendDataItem *item;
 				if (!group || !name) {
+					printf("skipping %s\n", path);
 					continue;
 				}
 
+				printf("linking %s\n", path);
+
 				lib_idx = GET_INT_FROM_POINTER(BLI_ghash_lookup(libraries, libname));
 
 				item = wm_link_append_data_item_add(lapp_data, name, BKE_idcode_from_name(group), NULL);
@@ -2883,8 +2886,12 @@ static int wm_link_append_exec(bContext *C, wmOperator *op)
 		BLI_BITMAP_ENABLE(item->libraries, 0);
 	}
 
+	BKE_main_lock(bmain);
+
 	wm_link_do(lapp_data, op->reports, bmain, scene, CTX_wm_view3d(C));
 
+	BKE_main_unlock(bmain);
+
 	wm_link_append_data_free(lapp_data);
 
 	/* mark all library linked objects to be updated */




More information about the Bf-blender-cvs mailing list