This is an archive of the discontinued LLVM Phabricator instance.

[tsan] Fix a crash when exiting the main thread (e.g. dispatch_main)
ClosedPublic

Authored by kubamracek on Mar 27 2016, 12:34 AM.

Details

Diff Detail

Repository
rL LLVM

Event Timeline

kubamracek retitled this revision from to [tsan] Fix a crash when exiting the main thread (e.g. dispatch_main).
kubamracek updated this object.
kubamracek added reviewers: dvyukov, samsonov, kcc, glider.
dvyukov added inline comments.Mar 27 2016, 7:22 AM
test/tsan/Darwin/dispatch_main.mm
27 ↗(On Diff #51729)

Why do we need this sleep? Can we remove it? Can we use the invisible barrier instead?
This test will run for 3 seconds. I would like to reduce it's duration.

28 ↗(On Diff #51729)

This is exit which should terminate whole process. How is calling pthread_exit? Where?

kubamracek added inline comments.Mar 27 2016, 7:23 AM
test/tsan/Darwin/dispatch_main.mm
27 ↗(On Diff #51729)

I'll try to remove it.

28 ↗(On Diff #51729)

pthread_exit is called immediately from dispatch_main below, not here.

dvyukov added inline comments.Mar 27 2016, 7:34 AM
test/tsan/Darwin/dispatch_main.mm
28 ↗(On Diff #51729)

I am confused. You do "// CHECK: Done.", so NSLog is executed. But then exit is not executed? And this is executed on the main thread? How does dispatch_main calls pthread_exit while we are blocked in sleep in the very same thread? From a signal handler? Why does it do it?
I seem to be missing something.

kubamracek added inline comments.Mar 27 2016, 7:39 AM
test/tsan/Darwin/dispatch_main.mm
28 ↗(On Diff #51729)

This is not executed on the main thread, the main thread is terminated from dispatch_main. This gets executed on a different thread, but technically still on the "main queue".

kubamracek added inline comments.Mar 27 2016, 7:42 AM
test/tsan/Darwin/dispatch_main.mm
28 ↗(On Diff #51729)

Calling dispatch_main() from the main thread basically says: "I'm not using the main thread for anything else that libdispatch stuff." So libdispatch knows it's safe to treat the main queue as a "regular serial queue", exit the main thread and schedule stuff dispatched to the main queue on worker threads (but still in a serial, synchronized manner).

dvyukov accepted this revision.Mar 27 2016, 10:51 AM
dvyukov edited edge metadata.

LGTM. But please document how pthread_exit gets called on the main thread and change sleep(3) to sleep(1) which should be enough to catch the crash in most executions.

This revision is now accepted and ready to land.Mar 27 2016, 10:51 AM
This revision was automatically updated to reflect the committed changes.