Skip to content

Add Unix.sigwait and implement Thread signal-related functions with Unix#13429

Merged
xavierleroy merged 5 commits intoocaml:trunkfrom
xavierleroy:signals-sigthreads
Sep 11, 2024
Merged

Add Unix.sigwait and implement Thread signal-related functions with Unix#13429
xavierleroy merged 5 commits intoocaml:trunkfrom
xavierleroy:signals-sigthreads

Conversation

@xavierleroy
Copy link
Copy Markdown
Contributor

The sigwait system call is currently exposed in the Thread interface, but it's more generally useful, so add it to Unix.

As explained in the individual commits, Thread.sigmask is now functionally equivalent to Unix.sigprocmask, and Thread.wait_signal to the new Unix.sigwait, so we can implement the Thread functions in terms of the Unix functions, getting rid of some stub code in otherlibs/systhreads.

This will also help with #13348 .

Copy link
Copy Markdown
Member

@gasche gasche left a comment

Choose a reason for hiding this comment

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

Looks fine to me, thanks! Approved.


decode_sigset(sigs, &set);
caml_enter_blocking_section();
retcode = sigwait(&set, &signo);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Note: in st_posix.h, the use of this function was conditioned on the HAS_SIGWAIT macro, correesponding to a AC_CHECK_FUNC([sigwait]...) check in the configure. Now it is only conditioned on the POSIX_SIGNALS macro, but the definition of POSIX_SIGNALS only checks for sigaction and sigprocmask. So now we are (newly) assuming that if those two are available, then sigwait also is (and, above, sigsuspend).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

See my helpful commit message in b62ba96. This kind of per-function configure test was appropriate in the 1990's, but nowadays either you're on Windows or you have full POSIX.1-2008 (or at the very least -2001) support.

caml_enter_blocking_section();
retcode = sigwait(&set, &signo);
caml_leave_blocking_section();
if (retcode == -1) caml_uerror("sigwait", Nothing);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This logic is wrong. sigwait either returns zero or a positive errno.

Suggested change
if (retcode == -1) caml_uerror("sigwait", Nothing);
if (retcode != 0) caml_uerror("sigwait", Nothing);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Well spotted, thanks. The correct fix uses caml_unix_error, see b62ba96


val sigprocmask : mode:sigprocmask_command -> int list -> int list
(** [sigprocmask ~mode sigs] changes the set of blocked signals.
(** [sigprocmask mode sigs] changes the set of blocked signals.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why did you remove the tilde? It's a labelled argument.

Copy link
Copy Markdown
Contributor Author

@xavierleroy xavierleroy Sep 11, 2024

Choose a reason for hiding this comment

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

It was a cut-and-paste error, obviously. Fixed.

In preparation for subsuming `Thread.wait_signal`.
The signal mask is per thread and per domain.
It used to be that `Unix.sigprocmask` calls `sigprocmask` while
`Thread.sigmask` calls `pthread_sigmask`.  However, since OCaml 5,
`Unix.sigprocmask` also calls `pthread_sigmask`, so the separate
implementation in `Thread.sigmask` is no longer needed.

Likewise, the previous commit introduced `Unix.sigwait`, with exactly
the same functionality as `Thread.wait_signal`.
`sigwait` is POSIX.1-2001.  If we have POSIX signals, we have `sigwait`.
Copy link
Copy Markdown

@ghost ghost left a comment

Choose a reason for hiding this comment

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

Looking good now.

Also: bow to check-typo.
@xavierleroy
Copy link
Copy Markdown
Contributor Author

Thanks for the reviews and the approvals. I feel brave enough to merge.

@xavierleroy xavierleroy merged commit 720db89 into ocaml:trunk Sep 11, 2024
@xavierleroy xavierleroy deleted the signals-sigthreads branch September 11, 2024 16:11
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.

3 participants