tonysdg
tonysdg

Reputation: 1385

Suspending thread execution within signal handler

This is a bit of a follow-up to this question.

Suppose I use sigaction to set up a signal handler of my own design. In this signal handler, I access information about the CPU state (the thread's stack pointer, for example) and copy it to a predetermined location in memory. I now want to examine a value on this thread's stack from another thread.

Is there a way to suspend execution of the thread in the signal handler from within the same signal handler, so that I can safely examine this thread's stack from another thread? (Note: I define "safely" here to mean "without worrying about the thread completing or returning from a function.)

If I was outside of the signal handler, I could use sigsupend; however, it's not safe to use within the signal handler according to the GNU documentation. I could also try extending a signal using the method described in this question, but I don't think any of the standard *nix signals will help me here.

Upvotes: 1

Views: 328

Answers (1)

pilcrow
pilcrow

Reputation: 58524

A blocking read on a pipe is async-signal-safe, and provides a convenient way to wake up your "suspended" thread.

E.g.,

static int the_pipe[2] = {-1, -1};  // file descriptors allocated in main via pipe()

static void
siginfo_handler(int s, siginfo_t *si, void *ctx) {
  char c;

  // do something with (ucontext_t *)ctx

  // now wait for someone else to wake us up
  read(the_pipe[0], &c, 1);

  ...
}

Upvotes: 1

Related Questions