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

Restore "Protect against repeated initialization (PR#3532)" #11473

Merged
merged 1 commit into from Aug 28, 2022

Conversation

gadmm
Copy link
Contributor

@gadmm gadmm commented Aug 3, 2022

This restores some bit left out from the previous systhreads implementation.

I would like to have this in 5.0. The ability to call caml_thread_initialize from Rust directly after calling caml_startup potentially avoids some initialization order pains. I give this advice to Rust users for OCaml 4 and it would be nice if my advice worked consistently between major OCaml versions (without this PR, it would crash when re-entering caml_thread_initialize from the Thread module). Since this functionality was there before, this is fixing an API discrepancy, so I think it is appropriate for 5.0.

No change entry needed.

(cc @Engil, @gasche)

@@ -437,7 +437,8 @@ void caml_thread_interrupt_hook(void)
system. */
CAMLprim value caml_thread_initialize(value unit)
{
CAMLparam0();
/* Protect against repeated initialization (PR#3532) */
if (Active_thread != NULL) return Val_unit;
Copy link
Member

Choose a reason for hiding this comment

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

Why can we assume that Active_thread is NULL before initialization, and not some uninitialized value whose read is undefined behavior? thread_table does not seem 0-initialized.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The “thread_table table” is actually 0-initialized, since it is a non-local static and an array of values.

Copy link
Contributor

Choose a reason for hiding this comment

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

To me all fields should be indeed zero initialized as @gadmm mentioned, so I think this bit is ok. (thread_table[n].active_thread should indeed be 0)

@dra27 dra27 added this to the 5.0 milestone Aug 3, 2022
@dra27 dra27 requested a review from abbysmal August 3, 2022 09:15
Copy link
Contributor

@abbysmal abbysmal left a comment

Choose a reason for hiding this comment

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

Hello, thanks you for this re-import!
I think this change is good and make sense, but I fail to link it to the Rust usecase you are talking about.
Why would one want to call caml_thread_initialize right after calling caml_startup and not let it be initialized in the "organic" way through entering the Thread module?
I may be missing a subtlety here.

Nonetheless this change looks good to me and seems correct.

@gadmm
Copy link
Contributor Author

gadmm commented Aug 22, 2022

It is quite specific and I am not sure I explained it well. Boxroot (cf. our ML workshop paper) installs various callbacks using runtime hooks. In multicore this goes well with systhread (I made sure of it), but for existing OCaml (4.x) versions we need to guarantee that systhread does not overwrite hooks. Hence some initialization order issue. Now it would be very nice if the advice I want to give for OCaml 4.x (calling caml_thread_initialize immediately) does not result in broken programs for OCaml 5.

Do you know if the justification mentioned in the documentation comment (cf. #3532) is still valid? Otherwise I must adapt the comment.

@abbysmal
Copy link
Contributor

I see, looking at Boxroot I think I understand the issue here.

It does make sense to me to reinstate it then.

Do you know if the justification mentioned in the documentation comment (cf. #3532) is still valid? Otherwise I must adapt the comment.

You mean the erroneous linking scenario causing a second entry into Thread? I'm not sure, I will check it.

@xavierleroy
Copy link
Contributor

Do you know if the justification mentioned in the documentation comment (cf. #3532) is still valid

I suspect it is still valid, in that the bytecode linker still accepts to link threads.cma twice (but with copious warnings).

@xavierleroy xavierleroy merged commit 1960635 into ocaml:trunk Aug 28, 2022
xavierleroy pushed a commit that referenced this pull request Aug 28, 2022
@xavierleroy
Copy link
Contributor

Cherry-picked to 5.0: c5cda47

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.

None yet

5 participants