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

Change from 'monitor lock' to 'Interlocked' #192

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
15 changes: 9 additions & 6 deletions SharpSnmpLib/Messaging/NumberGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
// DEALINGS IN THE SOFTWARE.

using System;
using System.Threading;

namespace Lextm.SharpSnmpLib.Messaging
{
Expand Down Expand Up @@ -46,22 +47,24 @@ public int NextId
{
get
{
lock (_root)
while (true)
{
if (_salt == _max)
var next = Interlocked.Increment(ref _salt);
if (next < _min || next >= _max)
{
_salt = _min;
if (Interlocked.CompareExchange(ref _salt, _min, next) == next)
{
return _min;
}
}
else
{
_salt++;
return next;
}
return _salt;
}
}
}

private readonly object _root = new();
private int _salt;
private readonly int _min;
private readonly int _max;
Expand Down
24 changes: 13 additions & 11 deletions SharpSnmpLib/Security/SaltGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

using System;
using System.Linq;
using System.Threading;

namespace Lextm.SharpSnmpLib.Security
{
Expand All @@ -27,14 +28,14 @@ namespace Lextm.SharpSnmpLib.Security
/// </summary>
public sealed class SaltGenerator
{
private readonly object _root = new();
private long _salt = Convert.ToInt64(new Random().Next(1, int.MaxValue));
internal static bool LockSalt { get; set; }

internal static int LockSalt;

internal void SetSalt(long salt)
{
// IMPORTANT: for unit testing only.
_salt = salt;
Interlocked.Exchange(ref _salt, salt);
}

/// <summary>
Expand All @@ -45,25 +46,26 @@ internal long NextSalt
{
get
{
lock (_root)
while (true)
{
if (LockSalt)
if (Interlocked.CompareExchange(ref LockSalt, 0, 1) == 1)
{
// IMPORTANT: for unit testing only.
LockSalt = false;
return 2048;
}

if (_salt == long.MaxValue)
var next = Interlocked.Increment(ref _salt);
if (next < 1 || next > long.MaxValue)
{
_salt = 1;
if (Interlocked.CompareExchange(ref _salt, 1, next) == next)
{
return 1;
}
}
else
{
_salt++;
return next;
}

return _salt;
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion Tests/CSharpCore/Unit/Messaging/MessageFactoryTestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Lextm.SharpSnmpLib.Security;
using Xunit;
using System.IO;
using System.Threading;

#pragma warning disable 1591, 0618
namespace Lextm.SharpSnmpLib.Unit.Messaging
Expand Down Expand Up @@ -229,7 +230,7 @@ public void TestGetRequestV3AuthPrivAES2()
return;
}

SaltGenerator.LockSalt = true;
Interlocked.Exchange(ref SaltGenerator.LockSalt, 1);
var auth = new SHA1AuthenticationProvider(new OctetString("authkey1"));
var privacy = new AESPrivacyProvider(new OctetString("privkey1"), auth);
var getRequestMessage = new GetRequestMessage(
Expand Down