Skip to content

Commit

Permalink
Merge pull request #156 from cnblogs/use-ConcurrentStack
Browse files Browse the repository at this point in the history
Replace InterlockedStack with ConcurrentStack
  • Loading branch information
cnblogs-dudu authored Dec 6, 2020
2 parents 778914b + ac54c93 commit 439f4a1
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 49 deletions.
95 changes: 48 additions & 47 deletions Enyim.Caching/InterlockedStack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,77 +3,78 @@

namespace Enyim.Collections
{
/// <summary>
/// Implements a non-locking stack.
/// </summary>
/// <typeparam name="TItem"></typeparam>
public class InterlockedStack<TItem>
{
private Node head;
/// <summary>
/// Implements a non-locking stack.
/// </summary>
/// <typeparam name="TItem"></typeparam>
[Obsolete]
public class InterlockedStack<TItem>
{
private Node head;

public InterlockedStack()
{
this.head = new Node(default(TItem));
}
public InterlockedStack()
{
this.head = new Node(default(TItem));
}

public void Push(TItem item)
{
var node = new Node(item);
public void Push(TItem item)
{
var node = new Node(item);

do { node.Next = this.head.Next; }
while (Interlocked.CompareExchange(ref this.head.Next, node, node.Next) != node.Next);
}
do { node.Next = this.head.Next; }
while (Interlocked.CompareExchange(ref this.head.Next, node, node.Next) != node.Next);
}

public bool TryPop(out TItem value)
{
value = default(TItem);
Node node;
public bool TryPop(out TItem value)
{
value = default(TItem);
Node node;

do
{
node = head.Next;
if (node == null) return false;
}
while (Interlocked.CompareExchange(ref head.Next, node.Next, node) != node);
do
{
node = head.Next;
if (node == null) return false;
}
while (Interlocked.CompareExchange(ref head.Next, node.Next, node) != node);

value = node.Value;
value = node.Value;

return true;
}
return true;
}

#region [ Node ]
#region [ Node ]

private class Node
{
public readonly TItem Value;
public Node Next;
private class Node
{
public readonly TItem Value;
public Node Next;

public Node(TItem value)
{
this.Value = value;
}
}
public Node(TItem value)
{
this.Value = value;
}
}

#endregion
}
#endregion
}
}

#region [ License information ]
/* ************************************************************
*
*
* Copyright (c) 2010 Attila Kiskó, enyim.com
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
* ************************************************************/
#endregion
5 changes: 3 additions & 2 deletions Enyim.Caching/Memcached/MemcachedNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Enyim.Collections;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
Expand Down Expand Up @@ -266,7 +267,7 @@ private class InternalPoolImpl : IDisposable
/// <summary>
/// A list of already connected but free to use sockets
/// </summary>
private InterlockedStack<PooledSocket> _freeItems;
private ConcurrentStack<PooledSocket> _freeItems;

private bool isDisposed;
private bool isAlive;
Expand Down Expand Up @@ -307,7 +308,7 @@ internal InternalPoolImpl(
this.maxItems = config.MaxPoolSize;

_semaphore = new SemaphoreSlim(maxItems, maxItems);
_freeItems = new InterlockedStack<PooledSocket>();
_freeItems = new ConcurrentStack<PooledSocket>();

_logger = logger;
_isDebugEnabled = _logger.IsEnabled(LogLevel.Debug);
Expand Down

0 comments on commit 439f4a1

Please sign in to comment.