Skip to content

unittests/runtime: Migrate tests from pthreads to std::thread #14598

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

Merged
merged 1 commit into from
Mar 29, 2018

Conversation

AndrewSB
Copy link
Contributor

@AndrewSB AndrewSB commented Feb 13, 2018

This is required for Windows, which has std::threads, but no pthreads.
This is probably completely broken, working on running the tests locally right now.

If a member could ask @swift-ci to test, that would be much appreciated ❤️

@AndrewSB AndrewSB force-pushed the pthreads= branch 2 times, most recently from 4801d7f to 6689703 Compare February 13, 2018 23:51
@compnerd
Copy link
Member

CC: @gparker42

@AndrewSB AndrewSB force-pushed the pthreads= branch 3 times, most recently from c21d7ed to 7c1f63d Compare February 14, 2018 00:58
@compnerd
Copy link
Member

@swift-ci please smoke test

@AndrewSB AndrewSB force-pushed the pthreads= branch 2 times, most recently from afd0e45 to 70603fd Compare February 15, 2018 19:38
unsigned numThreads;
unsigned *numThreadsReady;
std::mutex *sharedMutex;
std::condition_variable *start_condition;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can these fields be references instead of pointers?

@@ -249,7 +185,7 @@ TEST(Concurrent, ConcurrentList) {
}

// Check that the length of the list is correct.
EXPECT_EQ(ListLen, results.size() * numElem);
EXPECT_EQ(ListLen, 0 * numElem);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks wrong. Why are we verifying the contents of List and then asserting that List is empty?

@AndrewSB
Copy link
Contributor Author

Should have said this: The patch is currently in progress, I know that's broken. I'll ping you when its ready to be reviewed.

@AndrewSB AndrewSB force-pushed the pthreads= branch 2 times, most recently from ab1a0d7 to 3b948bf Compare February 20, 2018 21:48
@AndrewSB
Copy link
Contributor Author

I think this is the patch I want. I'm testing it locally, but CI will probably go faster @compnerd / @gparker42 could you ask CI to test?

@gparker42 I might have changes to fix this, but this is more or less the change I was thinking. Took your references instead of pointer comment into account.

@AndrewSB AndrewSB force-pushed the pthreads= branch 4 times, most recently from 3f3077e to 9dacfa2 Compare February 20, 2018 23:14
@AndrewSB AndrewSB changed the title [WIP] unittests/runtime: Migrate tests from pthreads to std::thread unittests/runtime: Migrate tests from pthreads to std::thread Feb 20, 2018
@AndrewSB AndrewSB force-pushed the pthreads= branch 2 times, most recently from b7a846a to af0ec97 Compare February 21, 2018 00:09
std::vector<T>
RaceTest(std::function<T()> code)
{
std::vector<T> RaceTest(std::function<T()> code) {
const unsigned threadCount = NumThreads;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Im not sure I understand the point of the threadCount variable. Why not just use NumThreads everywhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was in the original implementation, I think it was because pthreads required an unsigned.
I'll remove it

@compnerd
Copy link
Member

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 668970310fc13b37adcaec0342b6e301601c3b0b

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 668970310fc13b37adcaec0342b6e301601c3b0b

@AndrewSB
Copy link
Contributor Author

AndrewSB commented Feb 21, 2018

@compnerd I removed threadCount, it looks like the CI failed with an Infra issue. Could you ask it to try again?


// Create all the threads
for (unsigned i = 0; i < NumThreads; i++) {
threads.emplace_back(std::thread(RaceThunk<T>, contexts[i]));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This combination of sized constructor and emplace_back() looks wrong. I think it creates a vector of 2*NumThreads elements, with the first half being default-initialized and the second half being the desired contents.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry! I'm still learning cpp, my bad 😬
How should I update the vector initializer to not fill in default values? I looked at http://en.cppreference.com/w/cpp/container/vector/vector but it doesn't seem easy to have the Allocator not insert values. Should I switch from emplace_backing to assigning to threads[i]?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this code simply assigning to threads[i] should be fine. Or create an empty vector and push_back or emplace_back onto it, like the old pthreads code did. Both of these might have performance downsides, but we don't care in this code.

This stackoverflow answer has a good overview of the combinations that work and their performance effects.
https://stackoverflow.com/a/32200517/881456

@AndrewSB
Copy link
Contributor Author

@gparker42 that was an interesting read. Thank you 😄
Switched to using reserving and emplacing, instead of initializing with with default-initialized

@compnerd
Copy link
Member

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - af0ec973d924343742d96a822df8d7cc09b8a1bd

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 7a31e1ad3582dc4d98bb1765b3da1360a77f3170

@shahmishal
Copy link
Member

Xcode updated

@swift-ci please test

@swift-ci
Copy link
Contributor

swift-ci commented Mar 1, 2018

Build failed
Swift Test Linux Platform
Git Sha - 7a31e1ad3582dc4d98bb1765b3da1360a77f3170

@swift-ci
Copy link
Contributor

swift-ci commented Mar 1, 2018

Build failed
Swift Test OS X Platform
Git Sha - 7a31e1ad3582dc4d98bb1765b3da1360a77f3170

@AndrewSB
Copy link
Contributor Author

AndrewSB commented Mar 1, 2018

This looks like a legitimate failure, not just an infra issue. Investigating today

@AndrewSB AndrewSB force-pushed the pthreads= branch 2 times, most recently from 95ab994 to 99056d4 Compare March 7, 2018 17:35
std::vector<std::thread> threads;
threads.reserve(NumThreads);
for (unsigned i = 0; i < NumThreads; i++)
threads.emplace_back(std::bind(RaceThunk<T>, contexts[i]));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs a std::cref on the contexts[i].

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right, thank you 🙏

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@compnerd
Copy link
Member

compnerd commented Mar 7, 2018

@swift-ci please test

@swift-ci
Copy link
Contributor

swift-ci commented Mar 7, 2018

Build failed
Swift Test OS X Platform
Git Sha - 7a31e1ad3582dc4d98bb1765b3da1360a77f3170

@swift-ci
Copy link
Contributor

swift-ci commented Mar 7, 2018

Build failed
Swift Test Linux Platform
Git Sha - 7a31e1ad3582dc4d98bb1765b3da1360a77f3170

@compnerd
Copy link
Member

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 31ebf68f32736edcc83825273f792ad81e1cd797

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 31ebf68f32736edcc83825273f792ad81e1cd797

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 31ebf68f32736edcc83825273f792ad81e1cd797

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 31ebf68f32736edcc83825273f792ad81e1cd797

@AndrewSB
Copy link
Contributor Author

@gparker42 can you take a look last this? After your review it should be good to merge

@compnerd compnerd merged commit 8695224 into swiftlang:master Mar 29, 2018
@AndrewSB AndrewSB deleted the pthreads=😥 branch March 31, 2018 00:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants