| 1 |
From 824caddcfdd5f4c7740e77ae7e5b5c46b2b1b47f Mon Sep 17 00:00:00 2001 |
| 2 |
From: David White <[email protected]> |
| 3 |
Date: Fri, 9 Nov 2012 19:41:34 -0800 |
| 4 |
Subject: fixed multi-threaded crash in editor tile rendering logic that could |
| 5 |
cause intermittent crashes when drawing tiles |
| 6 |
|
| 7 |
|
| 8 |
diff --git a/src/level.cpp b/src/level.cpp |
| 9 |
index 1f96f9dfa..a8ddf87d5 100644 |
| 10 |
--- a/src/level.cpp |
| 11 |
+++ b/src/level.cpp |
| 12 |
@@ -837,12 +837,19 @@ void level::start_rebuild_tiles_in_background(const std::vector<int>& layers) |
| 13 |
info.rebuild_tile_layers_worker_buffer = info.rebuild_tile_layers_buffer; |
| 14 |
info.rebuild_tile_layers_buffer.clear(); |
| 15 |
|
| 16 |
+ std::map<int, tile_map> worker_tile_maps = tile_maps_; |
| 17 |
+ for(std::map<int, tile_map>::iterator i = worker_tile_maps.begin(); |
| 18 |
+ i != worker_tile_maps.end(); ++i) { |
| 19 |
+ //make the tile maps safe to go into a worker thread. |
| 20 |
+ i->second.prepare_for_copy_to_worker_thread(); |
| 21 |
+ } |
| 22 |
+ |
| 23 |
static threading::mutex* sync = new threading::mutex; |
| 24 |
|
| 25 |
#if defined(__ANDROID__) && SDL_VERSION_ATLEAST(1, 3, 0) |
| 26 |
- info.rebuild_tile_thread = new threading::thread("rebuild_tiles", boost::bind(build_tiles_thread_function, &info, tile_maps_, *sync)); |
| 27 |
+ info.rebuild_tile_thread = new threading::thread("rebuild_tiles", boost::bind(build_tiles_thread_function, &info, worker_tile_maps, *sync)); |
| 28 |
#else |
| 29 |
- info.rebuild_tile_thread = new threading::thread(boost::bind(build_tiles_thread_function, &info, tile_maps_, *sync)); |
| 30 |
+ info.rebuild_tile_thread = new threading::thread(boost::bind(build_tiles_thread_function, &info, worker_tile_maps, *sync)); |
| 31 |
#endif |
| 32 |
} |
| 33 |
|
| 34 |
diff --git a/src/tile_map.cpp b/src/tile_map.cpp |
| 35 |
index e71a0373b..83525b4c0 100644 |
| 36 |
--- a/src/tile_map.cpp |
| 37 |
+++ b/src/tile_map.cpp |
| 38 |
@@ -694,6 +694,11 @@ void tile_map::flip_variation(int x, int y, int delta) |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
+void tile_map::prepare_for_copy_to_worker_thread() |
| 43 |
+{ |
| 44 |
+ node_ = variant(); |
| 45 |
+} |
| 46 |
+ |
| 47 |
namespace { |
| 48 |
|
| 49 |
//This function is a random hash. It takes an (x,y) position as well as a |
| 50 |
diff --git a/src/tile_map.hpp b/src/tile_map.hpp |
| 51 |
index e5245f770..d84862414 100644 |
| 52 |
--- a/src/tile_map.hpp |
| 53 |
+++ b/src/tile_map.hpp |
| 54 |
@@ -45,6 +45,10 @@ public: |
| 55 |
int get_variations(int x, int y) const; |
| 56 |
void flip_variation(int x, int y, int delta=0); |
| 57 |
|
| 58 |
+ //variants are not thread-safe, so this function clears out variant |
| 59 |
+ //info to prepare the tile map to be placed into a worker thread. |
| 60 |
+ void prepare_for_copy_to_worker_thread(); |
| 61 |
+ |
| 62 |
#ifndef NO_EDITOR |
| 63 |
//Functions for rebuilding all live tile maps when there is a change |
| 64 |
//to tile map data. prepare_rebuild_all() should be called before |