mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-23 22:02:27 +00:00
Make signal handling work well across platforms
- Fix sigsuspend() on XNU - Fix strsignal() on non-Linux - Add unit tests for strsignal() - Add unit tests for setitimer() - Add unit tests for sigsuspend() - Rewrite setitimer() for New Technology - Rewrite nanosleep() for New Technology - Polyfill SIGALRM on the New Technology - select(0,0,0,0) on NT now calls pause() - Remove some NTDLL calls that aren't needed - Polyfill SA_NOCLDWAIT on the New Technology - Polyfill SA_RESETHAND on the New Technology - Polyfill sigprocmask() on the New Technology - Polyfill SIGCHLD+SIG_IGN on the New Technology - Polyfill SA_RESTART masking on the New Technology - Deliver console signals from main thread on New Technology - Document SA_RESTART behavior w/ @sarestartable / @norestart - System call trace in MODE=dbg now prints inherited FDs and signal mask
This commit is contained in:
parent
3b9e66ecba
commit
072e1d2910
82 changed files with 1388 additions and 450 deletions
72
examples/setitimer.c
Normal file
72
examples/setitimer.c
Normal file
|
@ -0,0 +1,72 @@
|
|||
#if 0
|
||||
/*─────────────────────────────────────────────────────────────────╗
|
||||
│ To the extent possible under law, Justine Tunney has waived │
|
||||
│ all copyright and related or neighboring rights to this file, │
|
||||
│ as it is written in the following disclaimers: │
|
||||
│ • http://unlicense.org/ │
|
||||
│ • http://creativecommons.org/publicdomain/zero/1.0/ │
|
||||
╚─────────────────────────────────────────────────────────────────*/
|
||||
#endif
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/struct/itimerval.h"
|
||||
#include "libc/calls/struct/sigaction.h"
|
||||
#include "libc/calls/struct/siginfo.h"
|
||||
#include "libc/calls/ucontext.h"
|
||||
#include "libc/log/check.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/sysv/consts/itimer.h"
|
||||
#include "libc/sysv/consts/sa.h"
|
||||
#include "libc/sysv/consts/sig.h"
|
||||
#include "libc/time/time.h"
|
||||
|
||||
volatile bool gotalrm;
|
||||
|
||||
void OnSigAlrm(int sig, siginfo_t *si, ucontext_t *ctx) {
|
||||
gotalrm = true;
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
printf("first tutorial: set singleshot alarm for 1.5 seconds from now\n");
|
||||
|
||||
// setup alarm in 1.5 seconds
|
||||
// this timer tears itself down once it's handled
|
||||
struct itimerval shot = {{0, 0}, {1, 500000}};
|
||||
struct sigaction handler = {.sa_sigaction = OnSigAlrm,
|
||||
.sa_flags = SA_RESETHAND | SA_SIGINFO};
|
||||
CHECK_EQ(0, sigaction(SIGALRM, &handler, 0));
|
||||
CHECK_EQ(0, setitimer(ITIMER_REAL, &shot, 0));
|
||||
|
||||
// wait for alarm
|
||||
pause();
|
||||
printf("got singleshot alarm!\n\n");
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
printf("second tutorial: interval timer\n");
|
||||
|
||||
// setup timer every 1.5 seconds
|
||||
struct sigaction oldalrm;
|
||||
struct sigaction sigalrm = {.sa_sigaction = OnSigAlrm,
|
||||
.sa_flags = SA_SIGINFO};
|
||||
CHECK_EQ(0, sigaction(SIGALRM, &sigalrm, &oldalrm));
|
||||
struct itimerval oldtimer;
|
||||
struct itimerval timer = {{1, 500000}, {1, 500000}};
|
||||
CHECK_EQ(0, setitimer(ITIMER_REAL, &timer, &oldtimer));
|
||||
|
||||
// wait for three timeouts
|
||||
int i = 0;
|
||||
int n = 3;
|
||||
while (i < n) {
|
||||
pause();
|
||||
if (gotalrm) {
|
||||
++i;
|
||||
printf("got timeout %d out of %d\n", i, n);
|
||||
gotalrm = false;
|
||||
}
|
||||
}
|
||||
|
||||
// teardown timer
|
||||
CHECK_EQ(0, setitimer(ITIMER_REAL, &oldtimer, 0));
|
||||
CHECK_EQ(0, sigaction(SIGALRM, &oldalrm, 0));
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue