/// <summary> /// Gets the number of resources owned by the specified /// <see cref="Task"/> (if any). /// </summary> /// <param name="task"> /// The <see cref="Task"/> whose resource ownership count will be /// computed. /// </param> /// <returns> /// The number of resource from the pool owned by /// <paramref name="task"/>. If <paramref name="task"/> does not /// own any resources from the pool, the returned value will be zero /// (0). /// </returns> public int OwnershipCount(Task task) { int count; if (IsOwner(task)) { ResourceEntry entry = _owners[task]; count = entry.Count; System.Diagnostics.Debug.Assert(count > 0); } else { count = 0; } return(count); }
public virtual Task Release(Task owner) { if (owner == null) { throw new ArgumentNullException("owner", "cannot be null"); } if (!IsOwner(owner)) { throw new InvalidOperationException( "Task is not a resource owner."); } ResourceEntry entry = _owners[owner]; object item = SelectItemToRelease(owner, entry.Items); return(new ReleaseResource(owner.Simulation, this, item)); }
/// <summary> /// Removes the specified <see cref="Task"/> as a resource item owner. /// </summary> /// <param name="owner"> /// The <see cref="Task"/> that owns <paramref name="item"/>. /// </param> /// <param name="item"> /// A reference to the resource item previously obtained via a call to /// <see cref="AllocateResource"/> (for tracked resources); or /// <see langword="null"/> (for anonymous resources). /// </param> private void ClearOwner(Task owner, object item) { if (owner != null && _owners != null) { ResourceEntry entry = _owners[owner]; if (item == null) { entry.Remove(); } else { entry.Remove(item); } if (entry.Count < 1) { _owners.Remove(owner); } } }
/// <summary> /// Record the given <see cref="Task"/> as a resource item owner. /// </summary> /// <param name="task"> /// The <see cref="Task"/> that owns <paramref name="item"/>. /// </param> /// <param name="item"> /// The reference to a resource item for tracked resources; or /// <see langword="null"/> for anonymous resources. /// </param> internal void SetOwner(Task task, object item) { if (task != null) { ResourceEntry entry; if (_owners == null) { _owners = new Dictionary <Task, ResourceEntry>(); } if (!IsOwner(task)) { if (item == null) { entry = new ResourceEntry(); } else { entry = new ResourceEntry(item); } _owners.Add(task, entry); } else { entry = _owners[task]; if (item == null) { entry.Add(); } else { entry.Add(item); } } } }