Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uses lock was taken logic in channel #306

Merged
merged 1 commit into from
Oct 18, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 43 additions & 17 deletions src/NATS.Client/Channel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace NATS.Client
// to be shared by any more than a single producer and single consumer.
internal sealed class SingleUseChannel<T>
{
static readonly ConcurrentBag<SingleUseChannel<T>> Channels
static readonly ConcurrentBag<SingleUseChannel<T>> Channels
= new ConcurrentBag<SingleUseChannel<T>>();

readonly ManualResetEventSlim e = new ManualResetEventSlim();
Expand Down Expand Up @@ -93,9 +93,9 @@ internal void reset()
internal sealed class Channel<T>
{
readonly Queue<T> q;
readonly Object qLock = new Object();
readonly Object qLock = new Object();

bool finished = false;
bool finished = false;

public string Name { get; set; }

Expand All @@ -112,9 +112,12 @@ internal Channel(int initialCapacity)

internal T get(int timeout)
{
Monitor.Enter(qLock);
var lockWasTaken = false;

try
{
Monitor.Enter(qLock, ref lockWasTaken);

if (finished)
{
return default(T);
Expand Down Expand Up @@ -157,7 +160,8 @@ internal T get(int timeout)
}
finally
{
Monitor.Exit(qLock);
if (lockWasTaken)
Monitor.Exit(qLock);
}
} // get

Expand All @@ -168,9 +172,12 @@ internal int get(int timeout, T[] buffer)
if (buffer.Length < 1) throw new ArgumentException();

int delivered = 0;
Monitor.Enter(qLock);
var lockWasTaken = false;

try
{
Monitor.Enter(qLock, ref lockWasTaken);

if (finished)
{
return 0;
Expand Down Expand Up @@ -226,15 +233,19 @@ internal int get(int timeout, T[] buffer)
}
finally
{
Monitor.Exit(qLock);
if (lockWasTaken)
Monitor.Exit(qLock);
}
} // get

internal void add(T item)
{
Monitor.Enter(qLock);
var lockWasTaken = false;

try
{
Monitor.Enter(qLock, ref lockWasTaken);

q.Enqueue(item);

// if the queue count was previously zero, we were
Expand All @@ -246,17 +257,21 @@ internal void add(T item)
}
finally
{
Monitor.Exit(qLock);
if (lockWasTaken)
Monitor.Exit(qLock);
}
}

// attempt to add an item, if-and-only-if the queue has not
// exceeded the given upper bound.
internal bool tryAdd(T item, int upperBound)
{
Monitor.Enter(qLock);
var lockWasTaken = false;

try
{
Monitor.Enter(qLock, ref lockWasTaken);

if (q.Count >= upperBound)
return false;

Expand All @@ -273,15 +288,18 @@ internal bool tryAdd(T item, int upperBound)
}
finally
{
Monitor.Exit(qLock);
if (lockWasTaken)
Monitor.Exit(qLock);
}
}

internal void close()
{
Monitor.Enter(qLock);
var lockWasTaken = false;

try
{
Monitor.Enter(qLock, ref lockWasTaken);

finished = true;

Expand All @@ -291,7 +309,8 @@ internal void close()
}
finally
{
Monitor.Exit(qLock);
if (lockWasTaken)
Monitor.Exit(qLock);
}
}

Expand All @@ -300,10 +319,17 @@ internal int Count
get
{
int rv;

Monitor.Enter(qLock);
rv = q.Count;
Monitor.Exit(qLock);
var lockWasTaken = false;
try
{
Monitor.Enter(qLock, ref lockWasTaken);
rv = q.Count;
}
finally
{
if (lockWasTaken)
Monitor.Exit(qLock);
}

return rv;
}
Expand Down