示例#1
0
            public static Task <IDisposable> CreateAsync(MyAsyncLock host, int millisecondTimeout)
            {
                var instance = new MyLockInstance(host);

                lock (host)
                {
                    host._locks.Add(instance);

                    // If there aren't any ahead of this
                    if (host._locks.Count == 1)
                    {
                        // Just start it immediately
                        return(Task.FromResult <IDisposable>(instance));
                    }

                    // Otherwise, we need to set up a completion source
                    // which will be triggered after the one before it completes
                    instance._startCompletionSource = new TaskCompletionSource <IDisposable>();

                    if (millisecondTimeout != int.MaxValue)
                    {
                        instance.ConfigureTimeout(millisecondTimeout);
                    }

                    return(instance._startCompletionSource.Task);
                }
            }
示例#2
0
 public Task <IDisposable> LockAsync(int millisecondTimeout = int.MaxValue)
 {
     return(MyLockInstance.CreateAsync(this, millisecondTimeout));
 }