Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix potential spurious thread wakeup. #247

Merged
merged 1 commit into from
Apr 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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