public void TestAsyncPushList() { ThreadSafeStack <int> integers = new ThreadSafeStack <int>(); Task t1 = Task.Run( () => { List <int> data = new List <int>(500); for (int i = 0; i < 1000; i += 2) { System.Threading.Thread.Sleep(10); data.Add(i); } integers.Push(data); } ); Task t2 = Task.Run( () => { List <int> data = new List <int>(500); for (int i = 1; i < 1000; i += 2) { System.Threading.Thread.Sleep(10); data.Add(i); } integers.Push(data); } ); Task.WaitAll(t1, t2); Assert.AreEqual(1000, integers.Count); }
public void TestAsyncPop() { ThreadSafeStack <int> integers = new ThreadSafeStack <int>(); for (int i = 3; i < 1000000; ++i) { integers.Push(i); } Task t1 = Task.Run( () => { while (!integers.IsEmpty()) { integers.Pop(); } } ); Task t2 = Task.Run( () => { while (!integers.IsEmpty()) { integers.Pop(); } } ); Task.WaitAll(t1, t2); Assert.AreEqual(0, integers.Count); }
public void TestAsyncPush() { ThreadSafeStack <int> integers = new ThreadSafeStack <int>(); Task t1 = Task.Run( () => { for (int i = 0; i < 1000000; ++i) { integers.Push(i); } } ); Task t2 = Task.Run( () => { for (int i = 0; i < 1000000; ++i) { integers.Push(i); } } ); Task.WaitAll(t1, t2); Assert.AreEqual(2000000, integers.Count); }
T ReuseInstance(int pool, Func <T> onFirstInit) { T obj = null; ThreadSafeStack <T> localPool = ReturnValidPool(_recycledPools, pool); while (IsNull(obj) == true && localPool.count > 0) { localPool.TryPop(out obj); } if (IsNull(obj) == true) { obj = onFirstInit(); _objectsCreated++; } else { #if DEBUG && !PROFILE_SVELTO _alreadyRecycled.TryRemove(obj, out _); #endif _objectsReused++; } // ConcurrentStack<T> localUsedPool = ReturnValidPool(_usedPools, pool); // localUsedPool.Push(obj); return(obj); }
private ThreadSafeStack<SocketAsyncEventArgs> ioEventArgsPool; //IO事件池 public SocketListener(SocketSettings socketSettings) { this.socketSettings = socketSettings; this.bufferManager = new BufferManager(socketSettings.BufferSize * socketSettings.NumOfRecSend, socketSettings.BufferSize); this.ioEventArgsPool = new ThreadSafeStack<SocketAsyncEventArgs>(socketSettings.NumOfRecSend); this.acceptEventArgsPool = new ThreadSafeStack<SocketAsyncEventArgs>(socketSettings.MaxAcceptOps); this.maxConnectionsEnforcer = new Semaphore(socketSettings.MaxConnections, socketSettings.MaxConnections); Init(); }
ThreadSafeStack <T> ReturnValidPool(ThreadSafeDictionary <int, ThreadSafeStack <T> > pools, int pool) { if (pools.TryGetValue(pool, out var localPool) == false) { pools[pool] = localPool = new ThreadSafeStack <T>(); } return(localPool); }
public void TruncateAndFlush(Context ctx, long timestamp) { IMessageTree tree = ctx.Tree; ThreadSafeStack <ITransaction> stack = ctx.Stack; IMessage message = tree.Message; if (message is DefaultTransaction) { string id = tree.MessageId; string rootId = tree.RootMessageId; string childId = _mManager.NextMessageId(); DefaultTransaction source = (DefaultTransaction)message; DefaultTransaction target = new DefaultTransaction(source.Type, source.Name, _mManager); target.Timestamp = source.Timestamp; target.DurationInMicros = source.DurationInMicros; target.AddData(source.Data); target.Status = CatConstants.SUCCESS; MigrateMessage(stack, source, target, 1); int reducedByteSize = 0; foreach (ITransaction transaction in stack) { DefaultTransaction tran = (DefaultTransaction)transaction; tran.Timestamp = timestamp; reducedByteSize += transaction.EstimateByteSize(); } DefaultEvent next = new DefaultEvent("RemoteCall", "Next"); next.AddData(childId); next.Status = CatConstants.SUCCESS; target.AddChild(next); IMessageTree t = tree.Copy(); t.Message = target; ctx.Tree.MessageId = childId; ctx.Tree.ParentMessageId = id; ctx.Tree.RootMessageId = (rootId != null ? rootId : id); ctx._mLength = stack.Count; // Update estimated byte size of the truncated tree to be the total size of all on-stack transactions. ctx.Tree.EstimatedByteSize = reducedByteSize; ctx._mTotalDurationInMicros = ctx._mTotalDurationInMicros + target.DurationInMicros; _mManager.Flush(t); } }
/// <summary> /// Initializes a new instance of the <see cref="ZyGames.Framework.RPC.Sockets.SocketListener"/> class. /// </summary> /// <param name="socketSettings">Socket settings.</param> public SocketListener(SocketSettings socketSettings) { this.socketSettings = socketSettings; this.prefixHandler = new PrefixHandler(); this.messageHandler = new MessageHandler(); this.bufferManager = new BufferManager(this.socketSettings.BufferSize * this.socketSettings.NumOfSaeaForRecSend, this.socketSettings.BufferSize); this.ioEventArgsPool = new ThreadSafeStack<SocketAsyncEventArgs>(socketSettings.NumOfSaeaForRecSend); this.acceptEventArgsPool = new ThreadSafeStack<SocketAsyncEventArgs>(socketSettings.MaxAcceptOps); this.maxConnectionsEnforcer = new Semaphore(this.socketSettings.MaxConnections, this.socketSettings.MaxConnections); Init(); expireTimer = new Timer(CheckExpire, null, socketSettings.ExpireInterval, socketSettings.ExpireInterval); }
public Context(DefaultMessageManager manager, String domain) { _mManager = manager; _mTree = new DefaultMessageTree(); _mStack = new ThreadSafeStack <ITransaction>(); Thread thread = Thread.CurrentThread; String groupName = Thread.GetDomain().FriendlyName; _mTree.ThreadGroupName = groupName; _mTree.ThreadId = thread.ManagedThreadId.ToString(); _mTree.ThreadName = thread.Name; _mTree.Domain = domain; // _mTree.HostName = hostName; // _mTree.IpAddress = ipAddress; _mLength = 1; _mKnownExceptions = new HashSet <Exception>(); }
private void MigrateMessage(ThreadSafeStack <ITransaction> stack, ITransaction source, ITransaction target, int level) { // Note that stack.ToArray() gives an array reversed, which is the opposite of Java. ITransaction[] onStackTransactions = stack.ToArray(); ITransaction current = (level < stack.Count ? onStackTransactions[stack.Count - 1 - level] : null); bool shouldKeep = false; foreach (IMessage child in source.Children) { if (child != current) { target.AddChild(child); } else { DefaultTransaction cloned = new DefaultTransaction(current.Type, current.Name, _mManager); cloned.Timestamp = current.Timestamp; cloned.DurationInMicros = current.DurationInMicros; cloned.AddData(current.Data); cloned.Status = CatConstants.SUCCESS; target.AddChild(cloned); MigrateMessage(stack, current, cloned, level + 1); shouldKeep = true; } } lock (source.Children) { source.Children.Clear(); } if (shouldKeep) { source.AddChild(current); } }
public ConcurrentStack(IEnumerable <T> collection) { _wrapped = new ThreadSafeStack <T>(collection); }
public ConcurrentStack() { _wrapped = new ThreadSafeStack <T>(); }
/// <summary> /// Initializes a new instance /// </summary> /// <param name="socketSettings">Socket settings.</param> /// <param name="requestHandler"></param> public SocketListener(SocketSettings socketSettings, RequestHandler requestHandler) { this.socketSettings = socketSettings; this.requestHandler = requestHandler; this.bufferManager = new BufferManager(this.socketSettings.BufferSize * this.socketSettings.NumOfSaeaForRecSend, this.socketSettings.BufferSize); this.ioEventArgsPool = new ThreadSafeStack<SocketAsyncEventArgs>(socketSettings.NumOfSaeaForRecSend); this.acceptEventArgsPool = new ThreadSafeStack<SocketAsyncEventArgs>(socketSettings.MaxAcceptOps); this.maxConnectionsEnforcer = new Semaphore(this.socketSettings.MaxConnections, this.socketSettings.MaxConnections); Init(); }
protected override void OnSetUp() { base.OnSetUp(); stack = new ThreadSafeStack<int>(); }
protected override void OnSetUp() { base.OnSetUp(); stack = new ThreadSafeStack <int>(); }