public async Task <bool> RemoveStringFromHashField(string hashSet, string key, string value) { var mutex = Semaphores.GetIndexSemaphore(hashSet, key); await mutex.WaitAsync(); try { var field = await GetHashField(hashSet, key); if (!string.IsNullOrEmpty(field)) { field = string.Join(",", field.Split(',').Where(x => x != value)); } if (string.IsNullOrEmpty(field)) { return(await _redisDatabase.HashDeleteAsync(hashSet.ToLower(), key.ToLower())); } return(await SetHashField(hashSet, key, Helpers.SerializeRedisValue(field))); } finally { mutex.Release(); } }
public static async Task <ValueLock> EnterAsync(CachedValue value) { var semaphore = Semaphores.GetOrAdd(value, _ => new SemaphoreSlim(1)); await semaphore.WaitAsync().ConfigureAwait(false); return(new ValueLock(value, semaphore)); }
private async Task CreateEntity(string entityName, IDictionary <string, string> properties) { var mutex = Semaphores.GetEntitySemaphore(entityName); await mutex.WaitAsync(); try { if (await _setClient.SetContains(Constants.AllEntityNamesSetKeyName, entityName)) { throw new EntityAlreadyExistsException(entityName); } foreach (var property in properties) { await _hashClient.SetHashField(Helpers.GetEntityPropertyTypesKey(entityName), property.Key, property.Value); } await _setClient.AddToSet(Constants.AllEntityNamesSetKeyName, entityName); await _stringClient.StoreValue(Helpers.GetEntityCountKey(entityName), 0.ToString()); } finally { mutex.Release(); } }
public async Task <bool> AppendStringToHashField(string hashSet, string key, string value) { var mutex = Semaphores.GetIndexSemaphore(hashSet, key); await mutex.WaitAsync(); try { var field = await GetHashField(hashSet, key); if (string.IsNullOrEmpty(field)) { field = value; } else if (field.Split(',').All(x => x != value)) { field += "," + value; } return(await SetHashField(hashSet, key, Helpers.SerializeRedisValue(field))); } finally { mutex.Release(); } }
/// <summary> /// Sets a string-valued setting. /// </summary> /// <param name="Key">Key name.</param> /// <param name="Value">New value.</param> /// <returns>If the setting was saved (true). If the setting existed, and had the same value, false is returned.</returns> public static async Task <bool> SetAsync(string Key, string Value) { using (Semaphore Semaphore = await Semaphores.BeginWrite("setting:" + Key)) { StringSetting Setting = await Database.FindFirstDeleteRest <StringSetting>(new FilterFieldEqualTo("Key", Key)); if (Setting is null) { Setting = new StringSetting(Key, Value); await Database.Insert(Setting); return(true); } else { if (Setting.Value != Value) { Setting.Value = Value; await Database.Update(Setting); return(true); } else { return(false); } } } }
/// <summary> /// Registers and returns a new ID for an object of the given type. /// </summary> /// <returns>A new unique ID that can be used to identify the object.</returns> public int RegisterSemaphoreID() { if (Semaphores.Any() && _semaphoreID <= Semaphores.Max(e => e.ID)) { _semaphoreID = Semaphores.Max(e => e.ID) + 1; } return(_semaphoreID++); }
/// <summary> /// Sets a string-valued setting. /// </summary> /// <param name="Key">Key name.</param> /// <param name="Value">New value.</param> /// <returns>If the setting was saved (true). If the setting existed, and had the same value, false is returned.</returns> public static async Task <bool> SetAsync(string Key, string Value) { using (Semaphore Semaphore = await Semaphores.BeginWrite("setting:" + Key)) { StringSetting Setting = await Database.FindFirstDeleteRest <StringSetting>(new FilterFieldEqualTo("Key", Key)); return(await SetAsyncLocked(Key, Value, Setting)); } }
private static async Task <T> GetAsync <T>(string Key) where T : class { using (Semaphore Semaphore = await Semaphores.BeginRead("setting:" + Key)) { T Setting = await Database.FindFirstDeleteRest <T>(new FilterFieldEqualTo("Key", Key)); return(Setting); } }
/// <summary> /// 指定したキーで待機します。 /// </summary> /// <param name="key">待機するキー</param> public static void Wait(string key) { lock (LockObject) { if (!Semaphores.ContainsKey(key)) { Semaphores.Add(key, new SemaphoreSlim(1, 1)); } } Semaphores[key].Wait(3000); }
private SemaphoreSlim GetLocker(string connectionString) { SemaphoreSlim locker; if (!Semaphores.TryGetValue(GetPoolName(connectionString), out locker)) { locker = new SemaphoreSlim(PoolSize); Semaphores.TryAdd(GetPoolName(connectionString), locker); } return(locker); }
/// <summary> /// Creates a new semaphore. /// </summary> /// <param name="id">The ID of the semaphore.</param> /// <param name="maximalCount">The maximal number of bots in the managed area.</param> /// <returns>The newly created semaphore.</returns> public QueueSemaphore CreateSemaphore(int id, int maximalCount) { QueueSemaphore semaphore = new QueueSemaphore(this, maximalCount) { ID = id }; Semaphores.Add(semaphore); _idToSemaphore[semaphore.ID] = semaphore; return(semaphore); }
public void Dispose() { lock (Semaphores) { --RefCount; if (RefCount == 0) { Semaphores.Remove(Key); } } Semaphore.Release(); }
/// <summary> /// Deletes a runtime setting /// </summary> /// <param name="Key">Key name.</param> /// <returns>If a setting was found with the given name and deleted.</returns> public static async Task <bool> DeleteAsync(string Key) { using (Semaphore Semaphore = await Semaphores.BeginWrite("setting:" + Key)) { bool Found = false; foreach (Setting Setting in await Database.FindDelete <Setting>(new FilterFieldEqualTo("Key", Key))) { Found = true; } return(Found); } }
public void Dispose() { RefCounted <SemaphoreSlim> item; lock (Semaphores) { item = Semaphores[Key]; --item.RefCount; if (item.RefCount == 0) { Semaphores.Remove(Key); } } item.Value.Release(); }
private async Task CheckEntityExistance(string entityName) { var mutex = Semaphores.GetEntitySemaphore(entityName); await mutex.WaitAsync(); try { if (!await _setClient.SetContains(Constants.AllEntityNamesSetKeyName, entityName)) { throw new EntityNotFoundException(entityName); } } finally { mutex.Release(); } }
/// <summary> /// 指定したキーで待機します。 /// </summary> /// <param name="key">待機するキー</param> public static async Task WaitAsync(string key) { await LockSemaphore.WaitAsync(); try { if (!Semaphores.ContainsKey(key)) { Semaphores.Add(key, new SemaphoreSlim(1, 1)); } } finally { LockSemaphore.Release(); } await Semaphores[key].WaitAsync(); }
public void Dispose() { Holder holder = null; lock (Semaphores) { holder = Semaphores[Key]; --holder.References; if (holder.References == 0) { Semaphores.Remove(Key); } } holder.Semaphore.Release(); }
protected override void DisposeAll() { if (!PoolInitialized) { return; } PoolSuspended = true; foreach (var pool in Pools) { ConnectionStringPoolableBase poolItem; while (pool.Value.TryDequeue(out poolItem)) { poolItem.Dispose(true); } } Pools.Clear(); foreach (var semaphoreSlim in Semaphores) { if (semaphoreSlim.Value.CurrentCount > 0) { try { semaphoreSlim.Value.Release(semaphoreSlim.Value.CurrentCount); } catch { } } try { semaphoreSlim.Value.Dispose(); } catch { } } Semaphores.Clear(); PoolInitialized = false; PoolSuspended = false; }
/// <summary> /// Remove all semaphores and guards from the instance. This is only usable for visualization purposes. /// </summary> public void VisClearSemaphores() { Semaphores.Clear(); }
static void Main(string[] args) { //Client client = new Client(); //client.ShowDialog(); Semaphores.Start(); }
public void Dispose() { this._semaphore.Release(); Semaphores.Remove(this._value, out _); }
/// <summary> /// Sets a object-valued setting. /// </summary> /// <param name="Key">Key name.</param> /// <param name="Value">New value.</param> /// <returns>If the setting was saved (true). If the setting existed, and had the same value, false is returned.</returns> public static async Task <bool> SetAsync(string Key, object Value) { using (Semaphore Semaphore = await Semaphores.BeginWrite("setting:" + Key)) { Setting Setting = await Database.FindFirstDeleteRest <Setting>(new FilterFieldEqualTo("Key", Key)); if (Value is null) { if (!(Setting is ObjectSetting)) { await Database.Delete(Setting); Setting = null; } } else { if (Value is string s) { if (!(Setting is StringSetting StringSetting)) { await Database.Delete(Setting); StringSetting = null; } return(await SetAsyncLocked(Key, s, StringSetting)); } else if (Value is long l) { if (!(Setting is Int64Setting Int64Setting)) { await Database.Delete(Setting); Int64Setting = null; } return(await SetAsyncLocked(Key, l, Int64Setting)); } else if (Value is double d) { if (!(Setting is DoubleSetting DoubleSetting)) { await Database.Delete(Setting); DoubleSetting = null; } return(await SetAsyncLocked(Key, d, DoubleSetting)); } else if (Value is bool b) { if (!(Setting is BooleanSetting BooleanSetting)) { await Database.Delete(Setting); BooleanSetting = null; } return(await SetAsyncLocked(Key, b, BooleanSetting)); } else if (Value is DateTime TP) { if (!(Setting is DateTimeSetting DateTimeSetting)) { await Database.Delete(Setting); DateTimeSetting = null; } return(await SetAsyncLocked(Key, TP, DateTimeSetting)); } else if (Value is TimeSpan TS) { if (!(Setting is TimeSpanSetting TimeSpanSetting)) { await Database.Delete(Setting); TimeSpanSetting = null; } return(await SetAsyncLocked(Key, TS, TimeSpanSetting)); } else { Type T = Value.GetType(); TypeInfo TI = T.GetTypeInfo(); if (!(TI.GetCustomAttribute(typeof(CollectionNameAttribute)) is null)) { throw new InvalidOperationException("Object setting values cannot be stored separate collections. (CollectionName attribute found.)"); } TypeNameAttribute TypeNameAttribute = TI.GetCustomAttribute <TypeNameAttribute>(); if (TypeNameAttribute.TypeNameSerialization != TypeNameSerialization.FullName) { throw new InvalidOperationException("Full Type names must be serialized when persisting object setting values. (TypeName attribute.). Exceptions for the types: Boolean, Int64, String, DateTime, TimeSpan, Double."); } } } if (Setting is ObjectSetting ObjectSetting) { if (((ObjectSetting.Value is null) ^ (Value is null)) || !ObjectSetting.Value.Equals(Value)) { ObjectSetting.Value = Value; await Database.Update(ObjectSetting); return(true); } else { return(false); } }
/// <summary> /// Removes all elements in scope from the instance. /// </summary> /// <param name="theTier">The tier to remove the elements from.</param> /// <param name="xMin">The min x-value of the scope.</param> /// <param name="xMax">The max x-value of the scope.</param> /// <param name="yMin">The min y-value of the scope.</param> /// <param name="yMax">The max y-value of the scope.</param> public void ModRemoveWaypoints(ITierInfo theTier, double xMin, double yMin, double xMax, double yMax) { Tier tier = theTier as Tier; // Remove waypoints List <Waypoint> remWaypoints = Waypoints.Where(w => w.Tier == tier && xMin <= w.X && w.X <= xMax && yMin <= w.Y && w.Y <= yMax).ToList(); // Remove all of them foreach (var waypoint in remWaypoints) { // Remove the waypoint - the waypoint graph handles all cascading path removals waypoint.Tier.RemoveWaypoint(waypoint); // Remove all elements that were connected to the waypoint foreach (var guard in Semaphores.SelectMany(s => s.Guards).Where(guard => guard.From == waypoint || guard.To == waypoint).ToArray()) { guard.Semaphore.UnregisterGuard(guard); if (guard.Semaphore.Guards.Count() == 0) { Semaphores.Remove(guard.Semaphore); } } foreach (var station in InputStations.Where(s => s.Waypoint == waypoint).ToArray()) { station.Tier.RemoveInputStation(station); InputStations.Remove(station); } foreach (var station in OutputStations.Where(s => s.Waypoint == waypoint).ToArray()) { station.Tier.RemoveOutputStation(station); OutputStations.Remove(station); } foreach (var pod in Pods.Where(p => p.Waypoint == waypoint).ToArray()) { pod.Tier.RemovePod(pod); Pods.Remove(pod); } foreach (var elevator in Elevators.Where(e => e.ConnectedPoints.Contains(waypoint))) { elevator.UnregisterPoint(waypoint); if (elevator.ConnectedPoints.Count == 0) { Elevators.Remove(elevator); } } // Make sure it is not on the list of storage locations anymore if (waypoint.PodStorageLocation) { ResourceManager.RemovePodStorageLocation(waypoint); } // Finally remove from the overall waypoint list Waypoints.Remove(waypoint); } // Also remove movables in scope List <Bot> remBots = Bots.Where(b => b.Tier == tier && xMin <= b.X && b.X <= xMax && yMin <= b.Y && b.Y <= yMax).ToList(); foreach (var bot in remBots) { Bots.Remove(bot); bot.Tier.RemoveBot(bot); } List <Pod> remPods = Pods.Where(p => p.Tier == tier && xMin <= p.X && p.X <= xMax && yMin <= p.Y && p.Y <= yMax).ToList(); foreach (var pod in remPods) { Pods.Remove(pod); pod.Tier.RemovePod(pod); } }
public SemaphoreImage(char semaphore) { Semaphore = Semaphores.GetSemaphore(semaphore); ImageSource = Get(Semaphore.Name); }
public ILearner GetLearner(int result) { ILearner learner = null; switch (result) { case 1: learner = new ContextSwitching(1000); break; case 2: learner = new SharedResources(); break; case 3: learner = new LocalMemory(); break; case 4: learner = new ThreadPoolDemo(); break; case 5: learner = new Concepts(); break; case 6: learner = new ExceptionHandling(); break; case 7: learner = new TasksIntro(); break; case 8: learner = new TasksIOBound(); break; case 9: learner = new TasksChaining(); break; case 10: learner = new LocksAndMonitor(); break; case 11: learner = new NestedLocks(); break; case 12: learner = new DeadLocks(); break; case 13: learner = new ReaderWriterLock(); break; case 14: learner = new MutexLock(); break; case 15: learner = new Semaphores(); break; default: learner = new ContextSwitching(10); break; } return(learner); }