Skip to content

Commit 643360a

Browse files
authored
Merge pull request #9 from uber/fix_nscondition_loop
Fix CountDownLatch usage of NSCondition
2 parents 1afb463 + 1880d77 commit 643360a

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

Concurrency.xcodeproj/project.pbxproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@
3434
/* End PBXBuildFile section */
3535

3636
/* Begin PBXContainerItemProxy section */
37-
413AD8C020AF73AB00AD7F36 /* PBXContainerItemProxy */ = {
37+
412CDD2D20B88EAB00AF5890 /* PBXContainerItemProxy */ = {
3838
isa = PBXContainerItemProxy;
3939
containerPortal = OBJ_1 /* Project object */;
4040
proxyType = 1;
4141
remoteGlobalIDString = "Concurrency::Concurrency";
4242
remoteInfo = Concurrency;
4343
};
44-
413AD8C120AF73AB00AD7F36 /* PBXContainerItemProxy */ = {
44+
412CDD2E20B88EAB00AF5890 /* PBXContainerItemProxy */ = {
4545
isa = PBXContainerItemProxy;
4646
containerPortal = OBJ_1 /* Project object */;
4747
proxyType = 1;
@@ -258,12 +258,12 @@
258258
OBJ_42 /* PBXTargetDependency */ = {
259259
isa = PBXTargetDependency;
260260
target = "Concurrency::ConcurrencyTests" /* ConcurrencyTests */;
261-
targetProxy = 413AD8C120AF73AB00AD7F36 /* PBXContainerItemProxy */;
261+
targetProxy = 412CDD2E20B88EAB00AF5890 /* PBXContainerItemProxy */;
262262
};
263263
OBJ_54 /* PBXTargetDependency */ = {
264264
isa = PBXTargetDependency;
265265
target = "Concurrency::Concurrency" /* Concurrency */;
266-
targetProxy = 413AD8C020AF73AB00AD7F36 /* PBXContainerItemProxy */;
266+
targetProxy = 412CDD2D20B88EAB00AF5890 /* PBXContainerItemProxy */;
267267
};
268268
/* End PBXTargetDependency section */
269269

Sources/Concurrency/CountDownLatch.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,18 @@ public class CountDownLatch {
8181
}
8282

8383
condition.lock()
84+
defer {
85+
condition.unlock()
86+
}
8487
// Check count again after acquiring the lock, before entering waiting. This ensures the caller
8588
// does not enter waiting after the last counting down occurs.
86-
if conditionCount.value > 0 {
87-
return condition.wait(until: deadline)
89+
// NSCondition must be run in a loop, since it can wake up randomly without any siganling.
90+
while conditionCount.value > 0 {
91+
let result = condition.wait(until: deadline)
92+
if !result || Date() > deadline {
93+
return false
94+
}
8895
}
89-
condition.unlock()
9096
return true
9197
}
9298

0 commit comments

Comments
 (0)