Skip to content

Multithreading 2/N: PROXY_TO_PTHREAD option #5527

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 2 commits into from
Aug 31, 2017

Conversation

juj
Copy link
Collaborator

@juj juj commented Aug 26, 2017

The initial work to get --proxy-to-worker option to play nice with -s USE_PTHREADS=1 was a dead end, because it led to having to do a call graph flow analysis of which compiled functions main thread might need, which became ridiculous. Instead, this PR implements another approach that ended up being successful.

If -s PROXY_TO_PTHREAD=1 option is passed, then the application main() function will be invoked in a pthread_create()d function. There is nothing much magical here, since developers can also do this by themselves, but this has the big advantage of not needing to have Emscripten-specific boilerprate code around main() to set up a worker context. Undoubtedly some users will want to avoid this and do their thread setups themselves, and there are still a lot of users who will want to continue to run the application main thread in the main browser thread, which is why we will have both options.

@juj
Copy link
Collaborator Author

juj commented Aug 26, 2017

Also pushed a commit which makes --proxy-to-worker no longer work with -s USE_PTHREADS=1. I don't think anyone has ever been using those two together for real, since the support has been so limited. After this series of pull requests has landed, the -s USE_PTHREADS=1 -s PROXY_TO_PTHREAD=1 combination will have "proper" threading support so that will supersede all possible use cases that --proxy-to-worker -s USE_PTHREADS=1 might have had.

return (void*)__call_main(args->argc, args->argv);
}

static volatile main_args _main_arguments;
Copy link
Member

Choose a reason for hiding this comment

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

volatile?

Copy link
Member

Choose a reason for hiding this comment

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

lgtm. just curious why this is volatile.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Agree that it is not needed here from compiler perspective. In general when I have shared state, if I don't use atomic operations to access the data and if there does not exist a mutex object associated with the shared data, I like to mark global data volatile more for documentation reasons ("see this thing here is shared between threads") (shared, not to mean synchronized for the pedantics). Here pthread_create is the synchronization primitive, rather than an atomic op or a mutex lock.

We don't really have a convention here in the codebase for this yet, could certainly go either way here. wdyt?

Copy link
Member

Choose a reason for hiding this comment

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

Got it, sounds fine the current way.

juj added 2 commits August 31, 2017 10:06
…xy main() to be called in a web worker/pthread.
… Replace uses with -s PROXY_TO_PTHREAD=1 option.
@juj juj force-pushed the proxy_to_pthread branch from 8ae9f94 to 9576ca9 Compare August 31, 2017 07:09
@juj juj merged commit d301b71 into emscripten-core:incoming Aug 31, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants