internal override void DoJob(ref Worker wr, Cont <Unit> uK) { var tI = this.tI; Spin: var state = tI.State; if (state < Delayed) { goto Spin; } if (state != Interlocked.CompareExchange(ref tI.State, ~state, state)) { goto Spin; } if (state > Running) { goto IVarFull; } tI.Value = this.t; tI.State = HasValue; WaitQueue.PickReaders(ref tI.Readers, tI.Value, ref wr); Work.Do(uK, ref wr); return; IVarFull: tI.State = state; uK.DoHandle(ref wr, new Exception("IVar full")); }
// constructors public ExclusiveConnectionPool( ServerId serverId, EndPoint endPoint, ConnectionPoolSettings settings, IConnectionFactory connectionFactory, IEventSubscriber eventSubscriber) { _serverId = Ensure.IsNotNull(serverId, nameof(serverId)); _endPoint = Ensure.IsNotNull(endPoint, nameof(endPoint)); _settings = Ensure.IsNotNull(settings, nameof(settings)); _connectionFactory = Ensure.IsNotNull(connectionFactory, nameof(connectionFactory)); Ensure.IsNotNull(eventSubscriber, nameof(eventSubscriber)); _connectionHolder = new ListConnectionHolder(eventSubscriber); _poolQueue = new WaitQueue(settings.MaxConnections); _waitQueue = new SemaphoreSlim(settings.WaitQueueSize); _maintenanceCancellationTokenSource = new CancellationTokenSource(); _state = new InterlockedInt32(State.Initial); eventSubscriber.TryGetEventHandler(out _checkingOutConnectionEventHandler); eventSubscriber.TryGetEventHandler(out _checkedOutConnectionEventHandler); eventSubscriber.TryGetEventHandler(out _checkingOutConnectionFailedEventHandler); eventSubscriber.TryGetEventHandler(out _checkingInConnectionEventHandler); eventSubscriber.TryGetEventHandler(out _checkedInConnectionEventHandler); eventSubscriber.TryGetEventHandler(out _addingConnectionEventHandler); eventSubscriber.TryGetEventHandler(out _addedConnectionEventHandler); eventSubscriber.TryGetEventHandler(out _openingEventHandler); eventSubscriber.TryGetEventHandler(out _openedEventHandler); eventSubscriber.TryGetEventHandler(out _closingEventHandler); eventSubscriber.TryGetEventHandler(out _closedEventHandler); eventSubscriber.TryGetEventHandler(out _addingConnectionEventHandler); eventSubscriber.TryGetEventHandler(out _addedConnectionEventHandler); }
internal override void DoHandle(ref Worker wr, Exception e) { var tP = this.tP as Promise <T>; Spin: var state = tP.State; if (state != Running) { goto Spin; } if (Running != Interlocked.CompareExchange(ref tP.State, state - MakeLocked, state)) { goto Spin; } var readers = tP.Readers; tP.Readers = new Fail <T>(e); tP.State = HasExn; WaitQueue.FailReaders(readers, e, ref wr); var reader = this.reader; if (null == reader) { return; } wr.Handler = reader; reader.DoHandle(ref wr, e); }
internal override void DoJob(ref Worker wr, Cont <Y> yK) { var tM = this.tM; Spin: var state = tM.State; if (state == MVar.Locked) { goto Spin; } if (state != Interlocked.CompareExchange(ref tM.State, MVar.Locked, state)) { goto Spin; } if (state <= MVar.Empty) { goto EmptyOrDemand; } var t = tM.Value; tM.State = MVar.Empty; MVarTryModifyFunCont <T, Y> .Do(this, yK, ref wr, t); return; EmptyOrDemand: WaitQueue.AddTaker(ref tM.Takers, new MVarTryModifyFunCont <T, Y>(this, yK)); tM.State = MVar.Demand; }
internal override void DoJob(ref Worker wr, Cont <Unit> uK) { var tI = this.tI; Spin: var state = tI.State; if (state < Delayed) { goto Spin; } if (state != Interlocked.CompareExchange(ref tI.State, ~state, state)) { goto Spin; } if (state > Running) { goto IVarFull; } var readers = tI.Readers; var e = this.e; tI.Readers = new Fail <T>(e); tI.State = HasExn; WaitQueue.FailReaders(readers, e, ref wr); Work.Do(uK, ref wr); return; IVarFull: uK.DoHandle(ref wr, new Exception("IVar full")); }
internal override void DoJob(ref Worker wr, Cont <Unit> uK) { Spin: var state = this.State; if (state > Running) { goto Terminated; } if (state < Running) { goto Spin; } if (state != Interlocked.CompareExchange(ref this.State, Locked, state)) { goto Spin; } WaitQueue.AddTaker(ref this.Joiners, uK); this.State = Running; return; Terminated: Work.Do(uK, ref wr); }
public void IsEmpty_WithTwoItems_IsFalse() { var queue = new WaitQueue<int>(); queue.Enqueue(item0, none).WaitEx(); queue.Enqueue(item1, none).WaitEx(); Assert.IsFalse(queue.IsEmpty(none).ResultEx()); }
internal override void DoJob(ref Worker wr, Cont <Unit> uK) { var tI = this.tI; tI.Value = this.t; // This assumes correct usage of IVar. Spin: var state = tI.State; if (state < Delayed) { goto Spin; } if (state != Interlocked.CompareExchange(ref tI.State, HasValue, state)) { goto Spin; } if (state > Running) { uK.DoHandle(ref wr, new Exception("IVar full")); } else { WaitQueue.PickReaders(ref tI.Readers, tI.Value, ref wr); Work.Do(uK, ref wr); } }
internal override void DoJob(ref Worker wr, Cont <Unit> uK) { var tI = this.tI; Spin: var state = tI.State; if (state < Delayed) { goto Spin; } if (state > Running) { goto Done; } if (state != Interlocked.CompareExchange(ref tI.State, ~state, state)) { goto Spin; } tI.Value = this.t; tI.State = HasValue; WaitQueue.PickReaders(ref tI.Readers, tI.Value, ref wr); Done: Work.Do(uK, ref wr); }
// Note that via selective communication it is possible for a job to offer // to both give and take on the same channel simultaneously. So, both // Givers and Takers queues must be maintained even though in many cases // only one of them is non empty. internal override void DoJob(ref Worker wr, Cont <T> xK) { TryNextGiver: this.Lock.Enter(); var tail = this.Givers; if (null != tail) { goto TryGiver; } WaitQueue.AddTaker(ref this.Takers, xK); this.Lock.Exit(); return; TryGiver: var cursor = tail.Next; if (tail == cursor) { this.Givers = null; } else { tail.Next = cursor.Next; } this.Lock.Exit(); var giver = cursor as Giver <T>; if (null == giver) { goto GotSend; } var pkOther = giver.Pick; if (null == pkOther) { goto GotGiver; } TryPickOther: var stOther = Pick.TryPick(pkOther); if (stOther > 0) { goto TryNextGiver; } if (stOther < 0) { goto TryPickOther; } Pick.SetNacks(ref wr, giver.Me, pkOther); GotGiver: Worker.Push(ref wr, giver.Cont); GotSend: Cont.Do(xK, ref wr, cursor.Value); return; }
internal override void DoJob(ref Worker wr, Cont <Unit> uK) { var ch = this.Ch; TryNextTaker: ch.Lock.Enter(); var tail = ch.Takers; if (null == tail) { goto TryGiver; } var cursor = tail.Next; if (tail == cursor) { ch.Takers = null; ch.Lock.Exit(); } else { tail.Next = cursor.Next; ch.Lock.Exit(); tail = cursor as Cont <T>; } var taker = tail as Taker <T>; if (null == taker) { goto GotTaker; } var pkOther = taker.Pick; TryPickOther: var stOther = Pick.TryPick(pkOther); if (stOther > 0) { goto TryNextTaker; } if (stOther < 0) { goto TryPickOther; } Pick.SetNacks(ref wr, taker.Me, pkOther); tail = taker.Cont; GotTaker: tail.Value = this.X; Worker.Push(ref wr, tail); Work.Do(uK, ref wr); return; TryGiver: WaitQueue.AddGiver(ref ch.Givers, this.X, uK); ch.Lock.Exit(); return; }
internal override void DoJob(ref Worker wr, Cont <Unit> uK) { Spin: if (0 == this.Count) { goto Done; } var state = this.State; if (state < 0) { goto Spin; } if (Interlocked.Exchange(ref this.State, ~state) < state) { goto Spin; } if (0 == this.Count) { goto UnlockAndDone; } WaitQueue.AddTaker(ref this.Awaiters, uK); this.State = 0; return; UnlockAndDone: this.State = 0; Done: Work.Do(uK, ref wr); }
public void ConcurrentAddAndTestTest() { int size = 1000; int[] correct = new int[size]; var queue = new WaitQueue<int>(); ThreadPool.QueueUserWorkItem( x => { foreach (var i in Range(size).AsParallel()) { queue.Add(i); } }); foreach (var i in Range(size).AsParallel()) { var result = queue.Take(); correct[result]++; } foreach (var i in Range(size)) { Assert.AreEqual(1, correct[i], "Invalid value at" + i + " = " + correct[i]); } }
internal override void DoJob(ref Worker wr, Cont <T> tK) { var tM = this.tM; Spin: var state = tM.State; if (state == MVar.Locked) { goto Spin; } if (state != Interlocked.CompareExchange(ref tM.State, MVar.Locked, state)) { goto Spin; } if (state <= MVar.Empty) { goto EmptyOrDemand; } var t = tM.Value; tM.State = MVar.Full; Cont.Do(tK, ref wr, t); return; EmptyOrDemand: WaitQueue.AddTaker(ref tM.Takers, new MVarReadCont <T>(tM, tK)); tM.State = MVar.Demand; }
public void ConcurrentAddAndTestTest() { int size = 1000; int[] correct = new int[size]; var queue = new WaitQueue <int>(); ThreadPool.QueueUserWorkItem( x => { foreach (var i in Range(size).AsParallel()) { queue.Add(i); } }); foreach (var i in Range(size).AsParallel()) { var result = queue.Take(); correct[result]++; } foreach (var i in Range(size)) { Assert.AreEqual(1, correct[i], "Invalid value at" + i + " = " + correct[i]); } }
// constructors public ExclusiveConnectionPool( ServerId serverId, EndPoint endPoint, ConnectionPoolSettings settings, IConnectionFactory connectionFactory, IEventSubscriber eventSubscriber) { _serverId = Ensure.IsNotNull(serverId, nameof(serverId)); _endPoint = Ensure.IsNotNull(endPoint, nameof(endPoint)); _settings = Ensure.IsNotNull(settings, nameof(settings)); _connectionFactory = Ensure.IsNotNull(connectionFactory, nameof(connectionFactory)); Ensure.IsNotNull(eventSubscriber, nameof(eventSubscriber)); _connectionHolder = new ListConnectionHolder(eventSubscriber); _poolQueue = new WaitQueue(settings.MaxConnections); _waitQueue = new SemaphoreSlim(settings.WaitQueueSize); _maintenanceCancellationTokenSource = new CancellationTokenSource(); _state = new InterlockedInt32(State.Initial); eventSubscriber.TryGetEventHandler(out _checkingOutConnectionEventHandler); eventSubscriber.TryGetEventHandler(out _checkedOutConnectionEventHandler); eventSubscriber.TryGetEventHandler(out _checkingOutConnectionFailedEventHandler); eventSubscriber.TryGetEventHandler(out _checkingInConnectionEventHandler); eventSubscriber.TryGetEventHandler(out _checkedInConnectionEventHandler); eventSubscriber.TryGetEventHandler(out _addingConnectionEventHandler); eventSubscriber.TryGetEventHandler(out _addedConnectionEventHandler); eventSubscriber.TryGetEventHandler(out _openingEventHandler); eventSubscriber.TryGetEventHandler(out _openedEventHandler); eventSubscriber.TryGetEventHandler(out _closingEventHandler); eventSubscriber.TryGetEventHandler(out _closedEventHandler); eventSubscriber.TryGetEventHandler(out _addingConnectionEventHandler); eventSubscriber.TryGetEventHandler(out _addedConnectionEventHandler); }
internal override void DoJob(ref Worker wr, Cont <T> aK) { Spin: var state = this.State; if (state == Locked) { goto Spin; } if (state != Interlocked.CompareExchange(ref this.State, Locked, state)) { goto Spin; } if (state <= Empty) { goto EmptyOrDemand; } T value = this.Value; this.Value = default(T); // Avoid memory leaks. this.State = Empty; Cont.Do(aK, ref wr, value); return; EmptyOrDemand: WaitQueue.AddTaker(ref this.Takers, aK); this.State = Demand; }
/// Internal implementation detail. internal override void DoJob(ref Worker wr, Cont <T> aK) { Spin: var state = this.State; if (state > Empty) { goto GotValue; } if (state < Empty) { goto Spin; } if (Empty != Interlocked.CompareExchange(ref this.State, Locked, Empty)) { goto Spin; } WaitQueue.AddTaker(ref this.Takers, aK); this.State = Empty; return; GotValue: aK.DoCont(ref wr, this.Value); return; }
internal override void DoJob(ref Worker wr, Cont <Unit> uK) { var tI = this.tI; Spin: var state = tI.State; if (state < Delayed) { goto Spin; } if (state > Running) { goto Done; } if (state != Interlocked.CompareExchange(ref tI.State, ~state, state)) { goto Spin; } var readers = tI.Readers; var e = this.e; tI.Readers = new Fail <T>(e); tI.State = HasExn; WaitQueue.FailReaders(readers, e, ref wr); Done: Work.Do(uK, ref wr); }
internal override void TryAlt(ref Worker wr, int i, Cont<Unit> uK, Else aE) { var pkSelf = aE.pk; Spin: if (0 == this.Count) goto TryPick; var state = this.State; if (state < 0) goto Spin; if (Interlocked.Exchange(ref this.State, ~state) < state) goto Spin; if (0 == this.Count) goto UnlockAndTryPick; WaitQueue.AddTaker(ref this.Awaiters, i, pkSelf, uK); this.State = 0; aE.TryElse(ref wr, i + 1); return; UnlockAndTryPick: this.State = 0; TryPick: var st = Pick.TryPick(pkSelf); if (st > 0) goto AlreadyPicked; if (st < 0) goto TryPick; Pick.SetNacks(ref wr, i, pkSelf); Work.Do(uK, ref wr); AlreadyPicked: return; }
/// <summary> /// Unblock, but do not resume, the specified <see cref="Task"/>. /// </summary> /// <remarks> /// <para> /// This method is used to remove <paramref name="task"/> from the /// <see cref="Task"/> instance's wait list without resuming the /// execution of <paramref name="task"/>. The most common use for /// invoking <see cref="Unblock"/> is to stop a <see cref="Task"/> /// from waiting after it has been resumed by another means (e.g. a /// different simulation object has resumed <paramref name="task"/>). /// </para> /// <para> /// Again, it's very important to realize that <see cref="Unblock"/> /// does <b>not</b> activate <paramref name="task"/>. /// </para> /// <para> /// This method does nothing if <paramref name="task"/> equals /// <c>this</c> or is <see langword="null"/>. /// </para> /// </remarks> /// <param name="task"> /// The <see cref="Task"/> which will stop blocking on this /// <see cref="Task"/> instance. /// </param> public virtual void Unblock(Task task) { if (task != null && task != this && BlockCount > 0) { WaitQueue.Remove(task); task.UpdateBlockingLinks(this, false); } }
/// <summary> /// Brings buyers and sellers together. /// </summary> static ChatManager() { // This object is used for multithreaded coordination. ChatManager.syncRoot = new Object(); // This queue is filled up with Working Orders that need to be serviced because something changed the matching criteria. ChatManager.actionQueue = new WaitQueue <ObjectAction>(); }
/// <summary> /// Unblock, but do not resume, the specified <see cref="DesTask"/>. /// </summary> /// <remarks> /// <para> /// This method is used to remove <paramref name="desTask"/> from the /// <see cref="DesTask"/> instance's wait list without resuming the /// execution of <paramref name="desTask"/>. The most common use for /// invoking <see cref="Unblock"/> is to stop a <see cref="DesTask"/> /// from waiting after it has been resumed by another means (e.g. a /// different simulation object has resumed <paramref name="desTask"/>). /// </para> /// <para> /// Again, it's very important to realize that <see cref="Unblock"/> /// does <b>not</b> activate <paramref name="desTask"/>. /// </para> /// <para> /// This method does nothing if <paramref name="desTask"/> equals /// <c>this</c> or is <see langword="null"/>. /// </para> /// </remarks> /// <param name="desTask"> /// The <see cref="DesTask"/> which will stop blocking on this /// <see cref="DesTask"/> instance. /// </param> public virtual void Unblock(DesTask desTask) { if (desTask != null && desTask != this && BlockCount > 0) { WaitQueue.Remove(desTask); desTask.UpdateBlockingLinks(this, false); } }
internal static void Send(Ch <T> ch, ref Worker wr, T x) { TryNextTaker: ch.Lock.Enter(); var tail = ch.Takers; if (null == tail) { goto TryGiver; } var cursor = tail.Next; if (tail == cursor) { ch.Takers = null; ch.Lock.Exit(); } else { tail.Next = cursor.Next; ch.Lock.Exit(); tail = cursor as Cont <T>; } var taker = tail as Taker <T>; if (null == taker) { goto GotTaker; } var pkOther = taker.Pick; TryPickOther: var stOther = Pick.TryPick(pkOther); if (stOther > 0) { goto TryNextTaker; } if (stOther < 0) { goto TryPickOther; } Pick.SetNacks(ref wr, taker.Me, pkOther); tail = taker.Cont; GotTaker: tail.Value = x; Worker.Push(ref wr, tail); return; TryGiver: WaitQueue.AddSend(ref ch.Givers, x); ch.Lock.Exit(); return; }
internal override void DoJob(ref Worker wr, Cont<Unit> uK) { var latch = this.latch; var n = Interlocked.Decrement(ref latch.Count); if (0 == n) { latch.Lock(); WaitQueue.PickReaders(ref latch.Awaiters, null, ref wr); } Work.Do(uK, ref wr); }
internal override void DoJob(ref Worker wr, Cont <T> aK) { Spin: var state = this.State; Reconsider: if (state > Running) { goto Completed; } if (state < Delayed) { goto Spin; } var check = state; state = Interlocked.CompareExchange(ref this.State, state - MakeLocked, state); if (Delayed == state) { goto Delayed; } if (state != check) { goto Reconsider; } WaitQueue.AddTaker(ref this.Readers, aK); this.State = Running; return; Delayed: var readers = this.Readers; this.Readers = null; this.State = Running; var fulfill = readers as Fulfill; fulfill.tP = this; fulfill.reader = aK; var tJ = fulfill.tJ; fulfill.tJ = null; Job.Do(tJ, ref wr, fulfill); return; Completed: if (state == HasValue) { Cont.Do(aK, ref wr, this.Value); } else { aK.DoHandle(ref wr, (this.Readers as Fail <T>).exn); } }
public void Dequeue_WithResult_CompletesWithResult() { var queue = new WaitQueue<int>(); var task = queue.Enqueue(item0, none); Test.Async(async () => { var result0 = await queue.Dequeue(none); Assert.AreEqual<int>(item0, result0); }); }
// // Constructors. // public StArrayBlockingQueue(int capacity, bool lifoQueue) { if (capacity <= 0) { throw new ArgumentOutOfRangeException("capacity"); } items = new T[length = capacity]; qlock = new SpinLock(LOCK_SPIN_CYCLES); this.lifoQueue = lifoQueue; waitQueue = new WaitQueue(); }
/// <summary> /// Brings buyers and sellers of equities together. /// </summary> static SettlementDocumentFactory() { try { // This is normally created by a channel when it establishes a connection. But this is a daemon process without a connection, so an // authorization policy needs be created explicilty in order to have a thread that can run as an authorized user. SettlementDocumentFactory.authorizationPolicy = new AuthorizationPolicy(); // This queue is filled up with Working Orders that need to be serviced because something changed the matching criteria. SettlementDocumentFactory.actionQueue = new WaitQueue <ObjectAction>(); // This identity and set of claims gives the worker threads access to do anything to the data model. List <Claim> listClaims = new List <Claim>(); listClaims.Add(new Claim(FluidTrade.Core.ClaimTypes.Create, Resources.Application, Rights.PossessProperty)); listClaims.Add(new Claim(FluidTrade.Core.ClaimTypes.Update, Resources.Application, Rights.PossessProperty)); listClaims.Add(new Claim(FluidTrade.Core.ClaimTypes.Read, Resources.Application, Rights.PossessProperty)); listClaims.Add(new Claim(FluidTrade.Core.ClaimTypes.Destroy, Resources.Application, Rights.PossessProperty)); ClaimSet adminClaims = new DefaultClaimSet(ClaimsAuthorizationPolicy.IssuerClaimSet, listClaims.ToArray()); SettlementDocumentFactory.claimsPrincipal = new ClaimsPrincipal( new GenericIdentity("*****@*****.**"), adminClaims); // This will create an instance of the Mail Merge factory that takes templates and mail merge fields and generates a complete document. It is // loaded up dymanically so that servers that don't have Microsoft Office installed on them will be able to run, but will not be able to // generate settlement letters. SettlementDocumentFactory.iMailMerge = new MailMerge(); if (SettlementDocumentFactory.iMailMerge == null) { EventLog.Information("This server is unable to process Mail Merge functions."); } else { // These business rules will move negotiation information across the Chinese wall if a counter party exists. DataModel.ConsumerDebtSettlement.ConsumerDebtSettlementRowChanged += OnConsumerDebtSettlementRowChanged; // This thread will execution the actions that are created by changes to the data model. The triggers themselves can't modify the data // model because the triggers are called from the commit handlers. SettlementDocumentFactory.factoryThread = new Thread(new ThreadStart(SettlementDocumentFactory.FactoryThread)); SettlementDocumentFactory.factoryThread.Name = "Crossing Thread"; SettlementDocumentFactory.factoryThread.IsBackground = true; SettlementDocumentFactory.factoryThread.Start(); } } catch (Exception exception) { if (exception.InnerException != null) { EventLog.Error("{0}, {1}", exception.InnerException.Message, exception.InnerException.StackTrace); } EventLog.Error("{0}, {1}", exception.Message, exception.StackTrace); } }
internal override void TryAlt(ref Worker wr, int i, Cont <Y> yK, Else tE) { var tM = this.tM; var pkSelf = tE.pk; Spin: var state = tM.State; if (state == MVar.Locked) { goto Spin; } if (state != Interlocked.CompareExchange(ref tM.State, MVar.Locked, state)) { goto Spin; } if (state <= MVar.Empty) { goto EmptyOrDemand; } TryPick: var stSelf = Pick.TryPick(pkSelf); if (stSelf > 0) { goto AlreadyPicked; } if (stSelf < 0) { goto TryPick; } Pick.SetNacks(ref wr, i, pkSelf); var t = tM.Value; tM.State = MVar.Empty; MVarTryModifyFunCont <T, Y> .Do(this, yK, ref wr, t); return; AlreadyPicked: tM.State = MVar.Full; return; EmptyOrDemand: WaitQueue.AddTaker(ref tM.Takers, i, pkSelf, new MVarTryModifyFunCont <T, Y>(this, yK)); tM.State = MVar.Demand; tE.TryElse(ref wr, i + 1); return; }
private void InitOperators(WaitQueue <SocketAsyncEventArgs> queue, int queueCount, Action <SocketAsyncEventArgs> success, Action <SocketAsyncEventArgs> fail, Action <SocketAsyncEventArgs> fina) { if (TokenSource == null) { TokenSource = new CancellationTokenSource(); } if (queue == null) { queue = new WaitQueue <SocketAsyncEventArgs>(queueCount, Token); } Parallel.For(0, queueCount, x => queue.Enqueue(InitEventArgs(success, fail, fina))); }
public void Dequeue_WaitWithEnqCompletes_CompletesTask() { var queue = new WaitQueue<int>(); var task0 = queue.Dequeue(none); Assert.IsFalse(task0.IsCompleted); var task1 = queue.Enqueue(item0, none); task1.WaitEx(); task0.WaitEx(); Assert.IsTrue(task1.IsCompleted); Assert.IsTrue(task0.IsCompleted); Assert.AreEqual<int>(item0, task0.ResultEx()); }
internal override void TryAlt(ref Worker wr, int i, Cont <T> aK, Else aE) { var pkSelf = aE.pk; Spin: var state = this.State; if (state == Locked) { goto Spin; } if (state != Interlocked.CompareExchange(ref this.State, Locked, state)) { goto Spin; } if (state <= Empty) { goto EmptyOrDemand; } TryPick: var stSelf = Pick.TryPick(pkSelf); if (stSelf > 0) { goto AlreadyPicked; } if (stSelf < 0) { goto TryPick; } Pick.SetNacks(ref wr, i, pkSelf); T value = this.Value; this.Value = default(T); this.State = Empty; Cont.Do(aK, ref wr, value); return; AlreadyPicked: this.State = Full; return; EmptyOrDemand: WaitQueue.AddTaker(ref this.Takers, i, pkSelf, aK); this.State = Demand; aE.TryElse(ref wr, i + 1); return; }
public WriteStream(Stream stream, int bufferSize, BufferManager bufferManager) { _stream = stream; _bufferSize = bufferSize; _bufferManager = bufferManager; _queue = new WaitQueue <ArraySegment <byte> >(Math.Max(8, _bufferSize / QueueStream.BlockSize)); _watchThread = new Thread(this.WatchThread); _watchThread.Priority = ThreadPriority.BelowNormal; _watchThread.Name = "QueueStream_WatchThread"; _watchThread.Start(); }
/// <summary> /// Block the specified <see cref="DesTask"/> instance. /// </summary> /// <exception cref="ArgumentNullException"> /// If <paramref name="desTask"/> is <see langword="null"/>. /// </exception> /// <exception cref="ArgumentException"> /// If <paramref name="desTask"/> attempts to block itself. For /// example, if code like <c>this.Block(this);</c> is executed. /// </exception> /// <param name="desTask"> /// The <see cref="DesTask"/> to block. /// </param> public virtual void Block(DesTask desTask) { if (desTask == null) { throw new ArgumentNullException("'DesTask' cannot be null."); } if (desTask == this) { throw new ArgumentException("DesTask cannot block on itself."); } desTask.UpdateBlockingLinks(this, true); WaitQueue.Enqueue(desTask); }
public void Dequeue_WithTwoItems_OnlyCompletesFirstItem() { Test.Async(async () => { var queue = new WaitQueue<int>(); var task1 = queue.Enqueue(item0, none); var task2 = queue.Enqueue(item1, none); await task1; var deq0 = await queue.Dequeue(none); Assert.AreEqual<int>(item0, deq0); await task2; Assert.AreEqual<int>(item1, await queue.Peek(none)); }); }
// constructors public ExclusiveConnectionPool( ServerId serverId, EndPoint endPoint, ConnectionPoolSettings settings, IConnectionFactory connectionFactory, IConnectionPoolListener listener) { _serverId = Ensure.IsNotNull(serverId, "serverId"); _endPoint = Ensure.IsNotNull(endPoint, "endPoint"); _settings = Ensure.IsNotNull(settings, "settings"); _connectionFactory = Ensure.IsNotNull(connectionFactory, "connectionFactory"); _listener = listener; _connectionHolder = new ListConnectionHolder(_listener); _poolQueue = new WaitQueue(settings.MaxConnections); _waitQueue = new SemaphoreSlim(settings.WaitQueueSize); _maintenanceCancellationTokenSource = new CancellationTokenSource(); _state = new InterlockedInt32(State.Initial); }
public void Cancelled_BeforeDequeue_Faults() { var queue = new WaitQueue<int>(); var cts = new CancellationTokenSource(); cts.Cancel(); var task = queue.Dequeue(cts.Token); AssertEx.ThrowsExceptionAsync<OperationCanceledException>(task).GetAwaiter().GetResult(); Assert.IsTrue(task.IsCanceled); Assert.IsTrue(queue.IsEmpty(none).ResultEx()); }
public void Test_ConnectionManager() { for (int i = 0; i < 4; i++) { TcpListener listener = new TcpListener(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 60000)); listener.Start(); var listenerAcceptSocket = listener.BeginAcceptSocket(null, null); TcpClient client = new TcpClient(); client.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 60000)); var server = listener.EndAcceptSocket(listenerAcceptSocket); listener.Stop(); var tcpClient = new BaseConnection(new SocketCap(client.Client), null, Test_Library_Net_Amoeba.MaxReceiveCount, _bufferManager); var tcpServer = new BaseConnection(new SocketCap(server), null, Test_Library_Net_Amoeba.MaxReceiveCount, _bufferManager); List<ConnectionManager> connectionManagers = new List<ConnectionManager>(); { ConnectionManager serverConnectionManager; ConnectionManager clientConnectionManager; Node serverNode = null; Node clientNode = null; byte[] serverSessionId = null; byte[] clientSessionId = null; { var id = new byte[32]; _random.NextBytes(id); var uris = new string[] { "tcp:localhost:9000", "tcp:localhost:9001", "tcp:localhost:9002" }; serverNode = new Node(id, uris); } { var id = new byte[32]; _random.NextBytes(id); var uris = new string[] { "tcp:localhost:9000", "tcp:localhost:9001", "tcp:localhost:9002" }; clientNode = new Node(id, uris); } { serverSessionId = new byte[32]; _random.NextBytes(serverSessionId); } { clientSessionId = new byte[32]; _random.NextBytes(clientSessionId); } serverConnectionManager = new ConnectionManager(tcpServer, serverSessionId, serverNode, ConnectDirection.In, _bufferManager); clientConnectionManager = new ConnectionManager(tcpClient, clientSessionId, clientNode, ConnectDirection.Out, _bufferManager); var serverTask = Task.Run(() => serverConnectionManager.Connect()); var clientTask = Task.Run(() => clientConnectionManager.Connect()); Task.WaitAll(serverTask, clientTask); Assert.IsTrue(CollectionUtils.Equals(serverConnectionManager.SesstionId, clientSessionId), "ConnectionManager SessionId #1"); Assert.IsTrue(CollectionUtils.Equals(clientConnectionManager.SesstionId, serverSessionId), "ConnectionManager SessionId #2"); Assert.AreEqual(serverConnectionManager.Node, clientNode, "ConnectionManager Node #1"); Assert.AreEqual(clientConnectionManager.Node, serverNode, "ConnectionManager Node #2"); connectionManagers.Add(serverConnectionManager); connectionManagers.Add(clientConnectionManager); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullNodesEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullNodesEvent += (object sender, PullNodesEventArgs e) => { queue.Enqueue(e); }; List<Node> nodes = new List<Node>(); for (int j = 0; j < 32; j++) { Node node = null; { var id = new byte[32]; _random.NextBytes(id); var uris = new string[] { "net.tcp://localhost:9000", "net.tcp://localhost:9001", "net.tcp://localhost:9002" }; node = new Node(id, uris); } nodes.Add(node); } senderConnection.PushNodes(nodes); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtils.Equals(nodes, item.Nodes), "ConnectionManager #1"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullBlocksLinkEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBlocksLinkEvent += (object sender, PullBlocksLinkEventArgs e) => { queue.Enqueue(e); }; var keys = new List<Key>(); for (int j = 0; j < 32; j++) { Key key = null; { var id = new byte[32]; _random.NextBytes(id); key = new Key(HashAlgorithm.Sha256, id); } keys.Add(key); } senderConnection.PushBlocksLink(keys); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtils.Equals(keys, item.Keys), "ConnectionManager #2"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullBlocksRequestEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBlocksRequestEvent += (object sender, PullBlocksRequestEventArgs e) => { queue.Enqueue(e); }; var keys = new List<Key>(); for (int j = 0; j < 32; j++) { Key key = null; { var id = new byte[32]; _random.NextBytes(id); key = new Key(HashAlgorithm.Sha256, id); } keys.Add(key); } senderConnection.PushBlocksRequest(keys); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtils.Equals(keys, item.Keys), "ConnectionManager #3"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullBlockEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBlockEvent += (object sender, PullBlockEventArgs e) => { queue.Enqueue(e); }; var buffer = _bufferManager.TakeBuffer(1024 * 1024 * 8); var key = new Key(HashAlgorithm.Sha256, Sha256.ComputeHash(buffer)); senderConnection.PushBlock(key, new ArraySegment<byte>(buffer, 0, 1024 * 1024 * 4)); var item = queue.Dequeue(); Assert.AreEqual(key, item.Key, "ConnectionManager #4.1"); Assert.IsTrue(CollectionUtils.Equals(buffer, 0, item.Value.Array, item.Value.Offset, 1024 * 1024 * 4), "ConnectionManager #4.2"); _bufferManager.ReturnBuffer(buffer); _bufferManager.ReturnBuffer(item.Value.Array); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullBroadcastMetadatasRequestEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBroadcastMetadatasRequestEvent += (object sender, PullBroadcastMetadatasRequestEventArgs e) => { queue.Enqueue(e); }; var digitalSignature = new DigitalSignature("123", DigitalSignatureAlgorithm.Rsa2048_Sha256); var signatures = new SignatureCollection(); for (int j = 0; j < 32; j++) { signatures.Add(digitalSignature.ToString()); } senderConnection.PushBroadcastMetadatasRequest(signatures); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtils.Equals(signatures, item.Signatures), "ConnectionManager #5.1"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullBroadcastMetadatasEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBroadcastMetadatasEvent += (object sender, PullBroadcastMetadatasEventArgs e) => { queue.Enqueue(e); }; var digitalSignature = new DigitalSignature("123", DigitalSignatureAlgorithm.Rsa2048_Sha256); var metadatas1 = new List<BroadcastMetadata>(); for (int j = 0; j < 4; j++) { var key = new Key(HashAlgorithm.Sha256, new byte[32]); var metadata = new Metadata(1, key, CompressionAlgorithm.Xz, CryptoAlgorithm.Aes256, new byte[32 + 32]); var broadcastMetadata = new BroadcastMetadata("Type", DateTime.UtcNow, metadata, digitalSignature); metadatas1.Add(broadcastMetadata); } senderConnection.PushBroadcastMetadatas(metadatas1); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtils.Equals(metadatas1, item.BroadcastMetadatas), "ConnectionManager #6.1"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullUnicastMetadatasRequestEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullUnicastMetadatasRequestEvent += (object sender, PullUnicastMetadatasRequestEventArgs e) => { queue.Enqueue(e); }; var digitalSignature = new DigitalSignature("123", DigitalSignatureAlgorithm.Rsa2048_Sha256); var signatures = new SignatureCollection(); for (int j = 0; j < 32; j++) { signatures.Add(digitalSignature.ToString()); } senderConnection.PushUnicastMetadatasRequest(signatures); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtils.Equals(signatures, item.Signatures), "ConnectionManager #7.1"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullUnicastMetadatasEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullUnicastMetadatasEvent += (object sender, PullUnicastMetadatasEventArgs e) => { queue.Enqueue(e); }; var digitalSignature = new DigitalSignature("123", DigitalSignatureAlgorithm.Rsa2048_Sha256); var metadatas1 = new List<UnicastMetadata>(); for (int j = 0; j < 4; j++) { var key = new Key(HashAlgorithm.Sha256, new byte[32]); var metadata = new Metadata(1, key, CompressionAlgorithm.Xz, CryptoAlgorithm.Aes256, new byte[32 + 32]); var unicastMetadata = new UnicastMetadata("Type", digitalSignature.ToString(), DateTime.UtcNow, metadata, digitalSignature); metadatas1.Add(unicastMetadata); } senderConnection.PushUnicastMetadatas(metadatas1); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtils.Equals(metadatas1, item.UnicastMetadatas), "ConnectionManager #8.1"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullMulticastMetadatasRequestEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullMulticastMetadatasRequestEvent += (object sender, PullMulticastMetadatasRequestEventArgs e) => { queue.Enqueue(e); }; var tags = new TagCollection(); for (int j = 0; j < 32; j++) { var id = new byte[32]; _random.NextBytes(id); tags.Add(new Tag(RandomString.GetValue(256), id)); } senderConnection.PushMulticastMetadatasRequest(tags); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtils.Equals(tags, item.Tags), "ConnectionManager #9.1"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullMulticastMetadatasEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullMulticastMetadatasEvent += (object sender, PullMulticastMetadatasEventArgs e) => { queue.Enqueue(e); }; var digitalSignature = new DigitalSignature("123", DigitalSignatureAlgorithm.Rsa2048_Sha256); var metadatas1 = new List<MulticastMetadata>(); for (int j = 0; j < 4; j++) { var key = new Key(HashAlgorithm.Sha256, new byte[32]); var metadata = new Metadata(1, key, CompressionAlgorithm.Xz, CryptoAlgorithm.Aes256, new byte[32 + 32]); var tag = new Tag("oooo", new byte[32]); var miner = new Miner(CashAlgorithm.Version1, -1, TimeSpan.Zero); var multicastMetadata = new MulticastMetadata("Type", tag, DateTime.UtcNow, metadata, miner, digitalSignature); metadatas1.Add(multicastMetadata); } senderConnection.PushMulticastMetadatas(metadatas1); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtils.Equals(metadatas1, item.MulticastMetadatas), "ConnectionManager #10.1"); } foreach (var connectionManager in connectionManagers) { connectionManager.Dispose(); } client.Close(); server.Close(); } }
private void InitBlock() { maxFieldLength = IndexWriter.DEFAULT_MAX_FIELD_LENGTH; maxBufferedDeleteTerms = IndexWriter.DEFAULT_MAX_BUFFERED_DELETE_TERMS; ramBufferSize = (long) (IndexWriter.DEFAULT_RAM_BUFFER_SIZE_MB * 1024 * 1024); waitQueuePauseBytes = (long) (ramBufferSize * 0.1); waitQueueResumeBytes = (long) (ramBufferSize * 0.05); freeTrigger = (long) (IndexWriter.DEFAULT_RAM_BUFFER_SIZE_MB * 1024 * 1024 * 1.05); freeLevel = (long) (IndexWriter.DEFAULT_RAM_BUFFER_SIZE_MB * 1024 * 1024 * 0.95); maxBufferedDocs = IndexWriter.DEFAULT_MAX_BUFFERED_DOCS; skipDocWriter = new SkipDocWriter(); byteBlockAllocator = new ByteBlockAllocator(this, DocumentsWriter.BYTE_BLOCK_SIZE); perDocAllocator = new ByteBlockAllocator(this,DocumentsWriter.PER_DOC_BLOCK_SIZE); waitQueue = new WaitQueue(this); }
public void Cancelled_Dequeue() { var queue = new WaitQueue<int>(); var cts = new CancellationTokenSource(); var task0 = queue.Dequeue(cts.Token); Task.Run(async () => { // wait until dequeue request gets on waitlist while (queue.WaitCount == 0) await Task.Delay(10); Assert.AreEqual<int>(1, queue.WaitCount); }).GetAwaiter().GetResult(); cts.Cancel(); AssertEx.ThrowsExceptionAsync<OperationCanceledException>(task0).GetAwaiter().GetResult(); var isEmpty = queue.IsEmpty(none).GetAwaiter().GetResult(); Assert.IsTrue(isEmpty); }
public void Cancelled_Enqueue() { var wq = new WaitQueue<int>(); var queue = wq; var cts = new CancellationTokenSource(); Test.Async(async () => { // gets internal releaser for wait queue var releaser = (IDisposable)await wq.Lock.Wait(none); var task = queue.Enqueue(item0, cts.Token); // will block waiting for releaser Assert.IsFalse(task.IsCompleted); cts.Cancel(); // now cancel enqueue while it's blocked await AssertEx.ThrowsExceptionAsync<OperationCanceledException>(task); // clear the internal lock releaser.Dispose(); Assert.IsTrue(await queue.IsEmpty(none)); Assert.IsTrue(task.IsCanceled); }); }
public void IsEmpty_WhenEmpty_IsTrue() { var queue = new WaitQueue<int>(); Assert.IsTrue(queue.IsEmpty(none).ResultEx()); }
public void Test_ConnectionManager() { for (int i = 0; i < 4; i++) { TcpListener listener = new TcpListener(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 60000)); listener.Start(); var listenerAcceptSocket = listener.BeginAcceptSocket(null, null); TcpClient client = new TcpClient(); client.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 60000)); var server = listener.EndAcceptSocket(listenerAcceptSocket); listener.Stop(); var tcpClient = new BaseConnection(new SocketCap(client.Client), null, Test_Library_Net_Amoeba.MaxReceiveCount, _bufferManager); var tcpServer = new BaseConnection(new SocketCap(server), null, Test_Library_Net_Amoeba.MaxReceiveCount, _bufferManager); List<ConnectionManager> connectionManagers = new List<ConnectionManager>(); { ConnectionManager serverConnectionManager; ConnectionManager clientConnectionManager; Node serverNode = null; Node clientNode = null; byte[] serverSessionId = null; byte[] clientSessionId = null; { var id = new byte[32]; _random.NextBytes(id); var uris = new string[] { "net.tcp://localhost:9000", "net.tcp://localhost:9001", "net.tcp://localhost:9002" }; serverNode = new Node(id, uris); } { var id = new byte[32]; _random.NextBytes(id); var uris = new string[] { "net.tcp://localhost:9000", "net.tcp://localhost:9001", "net.tcp://localhost:9002" }; clientNode = new Node(id, uris); } { serverSessionId = new byte[32]; _random.NextBytes(serverSessionId); } { clientSessionId = new byte[32]; _random.NextBytes(clientSessionId); } serverConnectionManager = new ConnectionManager(tcpServer, serverSessionId, serverNode, ConnectDirection.In, _bufferManager); clientConnectionManager = new ConnectionManager(tcpClient, clientSessionId, clientNode, ConnectDirection.Out, _bufferManager); Thread serverThread = new Thread(new ThreadStart(() => { serverConnectionManager.Connect(); })); Thread clientThread = new Thread(new ThreadStart(() => { clientConnectionManager.Connect(); })); serverThread.Start(); clientThread.Start(); serverThread.Join(); clientThread.Join(); Assert.IsTrue(CollectionUtilities.Equals(serverConnectionManager.SesstionId, clientSessionId), "ConnectionManager SessionId #1"); Assert.IsTrue(CollectionUtilities.Equals(clientConnectionManager.SesstionId, serverSessionId), "ConnectionManager SessionId #2"); Assert.AreEqual(serverConnectionManager.Node, clientNode, "ConnectionManager Node #1"); Assert.AreEqual(clientConnectionManager.Node, serverNode, "ConnectionManager Node #2"); connectionManagers.Add(serverConnectionManager); connectionManagers.Add(clientConnectionManager); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullNodesEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullNodesEvent += (object sender, PullNodesEventArgs e) => { queue.Enqueue(e); }; List<Node> nodes = new List<Node>(); for (int j = 0; j < 32; j++) { Node node = null; { var id = new byte[32]; _random.NextBytes(id); var uris = new string[] { "net.tcp://localhost:9000", "net.tcp://localhost:9001", "net.tcp://localhost:9002" }; node = new Node(id, uris); } nodes.Add(node); } senderConnection.PushNodes(nodes); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(nodes, item.Nodes), "ConnectionManager #1"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullBlocksLinkEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBlocksLinkEvent += (object sender, PullBlocksLinkEventArgs e) => { queue.Enqueue(e); }; var keys = new List<Key>(); for (int j = 0; j < 32; j++) { Key key = null; { var id = new byte[32]; _random.NextBytes(id); key = new Key(id, HashAlgorithm.Sha256); } keys.Add(key); } senderConnection.PushBlocksLink(keys); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(keys, item.Keys), "ConnectionManager #2"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullBlocksRequestEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBlocksRequestEvent += (object sender, PullBlocksRequestEventArgs e) => { queue.Enqueue(e); }; var keys = new List<Key>(); for (int j = 0; j < 32; j++) { Key key = null; { var id = new byte[32]; _random.NextBytes(id); key = new Key(id, HashAlgorithm.Sha256); } keys.Add(key); } senderConnection.PushBlocksRequest(keys); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(keys, item.Keys), "ConnectionManager #3"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullBlockEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBlockEvent += (object sender, PullBlockEventArgs e) => { queue.Enqueue(e); }; var buffer = _bufferManager.TakeBuffer(1024 * 1024 * 8); var key = new Key(Sha256.ComputeHash(buffer), HashAlgorithm.Sha256); senderConnection.PushBlock(key, new ArraySegment<byte>(buffer, 0, 1024 * 1024 * 4)); var item = queue.Dequeue(); Assert.AreEqual(key, item.Key, "ConnectionManager #4.1"); Assert.IsTrue(CollectionUtilities.Equals(buffer, 0, item.Value.Array, item.Value.Offset, 1024 * 1024 * 4), "ConnectionManager #4.2"); _bufferManager.ReturnBuffer(buffer); _bufferManager.ReturnBuffer(item.Value.Array); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullSeedsRequestEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullSeedsRequestEvent += (object sender, PullSeedsRequestEventArgs e) => { queue.Enqueue(e); }; var digitalSignature = new DigitalSignature("123", DigitalSignatureAlgorithm.EcDsaP521_Sha256); var signatures = new SignatureCollection(); for (int j = 0; j < 32; j++) { signatures.Add(digitalSignature.ToString()); } senderConnection.PushSeedsRequest(signatures); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(signatures, item.Signatures), "ConnectionManager #5"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullSeedsEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullSeedsEvent += (object sender, PullSeedsEventArgs e) => { queue.Enqueue(e); }; List<Seed> seeds = new List<Seed>(); for (int j = 0; j < 32; j++) { var seed = new Seed(); seed.Name = "aaaa.zip"; seed.Keywords.AddRange(new KeywordCollection { "bbbb", "cccc", "dddd", }); seed.CreationTime = DateTime.Now; seed.Length = 10000; seed.Comment = "eeee"; seed.Rank = 1; seed.Key = new Key(new byte[32], HashAlgorithm.Sha256); seed.CompressionAlgorithm = CompressionAlgorithm.Xz; seed.CryptoAlgorithm = CryptoAlgorithm.Aes256; seed.CryptoKey = new byte[32 + 32]; seeds.Add(seed); } senderConnection.PushSeeds(seeds); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(seeds, item.Seeds), "ConnectionManager #6"); } foreach (var connectionManager in connectionManagers) { connectionManager.Dispose(); } client.Close(); server.Close(); } }
internal QueuedSemaphore(WaitQueue q, long initialPermits) : base(initialPermits) { wq_ = q; }
public void Test_ConnectionManager() { for (int i = 0; i < 4; i++) { TcpListener listener = new TcpListener(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 60000)); listener.Start(); var listenerAcceptSocket = listener.BeginAcceptSocket(null, null); TcpClient client = new TcpClient(); client.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 60000)); var server = listener.EndAcceptSocket(listenerAcceptSocket); listener.Stop(); var tcpClient = new BaseConnection(new SocketCap(client.Client), null, Test_Library_Net_Outopos.MaxReceiveCount, _bufferManager); var tcpServer = new BaseConnection(new SocketCap(server), null, Test_Library_Net_Outopos.MaxReceiveCount, _bufferManager); List<ConnectionManager> connectionManagers = new List<ConnectionManager>(); { ConnectionManager serverConnectionManager; ConnectionManager clientConnectionManager; Node serverNode = null; Node clientNode = null; byte[] serverSessionId = null; byte[] clientSessionId = null; { var id = new byte[32]; _random.NextBytes(id); var uris = new string[] { "net.tcp://localhost:9000", "net.tcp://localhost:9001", "net.tcp://localhost:9002" }; serverNode = new Node(id, uris); } { var id = new byte[32]; _random.NextBytes(id); var uris = new string[] { "net.tcp://localhost:9000", "net.tcp://localhost:9001", "net.tcp://localhost:9002" }; clientNode = new Node(id, uris); } { serverSessionId = new byte[32]; _random.NextBytes(serverSessionId); } { clientSessionId = new byte[32]; _random.NextBytes(clientSessionId); } serverConnectionManager = new ConnectionManager(tcpServer, serverSessionId, serverNode, ConnectDirection.In, _bufferManager); clientConnectionManager = new ConnectionManager(tcpClient, clientSessionId, clientNode, ConnectDirection.Out, _bufferManager); Thread serverThread = new Thread(new ThreadStart(() => { serverConnectionManager.Connect(); })); Thread clientThread = new Thread(new ThreadStart(() => { clientConnectionManager.Connect(); })); serverThread.Start(); clientThread.Start(); serverThread.Join(); clientThread.Join(); Assert.IsTrue(CollectionUtilities.Equals(serverConnectionManager.SesstionId, clientSessionId), "ConnectionManager SessionId #1"); Assert.IsTrue(CollectionUtilities.Equals(clientConnectionManager.SesstionId, serverSessionId), "ConnectionManager SessionId #2"); Assert.AreEqual(serverConnectionManager.Node, clientNode, "ConnectionManager Node #1"); Assert.AreEqual(clientConnectionManager.Node, serverNode, "ConnectionManager Node #2"); connectionManagers.Add(serverConnectionManager); connectionManagers.Add(clientConnectionManager); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullNodesEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullNodesEvent += (object sender, PullNodesEventArgs e) => { queue.Enqueue(e); }; List<Node> nodes = new List<Node>(); for (int j = 0; j < 32; j++) { Node node = null; { var id = new byte[32]; _random.NextBytes(id); var uris = new string[] { "net.tcp://localhost:9000", "net.tcp://localhost:9001", "net.tcp://localhost:9002" }; node = new Node(id, uris); } nodes.Add(node); } senderConnection.PushNodes(nodes); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(nodes, item.Nodes), "ConnectionManager #1"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullBlocksLinkEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBlocksLinkEvent += (object sender, PullBlocksLinkEventArgs e) => { queue.Enqueue(e); }; var keys = new List<Key>(); for (int j = 0; j < 32; j++) { Key key = null; { var id = new byte[32]; _random.NextBytes(id); key = new Key(id, HashAlgorithm.Sha256); } keys.Add(key); } senderConnection.PushBlocksLink(keys); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(keys, item.Keys), "ConnectionManager #2"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullBlocksRequestEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBlocksRequestEvent += (object sender, PullBlocksRequestEventArgs e) => { queue.Enqueue(e); }; var keys = new List<Key>(); for (int j = 0; j < 32; j++) { Key key = null; { var id = new byte[32]; _random.NextBytes(id); key = new Key(id, HashAlgorithm.Sha256); } keys.Add(key); } senderConnection.PushBlocksRequest(keys); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(keys, item.Keys), "ConnectionManager #3"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullBlockEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBlockEvent += (object sender, PullBlockEventArgs e) => { queue.Enqueue(e); }; var buffer = _bufferManager.TakeBuffer(1024 * 1024 * 8); var key = new Key(Sha256.ComputeHash(buffer), HashAlgorithm.Sha256); senderConnection.PushBlock(key, new ArraySegment<byte>(buffer, 0, 1024 * 1024 * 4)); var item = queue.Dequeue(); Assert.AreEqual(key, item.Key, "ConnectionManager #4.1"); Assert.IsTrue(CollectionUtilities.Equals(buffer, 0, item.Value.Array, item.Value.Offset, 1024 * 1024 * 4), "ConnectionManager #4.2"); _bufferManager.ReturnBuffer(buffer); _bufferManager.ReturnBuffer(item.Value.Array); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullBroadcastMetadatasRequestEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBroadcastMetadatasRequestEvent += (object sender, PullBroadcastMetadatasRequestEventArgs e) => { queue.Enqueue(e); }; var digitalSignature = new DigitalSignature("123", DigitalSignatureAlgorithm.EcDsaP521_Sha256); var signatures = new SignatureCollection(); for (int j = 0; j < 32; j++) { signatures.Add(digitalSignature.ToString()); } senderConnection.PushBroadcastMetadatasRequest(signatures); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(signatures, item.Signatures), "ConnectionManager #5.1"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullBroadcastMetadatasEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullBroadcastMetadatasEvent += (object sender, PullBroadcastMetadatasEventArgs e) => { queue.Enqueue(e); }; var digitalSignature = new DigitalSignature("123", DigitalSignatureAlgorithm.EcDsaP521_Sha256); var metadatas1 = new List<ProfileMetadata>(); for (int j = 0; j < 4; j++) { var id = new byte[32]; _random.NextBytes(id); var key = new Key(id, HashAlgorithm.Sha256); var metadata = new ProfileMetadata(DateTime.UtcNow, key, digitalSignature); metadatas1.Add(metadata); } senderConnection.PushBroadcastMetadatas(metadatas1); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(metadatas1, item.ProfileMetadatas), "ConnectionManager #6.1"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullUnicastMetadatasRequestEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullUnicastMetadatasRequestEvent += (object sender, PullUnicastMetadatasRequestEventArgs e) => { queue.Enqueue(e); }; var digitalSignature = new DigitalSignature("123", DigitalSignatureAlgorithm.EcDsaP521_Sha256); var signatures = new SignatureCollection(); for (int j = 0; j < 32; j++) { signatures.Add(digitalSignature.ToString()); } senderConnection.PushUnicastMetadatasRequest(signatures); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(signatures, item.Signatures), "ConnectionManager #7.1"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullUnicastMetadatasEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullUnicastMetadatasEvent += (object sender, PullUnicastMetadatasEventArgs e) => { queue.Enqueue(e); }; var digitalSignature = new DigitalSignature("123", DigitalSignatureAlgorithm.EcDsaP521_Sha256); var metadatas1 = new List<SignatureMessageMetadata>(); for (int j = 0; j < 4; j++) { var id = new byte[32]; _random.NextBytes(id); var key = new Key(id, HashAlgorithm.Sha256); var miner = new Miner(CashAlgorithm.Version1, -1, TimeSpan.Zero); var metadata = new SignatureMessageMetadata(digitalSignature.ToString(), DateTime.UtcNow, key, miner, digitalSignature); metadatas1.Add(metadata); } senderConnection.PushUnicastMetadatas(metadatas1); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(metadatas1, item.SignatureMessageMetadatas), "ConnectionManager #8.1"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullMulticastMetadatasRequestEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullMulticastMetadatasRequestEvent += (object sender, PullMulticastMetadatasRequestEventArgs e) => { queue.Enqueue(e); }; var wikis = new WikiCollection(); var chats = new ChatCollection(); for (int j = 0; j < 32; j++) { var id = new byte[32]; _random.NextBytes(id); var key = new Key(id, HashAlgorithm.Sha256); wikis.Add(new Wiki(RandomString.GetValue(256), id)); } for (int j = 0; j < 32; j++) { var id = new byte[32]; _random.NextBytes(id); var key = new Key(id, HashAlgorithm.Sha256); chats.Add(new Chat(RandomString.GetValue(256), id)); } senderConnection.PushMulticastMetadatasRequest(wikis, chats); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(wikis, item.Wikis), "ConnectionManager #9.1"); Assert.IsTrue(CollectionUtilities.Equals(chats, item.Chats), "ConnectionManager #9.2"); } connectionManagers.Randomize(); { var queue = new WaitQueue<PullMulticastMetadatasEventArgs>(); var receiverConnection = connectionManagers[0]; var senderConnection = connectionManagers[1]; receiverConnection.PullMulticastMetadatasEvent += (object sender, PullMulticastMetadatasEventArgs e) => { queue.Enqueue(e); }; var digitalSignature = new DigitalSignature("123", DigitalSignatureAlgorithm.EcDsaP521_Sha256); var metadatas1 = new List<WikiDocumentMetadata>(); var metadatas2 = new List<ChatTopicMetadata>(); var metadatas3 = new List<ChatMessageMetadata>(); for (int j = 0; j < 4; j++) { var id = new byte[32]; _random.NextBytes(id); var key = new Key(id, HashAlgorithm.Sha256); var tag = new Wiki("oooo", new byte[32]); var miner = new Miner(CashAlgorithm.Version1, -1, TimeSpan.Zero); var metadata = new WikiDocumentMetadata(tag, DateTime.UtcNow, key, miner, digitalSignature); metadatas1.Add(metadata); } for (int j = 0; j < 4; j++) { var id = new byte[32]; _random.NextBytes(id); var key = new Key(id, HashAlgorithm.Sha256); var tag = new Chat("oooo", new byte[32]); var miner = new Miner(CashAlgorithm.Version1, -1, TimeSpan.Zero); var metadata = new ChatTopicMetadata(tag, DateTime.UtcNow, key, miner, digitalSignature); metadatas2.Add(metadata); } for (int j = 0; j < 4; j++) { var id = new byte[32]; _random.NextBytes(id); var key = new Key(id, HashAlgorithm.Sha256); var tag = new Chat("oooo", new byte[32]); var miner = new Miner(CashAlgorithm.Version1, -1, TimeSpan.Zero); var metadata = new ChatMessageMetadata(tag, DateTime.UtcNow, key, miner, digitalSignature); metadatas3.Add(metadata); } senderConnection.PushMulticastMetadatas(metadatas1, metadatas2, metadatas3); var item = queue.Dequeue(); Assert.IsTrue(CollectionUtilities.Equals(metadatas1, item.WikiDocumentMetadatas), "ConnectionManager #10.1"); Assert.IsTrue(CollectionUtilities.Equals(metadatas2, item.ChatTopicMetadatas), "ConnectionManager #10.2"); Assert.IsTrue(CollectionUtilities.Equals(metadatas3, item.ChatMessageMetadatas), "ConnectionManager #10.3"); } foreach (var connectionManager in connectionManagers) { connectionManager.Dispose(); } client.Close(); server.Close(); } }
internal DocumentsWriter(Directory directory, IndexWriter writer) { this.directory = directory; this.writer = writer; this.similarity = writer.GetSimilarity(); flushedDocCount = writer.MaxDoc(); byteBlockAllocator = new ByteBlockAllocator(this); waitQueue = new WaitQueue(this); /* This is the current indexing chain: DocConsumer / DocConsumerPerThread --> code: DocFieldProcessor / DocFieldProcessorPerThread --> DocFieldConsumer / DocFieldConsumerPerThread / DocFieldConsumerPerField --> code: DocFieldConsumers / DocFieldConsumersPerThread / DocFieldConsumersPerField --> code: DocInverter / DocInverterPerThread / DocInverterPerField --> InvertedDocConsumer / InvertedDocConsumerPerThread / InvertedDocConsumerPerField --> code: TermsHash / TermsHashPerThread / TermsHashPerField --> TermsHashConsumer / TermsHashConsumerPerThread / TermsHashConsumerPerField --> code: FreqProxTermsWriter / FreqProxTermsWriterPerThread / FreqProxTermsWriterPerField --> code: TermVectorsTermsWriter / TermVectorsTermsWriterPerThread / TermVectorsTermsWriterPerField --> InvertedDocEndConsumer / InvertedDocConsumerPerThread / InvertedDocConsumerPerField --> code: NormsWriter / NormsWriterPerThread / NormsWriterPerField --> code: StoredFieldsWriter / StoredFieldsWriterPerThread / StoredFieldsWriterPerField */ // TODO FI: this should be something the user can pass in // Build up indexing chain: TermsHashConsumer termVectorsWriter = new TermVectorsTermsWriter(this); TermsHashConsumer freqProxWriter = new FreqProxTermsWriter(); InvertedDocConsumer termsHash = new TermsHash(this, true, freqProxWriter, new TermsHash(this, false, termVectorsWriter, null)); NormsWriter normsWriter = new NormsWriter(); DocInverter docInverter = new DocInverter(termsHash, normsWriter); StoredFieldsWriter fieldsWriter = new StoredFieldsWriter(this); DocFieldConsumers docFieldConsumers = new DocFieldConsumers(docInverter, fieldsWriter); consumer = docFieldProcessor = new DocFieldProcessor(this, docFieldConsumers); }
public ReadStream(Stream stream, int bufferSize, BufferManager bufferManager) { _stream = stream; _bufferSize = bufferSize; _bufferManager = bufferManager; _queue = new WaitQueue<ArraySegment<byte>>(Math.Max(8, _bufferSize / QueueStream.BlockSize)); _watchThread = new Thread(this.WatchThread); _watchThread.Priority = ThreadPriority.BelowNormal; _watchThread.Name = "QueueStream_WatchThread"; _watchThread.Start(); }
/// <summary> /// Check to do on a wait node /// </summary> /// <param name="w"></param> /// <returns></returns> internal virtual bool Recheck(WaitQueue.WaitNode w) { lock (this) { bool pass = (nPermits > 0); if (pass) --nPermits; else wq_.Insert(w); return pass; } }
public void Dequeue_WithoutResult_CompletesWithDefaultResult() { var queue = new WaitQueue<int>(); var task0 = queue.Enqueue(item0, none); var result = queue.Dequeue(none).ResultEx(); task0.Wait(); Assert.AreEqual<int>(item0, result); }
public void Test_WaitQueue() { // アイテムが無い場合、Dequeueは待機される。 { WaitQueue<string> queue = new WaitQueue<string>(); var task1 = Task.Run(() => { Assert.AreEqual(queue.WaitDequeue(TimeSpan.Zero), false); Assert.Throws<TimeoutException>(() => { queue.Dequeue(TimeSpan.Zero); }); Assert.AreEqual(queue.Dequeue(), "Test"); }); var task2 = Task.Run(() => { Thread.Sleep(1000 * 3); queue.Enqueue("Test"); }); Task.WaitAll(task1, task2); } // キャパシティを超えるとEnqueueが待機状態になるため、4つ目のアイテムがタイムアウトの例外を投げる。 { WaitQueue<string> queue = new WaitQueue<string>(3); queue.Enqueue("1", TimeSpan.Zero); queue.Enqueue("2", TimeSpan.Zero); queue.Enqueue("3", TimeSpan.Zero); Assert.Throws<TimeoutException>(() => { queue.Enqueue("4", TimeSpan.Zero); }); } // キャパシティを超えるとEnqueueが待機され、Dequeueによって項目が減るとEnqueueが再開される。 { WaitQueue<string> queue = new WaitQueue<string>(3); queue.Enqueue("1", TimeSpan.Zero); queue.Enqueue("2", TimeSpan.Zero); queue.Enqueue("3", TimeSpan.Zero); var task1 = Task.Run(() => { Assert.AreEqual(queue.WaitEnqueue(TimeSpan.Zero), false); Thread.Sleep(1000 * 3); Assert.DoesNotThrow(() => { queue.Enqueue("4", TimeSpan.Zero); }); }); var task2 = Task.Run(() => { Thread.Sleep(1000 * 1); queue.Dequeue(); }); Task.WaitAll(task1, task2); } }
public void Dequeue_EndEnqueue_IsComplete() { var queue = new WaitQueue<int>(); var task1 = queue.Enqueue(item0, none); var task2 = queue.Enqueue(item1, none); Test.Async(async () => { await task1; await task2; Assert.IsFalse(await queue.IsEmpty(none)); Assert.IsFalse(await queue.IsComplete(none)); Assert.IsFalse(await queue.IsEnded(none)); await queue.EndEnqueue(none); Assert.IsTrue(await queue.IsEnded(none)); Assert.IsFalse(await queue.IsEmpty(none)); Assert.IsFalse(await queue.IsComplete(none)); while (queue.Count > 0) await queue.Dequeue(none); Assert.IsTrue(await queue.IsComplete(none)); Assert.IsTrue(await queue.IsEmpty(none)); Assert.IsTrue(await queue.IsEnded(none)); }); Assert.IsTrue(task1.IsCompleted); Assert.IsTrue(task2.IsCompleted); }