-
-
Notifications
You must be signed in to change notification settings - Fork 290
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
65 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -149,4 +149,4 @@ private void SendBatch(IContext context) | |
|
||
_messageCount -= _batchSize; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// ----------------------------------------------------------------------- | ||
// <copyright file = "LockingUnboundedMailboxQueue.cs" company = "Asynkron AB"> | ||
// Copyright (C) 2015-2022 Asynkron AB All rights reserved | ||
// </copyright> | ||
// ----------------------------------------------------------------------- | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
|
||
namespace Proto.Mailbox; | ||
|
||
public class LockingUnboundedMailboxQueue : IMailboxQueue | ||
{ | ||
private readonly object _lock = new(); | ||
private readonly Queue<object> _queue; | ||
private long _count; | ||
|
||
public LockingUnboundedMailboxQueue(int initialCapacity) | ||
{ | ||
_queue = new Queue<object>(initialCapacity); | ||
} | ||
|
||
public bool HasMessages => Length > 0; | ||
public int Length { | ||
get { | ||
Interlocked.Read(ref _count); | ||
return (int)_count; | ||
} | ||
} | ||
|
||
public void Push(object message) | ||
{ | ||
lock (_lock) | ||
{ | ||
_queue.Enqueue(message); | ||
Interlocked.Increment(ref _count); | ||
} | ||
} | ||
|
||
public object? Pop() | ||
{ | ||
if (!HasMessages) | ||
{ | ||
return null; | ||
} | ||
|
||
lock (_lock) | ||
{ | ||
if (_queue.TryDequeue(out var msg)) | ||
{ | ||
Interlocked.Decrement(ref _count); | ||
} | ||
return msg; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters