Skip to content

Commit

Permalink
Merge pull request #61 from HarryHeres/43-change-locking-api
Browse files Browse the repository at this point in the history
Updated Lock API (Issue #43)
  • Loading branch information
computablee authored Oct 3, 2023
2 parents db57ee0 + 78ea0d8 commit b441b43
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 25 deletions.
29 changes: 14 additions & 15 deletions DotMP-Tests/ParallelTests.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using DotMP;
using FluentAssertions;
using System;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using DotMP;
using FluentAssertions;
using Xunit;
using Xunit.Abstractions;

Expand Down Expand Up @@ -502,22 +501,22 @@ public void Locks_work()

DotMP.Parallel.ParallelRegion(num_threads: threads, action: () =>
{
DotMP.Lock.Set(l);
l.Set();
Thread.Sleep(100);
DotMP.Lock.Unset(l);
l.Unset();
});

double elapsed = DotMP.Parallel.GetWTime() - time;
elapsed.Should().BeGreaterThan(1.6);

DotMP.Lock.Test(l).Should().BeTrue();
DotMP.Lock.Test(l).Should().BeFalse();
DotMP.Lock.Test(l).Should().BeFalse();
DotMP.Lock.Unset(l);
DotMP.Lock.Test(l).Should().BeTrue();
DotMP.Lock.Test(l).Should().BeFalse();
DotMP.Lock.Test(l).Should().BeFalse();
DotMP.Lock.Unset(l);
l.Test().Should().BeTrue();
l.Test().Should().BeFalse();
l.Test().Should().BeFalse();
l.Unset();
l.Test().Should().BeTrue();
l.Test().Should().BeFalse();
l.Test().Should().BeFalse();
l.Unset();
}

/// <summary>
Expand Down Expand Up @@ -970,4 +969,4 @@ float[] saxpy_parallelfor(float a, float[] x, float[] y)
return z;
}
}
}
}
17 changes: 7 additions & 10 deletions DotMP/Lock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace DotMP
{
/// <summary>
/// A lock that can be used in a parallel region.
/// Also contains static methods for locking.
/// Also contains instance methods for locking.
/// Available methods are Set, Unset, and Test.
/// </summary>
public sealed class Lock
Expand All @@ -25,30 +25,27 @@ public Lock()
/// <summary>
/// Stalls the thread until the lock is set.
/// </summary>
/// <param name="lck">The lock to wait for.</param>
public static void Set(Lock lck)
public void Set()
{
while (Interlocked.CompareExchange(ref lck._lock, 1, 0) == 1) ;
while (Interlocked.CompareExchange(ref this._lock, 1, 0) == 1) ;
}

/// <summary>
/// Unsets the lock.
/// </summary>
/// <param name="lck">The lock to unset.</param>
public static void Unset(Lock lck)
public void Unset()
{
Interlocked.Exchange(ref lck._lock, 0);
Interlocked.Exchange(ref this._lock, 0);
}

/// <summary>
/// Attempts to set the lock.
/// Does not stall the thread.
/// </summary>
/// <param name="lck">The lock to attempt to set.</param>
/// <returns>True if the lock was set, false otherwise.</returns>
public static bool Test(Lock lck)
public bool Test()
{
return Interlocked.CompareExchange(ref lck._lock, 1, 0) == 0;
return Interlocked.CompareExchange(ref this._lock, 1, 0) == 0;
}
}
}

0 comments on commit b441b43

Please sign in to comment.