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

Make lib-str domain safe #635

Merged

Conversation

abbysmal
Copy link
Collaborator

@abbysmal abbysmal commented Sep 1, 2021

This PR moves the usage of global variables in str to thread local storage.

The one weird part revolves around freeing the resources on domain shutdown. It make use of the previously introduced domain_stop_hook (for systhreads).

There's also the alternative route of allocating str's internal data structures as custom blocks and letting the GC take care of these, though I'm not quite sure it is a cleaner solution.

I think adding a testcase that does some str computations in parallel would be useful, I will add some in the coming days.

@abbysmal
Copy link
Collaborator Author

abbysmal commented Sep 1, 2021

I also noticed there's one case I'm not handling properly, will mark it as draft until I fixed it.

@abbysmal abbysmal marked this pull request as draft September 1, 2021 15:37
Copy link
Collaborator

@Sudha247 Sudha247 left a comment

Choose a reason for hiding this comment

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

I went through this PR and had a discussion with @Engil offline, the gist of it is below:

Thread-local values are stored in pthread specific variable heres. This means the thread-local values will be unique not just for domains, but also for systhreads.

To be noted: systhreads access the same global state on trunk, if we'd like to replicate this behaviour I think it might be useful to have a C API for domain-local storage. It appears Str module is currently not thread-safe in the context of systhreads and this PR would actually fixes it. We think it's all right to move forward with this approach, unless there are any alternate ideas.

@abbysmal abbysmal marked this pull request as ready for review September 7, 2021 12:47
Copy link
Collaborator

@ctk21 ctk21 left a comment

Choose a reason for hiding this comment

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

I think this works and solves the problem of multiple domains using str.

I had a nit about error handling should an alloc fail. Also I'm not convinced we store prev_domain_stop_hook which is a bug.

otherlibs/str/strstubs.c Show resolved Hide resolved
otherlibs/str/strstubs.c Show resolved Hide resolved
@ctk21 ctk21 merged commit d9d5c2a into ocaml-multicore:4.12+domains+effects Sep 15, 2021
@ctk21 ctk21 moved this from In progress to Done in Prepare multicore to enable 5.0 patchset Sep 16, 2021
@xavierleroy
Copy link
Contributor

I wonder if we could avoid global state in strstubs.c altogether, using C local variables for the initial RE engine stack and for the register array. I'll give it a try in the sequential version of OCaml.

@xavierleroy
Copy link
Contributor

Are you OK to use ocaml/ocaml#10670 instead? Much less code.

@ctk21
Copy link
Collaborator

ctk21 commented Oct 4, 2021

Are you OK to use ocaml/ocaml#10670 instead? Much less code.

The approach taken in ocaml/ocaml#10670 looks good to me and is less code.

When that gets merged upstream, we will pick it up naturally as we rebase.

@abbysmal
Copy link
Collaborator Author

abbysmal commented Oct 4, 2021

It is much cleaner than the current approach, and removes the need to add more cleanup hooks in the domain's lifecycle, thank you for this. :)

@xavierleroy
Copy link
Contributor

Thanks for the confirmation. In the future I'll try to make suggestions for alternate implementations earlier!

sadiqj pushed a commit to sadiqj/ocaml that referenced this pull request Jan 10, 2022
…e_str_domain_safe

Make lib-str domain safe
ctk21 added a commit to ctk21/ocaml that referenced this pull request Jan 11, 2022
…e_str_domain_safe

Make lib-str domain safe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants