Skip to content

Commit f4183f4

Browse files
committed
Merge branch 'fix-exception-on-macos' into 'main'
Fix exception occuring sometimes on macos when Accessing UniqueList.SortedValues See merge request Sharpmake/sharpmake!587
2 parents 669ceb8 + 0b7e2e1 commit f4183f4

File tree

6 files changed

+201
-89
lines changed

6 files changed

+201
-89
lines changed

Sharpmake.UnitTests/UniqueListTests.cs

+25-9
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,9 @@ public ListContainer()
507507
}
508508
public UniqueList<string> List = new UniqueList<string>();
509509
public IEnumerable<string> SortedList => List.SortedValues;
510+
511+
public long IterationCount = 0;
512+
public long NonEmptyCount = 0;
510513
}
511514

512515
/// <summary>
@@ -516,27 +519,32 @@ public ListContainer()
516519
/// exception when multiple threads were accessing the property.
517520
/// </summary>
518521
[Test]
519-
public static void MultithreadEmptyValuesSorted()
522+
[TestCase("")]
523+
[TestCase("Test")]
524+
public static void MultithreadEmptyValuesSorted(string initialContent)
520525
{
521526
int nbrThreads = Environment.ProcessorCount;
522527
var container = new ListContainer();
528+
if (!string.IsNullOrEmpty(initialContent))
529+
container.List.Add(initialContent);
523530

524531
int TOTAL_TEST_COUNT = 100000;
525532
long nbrThreadsFinished = 0;
526533
long nbrThreadsGate1 = 0;
527534
Exception taskTestException = null;
528535

529536
// Note: Using a Barrier to synchronize all the threads at each iteration
530-
using (Barrier barrier = new Barrier(nbrThreads, (b) =>
537+
using (Barrier barrier = new Barrier(0, (b) =>
531538
{
539+
container.List.SetDirty();
532540
Interlocked.Increment(ref nbrThreadsGate1);
533-
container.List.AddRange(new List<string> { }); // Adding an empty collection makes the UniqueList dirty
534541
}))
535542
{
536543
ThreadPool.TaskCallback taskLambda = (object taskParams) =>
537544
{
538545
var listContainersTask = (ListContainer)taskParams;
539-
546+
int count = 0;
547+
int nonEmptyCount = 0;
540548
try
541549
{
542550
for (int i = 0; i < TOTAL_TEST_COUNT; ++i)
@@ -547,11 +555,15 @@ public static void MultithreadEmptyValuesSorted()
547555
if (taskTestException != null)
548556
break; // Abort once we got an exception
549557

550-
// Attempt to access the SortedList property from multiple threads.
551-
// It must not create any exception!
552-
foreach (var s in container.SortedList)
558+
for (int j = 0; j < 5; ++j)
553559
{
554-
Console.WriteLine(s);
560+
// Attempt to access the SortedList property from multiple threads.
561+
// It must not create any exception!
562+
foreach (var s in container.SortedList)
563+
{
564+
++nonEmptyCount;
565+
}
566+
++count;
555567
}
556568
}
557569
}
@@ -562,8 +574,10 @@ public static void MultithreadEmptyValuesSorted()
562574
}
563575
finally
564576
{
565-
barrier.RemoveParticipant(); // Must remove the participant to unblock all the other threads.
566577
Interlocked.Increment(ref nbrThreadsFinished);
578+
Interlocked.Add(ref listContainersTask.IterationCount, count);
579+
Interlocked.Add(ref listContainersTask.IterationCount, nonEmptyCount);
580+
barrier.RemoveParticipant();
567581
}
568582
};
569583

@@ -572,6 +586,7 @@ public static void MultithreadEmptyValuesSorted()
572586
{
573587
// Add 1 task per thread
574588
pool.Start(nbrThreads);
589+
barrier.AddParticipants(nbrThreads);
575590
for (int i = 0; i < nbrThreads; ++i)
576591
{
577592
pool.AddTask(taskLambda, container);
@@ -582,6 +597,7 @@ public static void MultithreadEmptyValuesSorted()
582597

583598
// Check the results.
584599
TestContext.Out.WriteLine("nbr Finished: {0}, nbr Gate1: {1}", nbrThreadsFinished, nbrThreadsGate1);
600+
TestContext.Out.WriteLine($"IterationCount : {container.IterationCount}");
585601
if (taskTestException != null)
586602
{
587603
throw taskTestException;

Sharpmake/Options.XCode.cs

+6-18
Original file line numberDiff line numberDiff line change
@@ -1027,29 +1027,17 @@ public UIInterfaceOrientation_iPad(UIInterfaceOrientation value)
10271027
: base(value) { }
10281028
}
10291029

1030-
public class UISupportedInterfaceOrientations : UniqueList<UIInterfaceOrientation>
1030+
public class UISupportedInterfaceOrientations
10311031
{
1032+
private UIInterfaceOrientation[] _uIInterfaceOrientations;
10321033
public UISupportedInterfaceOrientations(params UIInterfaceOrientation[] values)
1033-
: base(EqualityComparer<UIInterfaceOrientation>.Default, values) { }
1034+
{
1035+
_uIInterfaceOrientations = values;
1036+
}
10341037

10351038
public override string ToString()
10361039
{
1037-
StringBuilder builder = new StringBuilder(Count * 128);
1038-
bool first = true;
1039-
foreach (UIInterfaceOrientation value in _hash)
1040-
{
1041-
if (!first)
1042-
builder.Append(' ');
1043-
else
1044-
first = false;
1045-
1046-
builder.Append(value.ToString());
1047-
}
1048-
1049-
if (_hash.Count > 1)
1050-
return @$"""{builder.ToString()}""";
1051-
1052-
return builder.ToString();
1040+
return "\"" + string.Join(" ", _uIInterfaceOrientations) + "\"";
10531041
}
10541042
}
10551043

Sharpmake/Strings.cs

+2-4
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@ namespace Sharpmake
1313
public class Strings : UniqueList<string>
1414
{
1515
public Strings(IEqualityComparer<string> hashComparer, IComparer<string> sortComparer)
16-
: base(hashComparer)
16+
: base(hashComparer, sortComparer)
1717
{
18-
SortComparer = sortComparer;
1918
}
2019
public Strings()
21-
: base(StringComparer.OrdinalIgnoreCase)
20+
: base(StringComparer.OrdinalIgnoreCase, StringComparer.OrdinalIgnoreCase)
2221
{
23-
SortComparer = StringComparer.OrdinalIgnoreCase;
2422
}
2523

2624
public Strings(IEnumerable<string> other) : base(StringComparer.OrdinalIgnoreCase, other) { }

0 commit comments

Comments
 (0)