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 }
//protected override void UnloadData() //{ TurretActions = null; } public static void EnqueueAction(Action item, FastResourceLock lock_MyAPIGateway = null) { using (lock_TurretActions.AcquireExclusiveUsing()) TurretActions.Push(item); using (lock_isRunning.AcquireExclusiveUsing()) { if (isRunning) { return; } isRunning = true; } if (lock_MyAPIGateway != null) { lock_MyAPIGateway.AcquireShared(); } try { MyAPIGateway.Parallel.Start(Run); } finally { if (lock_MyAPIGateway != null) { lock_MyAPIGateway.ReleaseShared(); } } }
/// <summary> /// Gets and loads the texture. /// Texture is unloaded when nobody is using it. /// </summary> /// <typeparam name="T">MyTexture2D or MyTextureCube</typeparam> /// <param name="path">The path.</param> /// <param name="loadedCallback">Callback that is invoked when texture is really loaded and ready for use.</param> /// <param name="loadingMode">The loading mode viz. LoadingMode.</param> /// <param name="flags">The flags.</param> /// <returns></returns> public static T GetTexture <T>(string path, TextureLoadedHandler loadedCallback = null, LoadingMode loadingMode = (MyFakes.LOAD_TEXTURES_IMMEDIATELY ? LoadingMode.Immediate : LoadingMode.Lazy) , TextureFlags flags = TextureFlags.None) where T : MyTexture { if (OverrideLoadingMode != null) { loadingMode = OverrideLoadingMode.Value; } MyTexture texture; TexturesLock.AcquireShared(); if (!m_textures.TryGetValue(path, out texture)) { TexturesLock.ReleaseShared(); return(LoadTexture <T>(path, loadedCallback, loadingMode, flags)); } TexturesLock.ReleaseShared(); DbgWatchLoadedTextures(); return((T)texture); }
protected override void Update() { var networkDict = Windows.GetNetworkConnections(); var preKeyDict = new Dictionary <string, KeyValuePair <int, NetworkConnection> >(); var keyDict = new Dictionary <string, NetworkItem>(); Dictionary <string, NetworkItem> newDict = new Dictionary <string, NetworkItem>(this.Dictionary); // Flattens list, assigns IDs and counts foreach (var list in networkDict.Values) { foreach (var connection in list) { if (connection.Pid == Program.CurrentProcessId && Settings.Instance.HideProcessHackerNetworkConnections) { continue; } string id = connection.Pid.ToString() + "-" + connection.Local.ToString() + "-" + (connection.Remote != null ? connection.Remote.ToString() : "") + "-" + connection.Protocol.ToString(); if (preKeyDict.ContainsKey(id)) { preKeyDict[id] = new KeyValuePair <int, NetworkConnection>( preKeyDict[id].Key + 1, preKeyDict[id].Value); } else { preKeyDict.Add(id, new KeyValuePair <int, NetworkConnection>(1, connection)); } } } // Merges counts into IDs foreach (string s in preKeyDict.Keys) { var connection = preKeyDict[s].Value; NetworkItem item = new NetworkItem { Id = s + "-" + preKeyDict[s].Key.ToString(), Connection = connection }; keyDict.Add(s + "-" + preKeyDict[s].Key.ToString(), item); } foreach (var connection in this.Dictionary.Values) { if (!keyDict.ContainsKey(connection.Id)) { OnDictionaryRemoved(connection); newDict.Remove(connection.Id); } } // Get resolve results. _messageQueue.Listen(); foreach (var connection in keyDict.Values) { if (!this.Dictionary.ContainsKey(connection.Id)) { connection.Tag = this.RunCount; // Resolve the IP addresses. if (connection.Connection.Local != null) { if (!connection.Connection.Local.Address.GetAddressBytes().IsEmpty()) { bool queue = false; // See if IP address is in the cache. _resolveCacheLock.AcquireShared(); try { if (_resolveCache.ContainsKey(connection.Connection.Local.Address)) { // We have the resolved address. connection.LocalString = _resolveCache[connection.Connection.Local.Address]; } else { queue = true; } } finally { _resolveCacheLock.ReleaseShared(); } if (queue) { // Queue for resolve. WorkQueue.GlobalQueueWorkItemTag( new Action <string, bool, IPAddress>(this.ResolveAddresses), "network-resolve-local", connection.Id, false, connection.Connection.Local.Address ); } } } if (connection.Connection.Remote != null) { if (!connection.Connection.Remote.Address.GetAddressBytes().IsEmpty()) { bool queue = false; _resolveCacheLock.AcquireShared(); try { if (_resolveCache.ContainsKey(connection.Connection.Remote.Address)) { // We have the resolved address. connection.RemoteString = _resolveCache[connection.Connection.Remote.Address]; } else { queue = true; } } finally { _resolveCacheLock.ReleaseShared(); } if (queue) { WorkQueue.GlobalQueueWorkItemTag( new Action <string, bool, IPAddress>(this.ResolveAddresses), "network-resolve-remote", connection.Id, true, connection.Connection.Remote.Address ); } } } // Update the dictionary. newDict.Add(connection.Id, connection); OnDictionaryAdded(connection); } else { if ( connection.Connection.State != Dictionary[connection.Id].Connection.State || Dictionary[connection.Id].JustProcessed ) { NetworkItem oldConnection = Dictionary[connection.Id].Clone() as NetworkItem; newDict[connection.Id].Connection.State = connection.Connection.State; newDict[connection.Id].JustProcessed = false; OnDictionaryModified(oldConnection, newDict[connection.Id]); } } } this.Dictionary = newDict; }
public void Dispose() { _lock.ReleaseShared(); _enumerator.Dispose(); }
public void Dispose() { m_lockObject.ReleaseShared(); }