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

Expand API to Wait for Signals #14

Closed
johnpatek opened this issue Jan 4, 2025 · 1 comment
Closed

Expand API to Wait for Signals #14

johnpatek opened this issue Jan 4, 2025 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@johnpatek
Copy link
Member

johnpatek commented Jan 4, 2025

Linux provides an API for blocking execution until a signal is received. This would be desirable in a program where most of the execution occurs in background threads and the main thread waits until it's time to shut down.

API Changes

For the sake of simplicity and flexibility, the function should allow the function to wait on multiple signums. This would add 3 new entries to the C API:

int sigfn_wait(const int *signums, size_t count, int *received);
int sigfn_wait_for(const int *signums, size_t count, int *received, const timeval* duration);
int sigfn_wait_until(const int *signums, size_t count, int *received, const timeval* time_point);

For the timed wait functions, it might make sense to introduce a new status code SIGFN_TIMEOUT instead of trying to populate the received value with a designated value to indicate no signal was received.

And 3 equivalent functions for the C++ API.

Implementation Suggestions

A straightforward way to do this would to recreate the signal handling pattern from golang:

package main

import (
	"fmt"
	"os"
	"os/signal"
)

func main() {
	// Set up channel on which to send signal notifications.
	// We must use a buffered channel or risk missing the signal
	// if we're not ready to receive when the signal is sent.
	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt)

	// Block until a signal is received.
	s := <-c
	fmt.Println("Got signal:", s)
}

This could be accomplished in the following steps:

  1. Define a signal handler that writes the signum to a channel
  2. Register a sigfn_handle for each signum in the set to invoke the handler
  3. Receive the sigum from the channel in the main thread after it has been written by the interrupt handler
@johnpatek johnpatek added the enhancement New feature or request label Jan 4, 2025
@johnpatek johnpatek self-assigned this Jan 4, 2025
@johnpatek
Copy link
Member Author

This was done in #15

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant