Exemple #1
0
        /// <summary>
        /// Creates a new entry and queues it to this wait queue. If the cancellation token is already canceled, this method immediately returns a canceled task without modifying the wait queue.
        /// </summary>
        /// <param name="this">The wait queue.</param>
        /// <param name="syncObject">A synchronization object taken while cancelling the entry.</param>
        /// <param name="token">The token used to cancel the wait.</param>
        /// <returns>The queued task.</returns>
        public static Task <T> Enqueue <T>(this IAsyncWaitQueue <T> @this, object syncObject, CancellationToken token)
        {
            if (token.IsCancellationRequested)
            {
                return(Task.FromCanceled <T>(token));
            }

            var ret = @this.Enqueue();

            if (!token.CanBeCanceled)
            {
                return(ret);
            }

            var registration = token.Register(() =>
            {
                IDisposable finish;
                lock (syncObject)
                    finish = @this.TryCancel(ret);
                finish.Dispose();
            }, useSynchronizationContext: false);

            ret.ContinueWith(_ => registration.Dispose(), CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);
            return(ret);
        }
Exemple #2
0
        /// <summary>
        ///     Creates a new entry and queues it to this wait queue. If the cancellation token is already canceled, this method
        ///     immediately returns a canceled task without modifying the wait queue.
        /// </summary>
        /// <param name="this">The wait queue.</param>
        /// <param name="mutex">A synchronization object taken while cancelling the entry.</param>
        /// <param name="token">The token used to cancel the wait.</param>
        /// <returns>The queued task.</returns>
        public static Task <T> Enqueue <T>(this IAsyncWaitQueue <T> @this, object mutex, CancellationToken token)
        {
            if (token.IsCancellationRequested)
            {
                return(Task.FromCanceled <T>(token));
            }

            Task <T> ret = @this.Enqueue();

            if (!token.CanBeCanceled)
            {
                return(ret);
            }

            CancellationTokenRegistration registration = token.Register(() =>
            {
                lock (mutex)
                {
                    @this.TryCancel(ret, token);
                }
            },
                                                                        false);

            ret.ContinueWith(_ => registration.Dispose(),
                             CancellationToken.None,
                             TaskContinuationOptions.ExecuteSynchronously,
                             TaskScheduler.Default);
            return(ret);
        }
Exemple #3
0
 /// <summary>
 /// Creates a new async-compatible semaphore with the specified initial count.
 /// </summary>
 /// <param name="initialCount">The initial count for this semaphore. This must be greater than or equal to zero.</param>
 /// <param name="queue">The wait queue used to manage waiters.</param>
 public AsyncSemaphore(int initialCount, IAsyncWaitQueue<object> queue)
 {
     _queue = queue;
     _count = initialCount;
     _mutex = new object();
     //Enlightenment.Trace.AsyncSemaphore_CountChanged(this, initialCount);
 }
Exemple #4
0
 /// <summary>
 /// Creates a new async-compatible semaphore with the specified initial count.
 /// </summary>
 /// <param name="initialCount">The initial count for this semaphore. This must be greater than or equal to zero.</param>
 /// <param name="queue">The wait queue used to manage waiters.</param>
 public AsyncSemaphore(int initialCount, IAsyncWaitQueue <object> queue)
 {
     _queue = queue;
     _count = initialCount;
     _mutex = new object();
     //Enlightenment.Trace.AsyncSemaphore_CountChanged(this, initialCount);
 }
Exemple #5
0
 /// <summary>
 /// Creates an async-compatible auto-reset event.
 /// </summary>
 /// <param name="set">Whether the auto-reset event is initially set or unset.</param>
 /// <param name="queue">The wait queue used to manage waiters.</param>
 public AsyncAutoResetEvent(bool set, IAsyncWaitQueue<object> queue)
 {
     _queue = queue;
     _set = set;
     _mutex = new object();
     //if (set)
     //    Enlightenment.Trace.AsyncAutoResetEvent_Set(this);
 }
 /// <summary>
 /// Creates an async-compatible auto-reset event.
 /// </summary>
 /// <param name="set">Whether the auto-reset event is initially set or unset.</param>
 /// <param name="queue">The wait queue used to manage waiters.</param>
 public AsyncAutoResetEvent(bool set, IAsyncWaitQueue <object> queue)
 {
     _queue = queue;
     _set   = set;
     _mutex = new object();
     //if (set)
     //    Enlightenment.Trace.AsyncAutoResetEvent_Set(this);
 }
 /// <summary>
 /// Creates a new async-compatible reader/writer lock.
 /// </summary>
 public AsyncReaderWriterLock(IAsyncWaitQueue <IDisposable> writerQueue, IAsyncWaitQueue <IDisposable> readerQueue,
                              IAsyncWaitQueue <UpgradeableReaderKey> upgradeableReaderQueue, IAsyncWaitQueue <IDisposable> upgradeReaderQueue)
 {
     _writerQueue            = writerQueue;
     _readerQueue            = readerQueue;
     _upgradeableReaderQueue = upgradeableReaderQueue;
     _upgradeReaderQueue     = upgradeReaderQueue;
     _mutex = new object();
     _cachedReaderKeyTask = TaskShim.FromResult <IDisposable>(new ReaderKey(this));
     _cachedWriterKeyTask = TaskShim.FromResult <IDisposable>(new WriterKey(this));
 }
Exemple #8
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="writers"></param>
 /// <param name="readers"></param>
 /// <param name="upgradeableReaders"></param>
 /// <param name="upgradeReaders"></param>
 public AsyncReaderWriterLock(
     IAsyncWaitQueue <IDisposable> writers,
     IAsyncWaitQueue <IDisposable> readers,
     IAsyncWaitQueue <UpgradeableReaderKey> upgradeableReaders,
     IAsyncWaitQueue <IDisposable> upgradeReaders)
 {
     SyncRoot                = new object();
     cachedReaderKey         = Task.FromResult <IDisposable>(new ReaderKey(this));
     cachedWriterKey         = Task.FromResult <IDisposable>(new WriterKey(this));
     this.writers            = writers;
     this.readers            = readers;
     this.upgradeableReaders = upgradeableReaders;
     this.upgradeReaders     = upgradeReaders;
 }
Exemple #9
0
        /// <summary>
        /// Creates a new entry and queues it to this wait queue. If the cancellation token is already canceled, this method immediately returns a canceled task without modifying the wait queue.
        /// </summary>
        /// <param name="this">The wait queue.</param>
        /// <param name="token">The token used to cancel the wait.</param>
        /// <returns>The queued task.</returns>
        public static Task <T> Enqueue <T>(this IAsyncWaitQueue <T> @this, CancellationToken token)
        {
            if (token.IsCancellationRequested)
            {
                return(TaskConstants <T> .Canceled);
            }

            var ret = @this.Enqueue();

            if (token.CanBeCanceled)
            {
                var registration = token.Register(() => @this.TryCancel(ret).Dispose(), useSynchronizationContext: false);
                ret.ContinueWith(_ => registration.Dispose(), CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);
            }
            return(ret);
        }
Exemple #10
0
 /// <summary>
 /// Constructs a new monitor.
 /// </summary>
 public AsyncMonitor(IAsyncWaitQueue<IDisposable> lockQueue, IAsyncWaitQueue<object> conditionVariableQueue)
 {
     _asyncLock = new AsyncLock(lockQueue);
     _conditionVariable = new AsyncConditionVariable(_asyncLock, conditionVariableQueue);
     //Enlightenment.Trace.AsyncMonitor_Created(_asyncLock, _conditionVariable);
 }
Exemple #11
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="lock"></param>
 /// <param name="queue"></param>
 public AsyncConditionVariable(AsyncLock @lock, IAsyncWaitQueue <object> queue)
 {
     mutex      = new object();
     this.@lock = @lock;
     this.queue = queue;
 }
 /// <summary>
 ///     Creates a new async-compatible semaphore with the specified initial count.
 /// </summary>
 /// <param name="initialCount">The initial count for this semaphore. This must be greater than or equal to zero.</param>
 /// <param name="queue">The wait queue used to manage waiters. This may be <c>null</c> to use a default (FIFO) queue.</param>
 public AsyncSemaphore(long initialCount, IAsyncWaitQueue <object> queue = null)
 {
     this.queue = queue ?? new DefaultAsyncWaitQueue <object>();
     this.count = initialCount;
     this.mutex = new object();
 }
Exemple #13
0
 /// <summary>
 /// Creates a new async-compatible semaphore with the specified initial count.
 /// </summary>
 /// <param name="initialCount">The initial count for this semaphore. This must be greater than or equal to zero.</param>
 /// <param name="queue">The wait queue used to manage waiters.</param>
 private AsyncSemaphore(int initialCount, IAsyncWaitQueue <object> queue)
 {
     _queue = queue;
     _count = initialCount;
     _mutex = new object();
 }
 /// <summary>
 /// Constructs a new monitor.
 /// </summary>
 /// <param name="lockQueue">The wait queue used to manage waiters for the lock. This may be <c>null</c> to use a default (FIFO) queue.</param>
 /// <param name="conditionVariableQueue">The wait queue used to manage waiters for the signal. This may be <c>null</c> to use a default (FIFO) queue.</param>
 internal AsyncMonitor(IAsyncWaitQueue <IDisposable>?lockQueue, IAsyncWaitQueue <object>?conditionVariableQueue)
 {
     _asyncLock         = new AsyncLock(lockQueue);
     _conditionVariable = new AsyncConditionVariable(_asyncLock, conditionVariableQueue);
 }
 /// <summary>
 /// Creates an async-compatible auto-reset event.
 /// </summary>
 /// <param name="set">Whether the auto-reset event is initially set or unset.</param>
 /// <param name="queue">The wait queue used to manage waiters.</param>
 public AsyncAutoResetEvent(bool set, IAsyncWaitQueue <object> queue)
 {
     _queue = queue;
     _set   = set;
     _mutex = new object();
 }
 public RecursiveAsyncLock(IAsyncWaitQueue<IDisposable> queue)
 {
     mutex = new AsyncLock(queue);
 }
 /// <summary>
 /// Creates an async-compatible condition variable associated with an async-compatible lock.
 /// </summary>
 /// <param name="asyncLock">The lock associated with this condition variable.</param>
 /// <param name="queue">The wait queue used to manage waiters.</param>
 public AsyncConditionVariable(AsyncLock asyncLock, IAsyncWaitQueue<object> queue)
 {
     _asyncLock = asyncLock;
     _queue = queue;
     _mutex = new object();
 }
 public RecursiveAsyncLock(IAsyncWaitQueue <IDisposable> queue)
 {
     mutex = new AsyncLock(queue);
 }
 /// <summary>
 /// Creates a new async-compatible reader/writer lock.
 /// </summary>
 /// <param name="writerQueue">The wait queue used to manage waiters for writer locks. This may be <c>null</c> to use a default (FIFO) queue.</param>
 /// <param name="readerQueue">The wait queue used to manage waiters for reader locks. This may be <c>null</c> to use a default (FIFO) queue.</param>
 internal AsyncReaderWriterLock(IAsyncWaitQueue <IDisposable>?writerQueue, IAsyncWaitQueue <IDisposable>?readerQueue)
 {
     _writerQueue = writerQueue ?? new DefaultAsyncWaitQueue <IDisposable>();
     _readerQueue = readerQueue ?? new DefaultAsyncWaitQueue <IDisposable>();
     _mutex       = new object();
 }
 public AsyncAutoResetEvent(bool set, IAsyncWaitQueue <object> queue)
 {
     _queue = (queue ?? new DefaultAsyncWaitQueue <object>());
     _set   = set;
     _mutex = new object();
 }
Exemple #21
0
 /// <summary>
 /// Creates a new async-compatible mutual exclusion lock using the specified wait queue.
 /// </summary>
 /// <param name="queue">The wait queue used to manage waiters.</param>
 public AsyncLock(IAsyncWaitQueue <IDisposable> queue)
 {
     _queue         = queue;
     _cachedKeyTask = TaskShim.FromResult <IDisposable>(new Key(this));
     _mutex         = new object();
 }
 /// <summary>
 /// Creates a new async-compatible mutual exclusion lock using the specified wait queue.
 /// </summary>
 /// <param name="queue">The wait queue used to manage waiters. This may be <c>null</c> to use a default (FIFO) queue.</param>
 public AsyncLock(IAsyncWaitQueue <IDisposable> queue)
 {
     Queue = queue ?? new DefaultAsyncWaitQueue <IDisposable>();
     Mutex = new object();
 }
Exemple #23
0
 /// <summary>
 /// Creates a new async-compatible reader/writer lock.
 /// </summary>
 public AsyncReaderWriterLock(IAsyncWaitQueue<IDisposable> writerQueue, IAsyncWaitQueue<IDisposable> readerQueue, IAsyncWaitQueue<UpgradeableReaderKey> upgradeableReaderQueue, IAsyncWaitQueue<IDisposable> upgradeReaderQueue)
 {
     _writerQueue = writerQueue;
     _readerQueue = readerQueue;
     _upgradeableReaderQueue = upgradeableReaderQueue;
     _upgradeReaderQueue = upgradeReaderQueue;
     _mutex = new object();
     _cachedReaderKeyTask = TaskShim.FromResult<IDisposable>(new ReaderKey(this));
     _cachedWriterKeyTask = TaskShim.FromResult<IDisposable>(new WriterKey(this));
 }
Exemple #24
0
 /// <summary>
 /// Creates an async-compatible condition variable associated with an async-compatible lock.
 /// </summary>
 /// <param name="asyncLock">The lock associated with this condition variable.</param>
 /// <param name="queue">The wait queue used to manage waiters.</param>
 public AsyncConditionVariable(AsyncLock asyncLock, IAsyncWaitQueue <object> queue)
 {
     _asyncLock = asyncLock;
     _queue     = queue;
     _mutex     = new object();
 }
Exemple #25
0
 /// <summary>
 /// Creates a new async-compatible mutual exclusion lock using the specified wait queue.
 /// </summary>
 /// <param name="queue">The wait queue used to manage waiters.</param>
 public AsyncLock(IAsyncWaitQueue <IDisposable> queue)
 {
     this.queue    = queue;
     cachedKeyTask = Task.FromResult((IDisposable) new Key(this));
     mutex         = new object();
 }
 /// <summary>
 /// Creates an async-compatible condition variable associated with an async-compatible lock.
 /// </summary>
 /// <param name="asyncLock">The lock associated with this condition variable.</param>
 /// <param name="queue">The wait queue used to manage waiters. This may be <c>null</c> to use a default (FIFO) queue.</param>
 internal AsyncConditionVariable(AsyncLock asyncLock, IAsyncWaitQueue <object>?queue)
 {
     _asyncLock = asyncLock;
     _queue     = queue ?? new DefaultAsyncWaitQueue <object>();
     _mutex     = new object();
 }
Exemple #27
0
 /// <summary>
 /// Creates a new async-compatible mutual exclusion lock using the specified wait queue.
 /// </summary>
 /// <param name="queue">The wait queue used to manage waiters. This may be <c>null</c> to use a default (FIFO) queue.</param>
 internal AsyncLock(IAsyncWaitQueue <IDisposable> queue)
 {
     _queue = queue ?? new DefaultAsyncWaitQueue <IDisposable>();
     _mutex = new object();
 }
 /// <summary>
 /// Creates a new async-compatible semaphore with the specified initial count.
 /// </summary>
 /// <param name="initialCount">The initial count for this semaphore. This must be greater than or equal to zero.</param>
 /// <param name="queue">The wait queue used to manage waiters. This may be <c>null</c> to use a default (FIFO) queue.</param>
 public AsyncSemaphore(long initialCount, IAsyncWaitQueue <object> queue)
 {
     _queue = queue ?? new DefaultAsyncWaitQueue <object>();
     _count = initialCount;
     _mutex = new object();
 }
 /// <summary>
 /// Creates an async-compatible auto-reset event.
 /// </summary>
 /// <param name="set">Whether the auto-reset event is initially set or unset.</param>
 /// <param name="queue">The wait queue used to manage waiters. This may be <c>null</c> to use a default (FIFO) queue.</param>
 internal AsyncAutoResetEvent(bool set, IAsyncWaitQueue <object>?queue)
 {
     _queue = queue ?? new DefaultAsyncWaitQueue <object>();
     _set   = set;
     _mutex = new object();
 }
Exemple #30
0
 /// <summary>
 /// Creates a new async-compatible mutual exclusion lock using the specified wait queue.
 /// </summary>
 /// <param name="queue">The wait queue used to manage waiters.</param>
 public AsyncLock(IAsyncWaitQueue<IDisposable> queue)
 {
     _queue = queue;
     _cachedKeyTask = TaskShim.FromResult<IDisposable>(new Key(this));
     _mutex = new object();
 }
Exemple #31
0
 /// <summary>
 /// Constructs a new monitor.
 /// </summary>
 public AsyncMonitor(IAsyncWaitQueue <IDisposable> lockQueue, IAsyncWaitQueue <object> conditionVariableQueue)
 {
     _asyncLock         = new AsyncLock(lockQueue);
     _conditionVariable = new AsyncConditionVariable(_asyncLock, conditionVariableQueue);
     //Enlightenment.Trace.AsyncMonitor_Created(_asyncLock, _conditionVariable);
 }
Exemple #32
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="initialCount"></param>
 /// <param name="queue"></param>
 public AsyncSemaphore(int initialCount, IAsyncWaitQueue <object> queue)
 {
     this.queue = queue;
     count      = initialCount;
     mutex      = new object();
 }