Skip to content

Commit d326aa0

Browse files
committed
Fix potential spurious thread wakeup. (#247)
1 parent a40b65b commit d326aa0

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

Bolts/Common/BFTask.m

+3-1
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,9 @@ - (void)waitUntilFinished {
499499
}
500500
[self.condition lock];
501501
}
502-
[self.condition wait];
502+
while (!self.completed) {
503+
[self.condition wait];
504+
}
503505
[self.condition unlock];
504506
}
505507

BoltsTests/TaskTests.m

+39
Original file line numberDiff line numberDiff line change
@@ -858,4 +858,43 @@ - (void)testTrySetCancelled {
858858
XCTAssertTrue(taskCompletionSource.task.cancelled);
859859
}
860860

861+
- (void)testMultipleWaitUntilFinished {
862+
BFTask *task = [[BFTask taskWithDelay:50] continueWithBlock:^id(BFTask *task) {
863+
return @"foo";
864+
}];
865+
866+
[task waitUntilFinished];
867+
868+
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
869+
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
870+
[task waitUntilFinished];
871+
[expectation fulfill];
872+
});
873+
[self waitForExpectationsWithTimeout:10.0 handler:nil];
874+
}
875+
876+
- (void)testMultipleThreadsWaitUntilFinished {
877+
BFTask *task = [[BFTask taskWithDelay:500] continueWithBlock:^id(BFTask *task) {
878+
return @"foo";
879+
}];
880+
881+
dispatch_queue_t queue = dispatch_queue_create("com.bolts.tests.wait", DISPATCH_QUEUE_CONCURRENT);
882+
dispatch_group_t group = dispatch_group_create();
883+
884+
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
885+
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
886+
dispatch_group_async(group, queue, ^{
887+
[task waitUntilFinished];
888+
});
889+
dispatch_group_async(group, queue, ^{
890+
[task waitUntilFinished];
891+
});
892+
[task waitUntilFinished];
893+
894+
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
895+
[expectation fulfill];
896+
});
897+
[self waitForExpectationsWithTimeout:10.0 handler:nil];
898+
}
899+
861900
@end

0 commit comments

Comments
 (0)