1 /* Copyright (c) 2014 The Regents of the University of California
2 * Kevin Klues <klueska@cs.berkeley.edu>
3 * See LICENSE for details.
13 // Signal handler run in vcore context which redirects signals to userspace by
14 // waking a thread waiting on a futex. Note, this does not guarantee that every
15 // signal that comes through this handler will be noticed or processed by the
18 void sig_handler(int signr) {
20 futex(&__sigpending, FUTEX_WAKE, INT_MAX, NULL, NULL, 0);
23 // User level thread waiting on a futex to count signals
25 void *count_signals(void *arg) {
27 futex(&__sigpending, FUTEX_WAIT, 0, NULL, NULL, 0);
28 __sync_fetch_and_add(&count, 1);
33 // Thread spamming us with signals
34 void *sig_thread(void *arg) {
35 int *done = (int*)arg;
36 struct sigaction sigact = {.sa_handler = sig_handler, 0};
37 sigaction(SIGUSR1, &sigact, 0);
39 kill(getpid(), SIGUSR1);
41 if (*done) return NULL;
45 int main(int argc, char **argv)
48 pthread_t pth_handle, pth_handle2;
49 // Spawn off a thread to spam us with signals
50 pthread_create(&pth_handle, NULL, &sig_thread, &done);
51 // Spawn off a thread to process those signals
52 pthread_create(&pth_handle2, NULL, &count_signals, NULL);
53 // Sleep for 3 seconds by timing out on a futex
55 struct timespec timeout = {.tv_sec = 3, 0};
56 futex(&dummy, FUTEX_WAIT, 0, &timeout, NULL, 0);
57 // Force the signal tread to exit
60 pthread_join(pth_handle, NULL);
61 printf("count: %d\n", count);