public void WaitOne() { _lock.AcquireExclusive(); _waiters = 1; _lock.AcquireExclusive(); _lock.ReleaseExclusive(); }
/// <summary> /// Invokes actions on the game thread, and blocks until completion /// </summary> /// <param name="action"></param> public static void InvokeBlocking(Action action) { var threadLock = new FastResourceLock(); if (!SessionClosing) { ThreadLocks.Add(threadLock); } threadLock.AcquireExclusive(); try { MyAPIGateway.Utilities.InvokeOnGameThread(() => { try { var invokeBlock = Profiler.Start(FullName, nameof(InvokeBlocking)); action(); invokeBlock.End(); } catch (Exception ex) { Logging.Instance.WriteLine("Exception on blocking game thread invocation: " + ex); if (!SessionClosing && ShipyardCore.Debug) { throw; } } finally { threadLock.ReleaseExclusive(); } }); } catch (Exception ex) { Logging.Instance.WriteLine("Exception in Utilities.InvokeBlocking: " + ex); threadLock.ReleaseExclusive(); if (!SessionClosing && ShipyardCore.Debug) { throw; } } threadLock.AcquireExclusive(); threadLock.ReleaseExclusive(); if (!SessionClosing) { ThreadLocks.Remove(threadLock); } }
private void RefreshInternalData() { if (!SandboxGameAssemblyWrapper.Instance.IsGameStarted) { return; } if (WorldManager.Instance.IsWorldSaving) { return; } if (WorldManager.Instance.InternalGetResourceLock() == null) { return; } if (WorldManager.Instance.InternalGetResourceLock().Owned) { return; } if (IsDynamic) { try { //Lock the main data m_resourceLock.AcquireExclusive(); RefreshRawData(); //Lock all of the raw data m_rawDataHashSetResourceLock.AcquireExclusive(); m_rawDataListResourceLock.AcquireExclusive(); m_rawDataObjectBuilderListResourceLock.AcquireExclusive(); //Refresh the main data LoadDynamic(); //Unlock all of the raw data m_rawDataHashSetResourceLock.ReleaseExclusive(); m_rawDataListResourceLock.ReleaseExclusive(); m_rawDataObjectBuilderListResourceLock.ReleaseExclusive(); //Unlock the main data m_resourceLock.ReleaseExclusive(); } catch (Exception ex) { LogManager.ErrorLog.WriteLine(ex); } } }
public HistoryLock(MyProfiler profiler, FastResourceLock historyLock) { m_profiler = profiler; m_lock = historyLock; m_lock.AcquireExclusive(); m_profiler.OnHistorySafe(); }
public void InitFromGrids(MyObjectBuilder_CubeGrid primaryGrid, ICollection <MyObjectBuilder_CubeGrid> allGrids) { try { if (!m_lock.TryAcquireExclusive()) { m_lock.AcquireExclusive(); if (m_initFromGrid) { return; } } // References to BlockInfo aren't threadsafe, so create a new one for this purpose. var blockInfo = new BlockSetInfo(); ComputeBlockMap(primaryGrid, allGrids, blockInfo); ComputeReservedSpace(primaryGrid, allGrids, blockInfo); ComputeMountPoints(primaryGrid, allGrids, blockInfo); blockInfo.UpdateCache(); BlockSetInfo = blockInfo; m_initFromGrid = true; } finally { m_lock.ReleaseExclusive(); } }
public HistoryLock(MyProfiler profiler, FastResourceLock historyLock) { m_profiler = profiler; m_lock = historyLock; m_lock.AcquireExclusive(); m_profiler.OnHistorySafe(); }
/// <summary> /// For logging WARNING and higher severity. /// </summary> /// <param name="level">severity level</param> /// <param name="methodName">calling method</param> /// <param name="toLog">message to log</param> /// <param name="primaryState">class specific, appears before secondary state in log</param> /// <param name="secondaryState">class specific, appears before message in log</param> public void log(severity level, string methodName, string toLog, string primaryState = null, string secondaryState = null) { if (closed) { return; } lock_log.AcquireExclusive(); try { if (logWriter == null) { if (MyAPIGateway.Utilities == null || !createLog()) { return; // cannot log } } if (f_gridName != null) { gridName = f_gridName.Invoke(); } if (primaryState == null) { if (f_state_primary != null) { default_primary = f_state_primary.Invoke(); } primaryState = default_primary; } if (secondaryState == null) { if (f_state_secondary != null) { default_secondary = f_state_secondary.Invoke(); } secondaryState = default_secondary; } if (toLog == null) { toLog = "no message"; } if (numLines >= maxNumLines) { return; } numLines++; appendWithBrackets(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss,fff")); appendWithBrackets(level.ToString()); appendWithBrackets(gridName); appendWithBrackets(className); appendWithBrackets(methodName); appendWithBrackets(primaryState); appendWithBrackets(secondaryState); stringCache.Append(toLog); logWriter.WriteLine(stringCache); logWriter.Flush(); stringCache.Clear(); } catch { } finally { lock_log.ReleaseExclusive(); } }
public static void InvokeOnGameThreadBlocking(Action action, ILogging logger = null) { var mutex = new FastResourceLock(); mutex.AcquireExclusive(); MyAPIGateway.Utilities.InvokeOnGameThread(WrapAction(() => { try { action(); } finally { mutex.ReleaseExclusive(); } }, logger)); mutex.AcquireExclusive(); mutex.ReleaseExclusive(); }
private static int GetStructSize(Type structType) { int size; #if SIZE_CACHE_USE_RESOURCE_LOCK _sizeCacheLock.AcquireShared(); if (_sizeCache.ContainsKey(structType)) { size = _sizeCache[structType]; _sizeCacheLock.ReleaseShared(); } else { _sizeCacheLock.ReleaseShared(); size = Marshal.SizeOf(structType); _sizeCacheLock.AcquireExclusive(); try { if (!_sizeCache.ContainsKey(structType)) { _sizeCache.Add(structType, size); } } finally { _sizeCacheLock.ReleaseExclusive(); } } return(size); #else lock (_sizeCache) { if (_sizeCache.ContainsKey(structType)) { size = _sizeCache[structType]; } else { _sizeCache.Add(structType, size = Marshal.SizeOf(structType)); } return(size); } #endif }
private int SpawnNewBotInternal(MyAgentDefinition agentDefinition, Vector3D? spawnPosition = null, bool createdByPlayer = false) { m_lock.AcquireExclusive(); foreach (var player in Sync.Players.GetOnlinePlayers()) { if (player.Id.SteamId == Sync.MyId && player.Id.SerialId > m_lastBotId) { m_lastBotId = player.Id.SerialId; } } m_lastBotId++; var lastBotId = m_lastBotId; m_lock.ReleaseExclusive(); m_processQueue.Enqueue(new AgentSpawnData(agentDefinition, lastBotId, spawnPosition, createdByPlayer)); return lastBotId; }
/// <summary> /// Closes all existing logs. /// Should be called at the end of each session and when game is closing. /// </summary> public static void Close() { Lock.AcquireExclusive(); foreach (var assembly in ModLogs.Keys) { Log("Closing log", Severity.Level.INFO, "SEPC.Logging", assembly: assembly); } WriteItems(); foreach (ModLog modLog in ModLogs.Values) { modLog.Close(); } LogItems.Clear(); ModLogs.Clear(); StringCache.Clear(); Lock.ReleaseExclusive(); }
private void ResolveAddresses(string id, bool remote, IPAddress address) { string hostName = null; bool inCache = false; // Last minute check of the cache. _resolveCacheLock.AcquireShared(); try { if (_resolveCache.ContainsKey(address)) { hostName = _resolveCache[address]; inCache = true; } } finally { _resolveCacheLock.ReleaseShared(); } // If it wasn't in the cache, resolve the address. if (!inCache) { try { hostName = Dns.GetHostEntry(address).HostName; } catch (SocketException) { // Host was not found. return; } // Update the cache. _resolveCacheLock.AcquireExclusive(); try { // Add the name if not present already. if (!string.IsNullOrEmpty(hostName)) { if (!_resolveCache.ContainsKey(address)) { _resolveCache.Add(address, hostName); } } } finally { _resolveCacheLock.ReleaseExclusive(); } } _messageQueue.Enqueue(new AddressResolveMessage { Id = id, Remote = remote, HostName = hostName }); }
private static void AcquireLock() { ReleaseLock(); _lock?.AcquireExclusive(); _lock?.AcquireExclusive(); }