private static void RegenerateNewRegionsFromDirtyCells2(RegionAndRoomUpdater __instance) { newRegions(__instance).Clear(); //already cleared at end of method TryRebuildDirtyRegionsAndRooms() //List<IntVec3> dirtyCells = map(__instance).regionDirtyer.DirtyCells; Map localMap = map(__instance); RegionDirtyer regionDirtyer = localMap.regionDirtyer; ConcurrentQueue <IntVec3> dirtyCells = RegionDirtyer_Patch.get_DirtyCells(regionDirtyer); //HashSet<IntVec3> oldDirtyCells = getOldDirtyCells(__instance); while (dirtyCells.TryDequeue(out IntVec3 dirtyCell)) { if (dirtyCell.GetRegion(localMap, RegionType.Set_All) == null) { Region region; lock (regionMakerLock) //TODO OPTIMIZE for multithreading { region = localMap.regionMaker.TryGenerateRegionFrom(dirtyCell); } //Region region = regionTryGenerateRegionFrom2(map(__instance).regionMaker, intVec); if (region != null) { //lock (newRegions(__instance)) //{ newRegions(__instance).Add(region); //} } } //oldDirtyCells.Add(dirtyCell); localMap.temperatureCache.ResetCachedCellInfo(dirtyCell); } }
public static bool TryRebuildDirtyRegionsAndRooms(RegionAndRoomUpdater __instance) { if (!__instance.Enabled || working) { return(false); } lock (RegionDirtyer_Patch.regionDirtyerLock) { working = true; if (!__instance.initialized) { __instance.RebuildAllRegionsAndRooms(); } List <IntVec3> dirtyCells = RegionDirtyer_Patch.get_DirtyCells(__instance.map.regionDirtyer); if (dirtyCells.Count == 0) { working = false; return(false); } try { RegenerateNewRegionsFromDirtyCells2(__instance, dirtyCells); __instance.CreateOrUpdateRooms(); } catch (Exception arg) { Log.Error("Exception while rebuilding dirty regions: " + arg); } foreach (IntVec3 dirtyCell in dirtyCells) { __instance.map.temperatureCache.ResetCachedCellInfo(dirtyCell); } dirtyCells.Clear(); foreach (Region region in regionsToReDirty) { RegionDirtyer_Patch.SetRegionDirty(region.Map.regionDirtyer, region); } regionsToReDirty.Clear(); __instance.initialized = true; working = false; //regionCleaning.Set(); if (DebugSettings.detectRegionListersBugs) { Autotests_RegionListers.CheckBugs(__instance.map); } } return(false); }
public static void SetAllClean2(RegionDirtyer __instance) { ConcurrentQueue <IntVec3> dirtyCells = RegionDirtyer_Patch.get_DirtyCells(__instance); while (dirtyCells.TryDequeue(out IntVec3 dirtyCell)) { //IntVec3 dirtyCell; //try //{ //dirtyCell = dirtyCells[i]; //} //catch(ArgumentOutOfRangeException) { break; } maprd(__instance).temperatureCache.ResetCachedCellInfo(dirtyCell); } //dirtyCells.Clear(); dirtyCells = new ConcurrentQueue <IntVec3>(); lock (RegionDirtyer_Patch.dirtyCellsDict) { RegionDirtyer_Patch.dirtyCellsDict.SetOrAdd(__instance, dirtyCells); } }
public static bool TryRebuildDirtyRegionsAndRooms2(RegionAndRoomUpdater __instance) { //if (working || !Enabled) // return; if (!__instance.Enabled) { return(false); } if (getThreadRebuilding()) { return(false); } //working = true; setThreadRebuilding(true); if (!initialized(__instance)) { lock (initializingLock) { if (!initialized(__instance)) { __instance.RebuildAllRegionsAndRooms(); } initialized(__instance) = true; } } if (RegionDirtyer_Patch.get_DirtyCells(map(__instance).regionDirtyer).IsEmpty) { //working = false; setThreadRebuilding(false); return(false); } try { int tid = Thread.CurrentThread.ManagedThreadId; //HashSet<int> gate1ThreadSet = getGate1ThreadSet(__instance); //gate1ThreadSet.Add(tid); Integer gate1Count = getGate1Count(__instance); int gate1Ticket = Interlocked.Increment(ref gate1Count.integer); EventWaitHandle gate1WaitHandle = getGate1WaitHandle(__instance); gate1WaitHandle.WaitOne(); //EventWaitHandle gate2WaitHandle = getGate2WaitHandle(__instance); //gate2WaitHandle.Reset(); if (gate1Ticket == 1) { RegenerateNewRegionsFromDirtyCells2(__instance); CreateOrUpdateRooms2(__instance); if (DebugSettings.detectRegionListersBugs) { Autotests_RegionListers.CheckBugs(map(__instance)); } newRegions(__instance).Clear(); gate1WaitHandle.Reset(); } //HashSet<int> gate2ThreadSet = getGate2ThreadSet(__instance); //gate2ThreadSet.Add(tid); Integer gate2Count = getGate2Count(__instance); Interlocked.Increment(ref gate2Count.integer); int gate1Remaining = Interlocked.Decrement(ref gate1Count.integer); EventWaitHandle gate2WaitHandle = getGate2WaitHandle(__instance); if (gate1Remaining == 0) { //CreateOrUpdateRooms2(__instance); /* * HashSet<IntVec3> oldDirtyCells = getOldDirtyCells(__instance); * foreach(IntVec3 oldDirtyCell in oldDirtyCells) * { * map(__instance).temperatureCache.ResetCachedCellInfo(oldDirtyCell); * } */ //if (DebugSettings.detectRegionListersBugs) //{ //Autotests_RegionListers.CheckBugs(map(__instance)); //} //newRegions(__instance).Clear(); gate2WaitHandle.Set(); } gate2WaitHandle.WaitOne(); int gate2Remaining = Interlocked.Decrement(ref gate2Count.integer); if (gate2Remaining == 0) { gate2WaitHandle.Reset(); gate1WaitHandle.Set(); } } catch (Exception arg) { Log.Error("Exception while rebuilding dirty regions: " + arg); } //newRegions.Clear(); //map.regionDirtyer.SetAllClean(); //initialized = true; //Moved to earlier code above //working = false; setThreadRebuilding(false); return(false); }
public static bool TryRebuildDirtyRegionsAndRooms(RegionAndRoomUpdater __instance) { //todo: optimize lock speedup fix //lock (workingLock) //{ //if (working(__instance) || !__instance.Enabled) if (!__instance.Enabled) { return(false); } if (getThreadRebuilding()) { return(false); } int workerId = Interlocked.Increment(ref workingInt); if (workerId > 1) { regionCleaning.WaitOne(); //Interlocked.Decrement(ref workingInt); return(false); } regionCleaning.Reset(); setThreadRebuilding(true); //working(__instance) = true; if (!initialized(__instance)) { __instance.RebuildAllRegionsAndRooms(); } //if (!map(__instance).regionDirtyer.AnyDirty) if (RegionDirtyer_Patch.get_DirtyCells(map(__instance).regionDirtyer).IsEmpty) { //working(__instance) = false; resumeThreads(); return(false); } try { RegenerateNewRegionsFromDirtyCells2(__instance); //RegenerateNewRegionsFromDirtyCells.Invoke(__instance, new object[] { }); CreateOrUpdateRooms2(__instance); //CreateOrUpdateRooms.Invoke(__instance, new object[] { }); } catch (Exception arg) { Log.Error("Exception while rebuilding dirty regions: " + arg); } newRegions(__instance).Clear(); SetAllClean2(map(__instance).regionDirtyer); //SetAllClean.Invoke(map(__instance).regionDirtyer, new object[] { }); initialized(__instance) = true; //working(__instance) = false; resumeThreads(); //} if (DebugSettings.detectRegionListersBugs) { Autotests_RegionListers.CheckBugs(map(__instance)); } return(false); }