Example #1
0
        /// <summary>
        /// Attempts to lock the gate. The action is run once the gate becomes free.
        /// Queued actions are run in order.
        /// </summary>
        public virtual void TryLock(IAction onAvailable, Needle needle = null, bool autoUnlock = true)
        {
            // replace action to unlock once task has run
            if (autoUnlock)
            {
                onAvailable = new ActionPair(onAvailable, Release);
            }

            // try get the lock
            if (TryTake)
            {
                // replace needle reference if needed
                if (needle == null)
                {
                    ManagerUpdate.Control.AddSingle(onAvailable);
                }
                else
                {
                    needle.AddSingle(onAvailable);
                }
                return;
            }

            // add to queue
            _queue.Enqueue(new Act(onAvailable));
        }
Example #2
0
        /// <summary>
        /// Attempts to lock the gate. The action is run once the gate becomes free.
        /// Queued actions are run in order.
        /// </summary>
        public override void TryLock(IAction onAvailable, Needle needle = null, bool autoUnlock = true)
        {
            // try get the lock
            if (TryTake)
            {
                // run the on lock method if set
                if (OnLock != null)
                {
                    OnLock.Run();
                }
                if (needle == null)
                {
                    needle = ManagerUpdate.Control;
                }

                // replace action to unlock once task has run
                if (autoUnlock)
                {
                    needle.AddSingle(new ActionPair(onAvailable, Release));
                }
                else
                {
                    // run on lock function
                    needle.AddSingle(onAvailable);
                }

                return;
            }

            if (autoUnlock)
            {
                // set an action to unlock once task has run and add the task to the queue
                _queue.Enqueue(new Act(new ActionPair(onAvailable, Release), needle));
            }
            else
            {
                // add the on available task
                _queue.Enqueue(new Act(onAvailable, needle));
            }
        }
Example #3
0
        //-------------------------------------------//

        /// <summary>
        /// Context for the needle.
        /// </summary>
        public NeedleContext(Needle needle)
        {
            _needle = needle;
        }
Example #4
0
 /// <summary>
 /// Non-blocking lock function. Calls the action when the lock is freed. Optionally do not automatically unlock once the task has run.
 /// </summary>
 public virtual void TryLock(Action onAvailable, Needle needle = null, bool autoUnlock = true)
 {
     TryLock(new ActionSet(onAvailable), needle, autoUnlock);
 }
Example #5
0
 /// <summary>
 /// Add a needle to this handle. The handle will retrieve tasks from the needles it knows about in order
 /// of priority.
 /// </summary>
 public void Add(Needle _needle)
 {
     Needles.Add(_needle);
     Needles.Sort((a, b) => a.Priority < b.Priority);
 }