Skip to content

Commit

Permalink
Fix potential spurious thread wakeup. (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
nlutsenko committed Apr 15, 2016
1 parent a40b65b commit d326aa0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Bolts/Common/BFTask.m
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,9 @@ - (void)waitUntilFinished {
}
[self.condition lock];
}
[self.condition wait];
while (!self.completed) {
[self.condition wait];
}
[self.condition unlock];
}

Expand Down
39 changes: 39 additions & 0 deletions BoltsTests/TaskTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -858,4 +858,43 @@ - (void)testTrySetCancelled {
XCTAssertTrue(taskCompletionSource.task.cancelled);
}

- (void)testMultipleWaitUntilFinished {
BFTask *task = [[BFTask taskWithDelay:50] continueWithBlock:^id(BFTask *task) {
return @"foo";
}];

[task waitUntilFinished];

XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[task waitUntilFinished];
[expectation fulfill];
});
[self waitForExpectationsWithTimeout:10.0 handler:nil];
}

- (void)testMultipleThreadsWaitUntilFinished {
BFTask *task = [[BFTask taskWithDelay:500] continueWithBlock:^id(BFTask *task) {
return @"foo";
}];

dispatch_queue_t queue = dispatch_queue_create("com.bolts.tests.wait", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_t group = dispatch_group_create();

XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_group_async(group, queue, ^{
[task waitUntilFinished];
});
dispatch_group_async(group, queue, ^{
[task waitUntilFinished];
});
[task waitUntilFinished];

dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
[expectation fulfill];
});
[self waitForExpectationsWithTimeout:10.0 handler:nil];
}

@end

0 comments on commit d326aa0

Please sign in to comment.