Permalink
Browse files

fixed multi-threaded crash in editor tile rendering logic that could …

…cause intermittent crashes when drawing tiles
  • Loading branch information...
1 parent bbd851f commit 824caddcfdd5f4c7740e77ae7e5b5c46b2b1b47f @davewx7 davewx7 committed Nov 10, 2012
Showing with 18 additions and 2 deletions.
  1. +9 −2 src/level.cpp
  2. +5 −0 src/tile_map.cpp
  3. +4 −0 src/tile_map.hpp
View
@@ -837,12 +837,19 @@ void level::start_rebuild_tiles_in_background(const std::vector<int>& layers)
info.rebuild_tile_layers_worker_buffer = info.rebuild_tile_layers_buffer;
info.rebuild_tile_layers_buffer.clear();
+ std::map<int, tile_map> worker_tile_maps = tile_maps_;
+ for(std::map<int, tile_map>::iterator i = worker_tile_maps.begin();
+ i != worker_tile_maps.end(); ++i) {
+ //make the tile maps safe to go into a worker thread.
+ i->second.prepare_for_copy_to_worker_thread();
+ }
+
static threading::mutex* sync = new threading::mutex;
#if defined(__ANDROID__) && SDL_VERSION_ATLEAST(1, 3, 0)
- info.rebuild_tile_thread = new threading::thread("rebuild_tiles", boost::bind(build_tiles_thread_function, &info, tile_maps_, *sync));
+ info.rebuild_tile_thread = new threading::thread("rebuild_tiles", boost::bind(build_tiles_thread_function, &info, worker_tile_maps, *sync));
#else
- info.rebuild_tile_thread = new threading::thread(boost::bind(build_tiles_thread_function, &info, tile_maps_, *sync));
+ info.rebuild_tile_thread = new threading::thread(boost::bind(build_tiles_thread_function, &info, worker_tile_maps, *sync));
#endif
}
View
@@ -694,6 +694,11 @@ void tile_map::flip_variation(int x, int y, int delta)
}
}
+void tile_map::prepare_for_copy_to_worker_thread()
+{
+ node_ = variant();
+}
+
namespace {
//This function is a random hash. It takes an (x,y) position as well as a
View
@@ -45,6 +45,10 @@ class tile_map : public game_logic::formula_callable {
int get_variations(int x, int y) const;
void flip_variation(int x, int y, int delta=0);
+ //variants are not thread-safe, so this function clears out variant
+ //info to prepare the tile map to be placed into a worker thread.
+ void prepare_for_copy_to_worker_thread();
+
#ifndef NO_EDITOR
//Functions for rebuilding all live tile maps when there is a change
//to tile map data. prepare_rebuild_all() should be called before

0 comments on commit 824cadd

Please sign in to comment.