akaros/user/utest/signal.c
<<
>>
Prefs
   1#include <utest/utest.h>
   2#include <pthread.h>
   3
   4TEST_SUITE("SIGNALS");
   5
   6/* <--- Begin definition of test cases ---> */
   7
   8bool test_sigmask(void)
   9{
  10        int count = 0;
  11        pthread_t sigphandle;
  12        void *thread_handler(void *arg)
  13        {
  14                sigset_t s;
  15                sigemptyset(&s);
  16                sigaddset(&s, SIGUSR2);
  17                pthread_sigmask(SIG_BLOCK, &s, NULL);
  18                for (int i=0; i<100000; i++)
  19                        pthread_yield();
  20                return 0;
  21        }
  22        void signal_handler(int signo)
  23        {
  24                sigphandle = pthread_self();
  25                __sync_fetch_and_add(&count, 1);
  26        }
  27
  28        struct sigaction sigact = {.sa_handler = signal_handler, 0};
  29        sigaction(SIGUSR1, &sigact, 0);
  30        sigaction(SIGUSR2, &sigact, 0);
  31
  32        pthread_t phandle;
  33        pthread_create(&phandle, NULL, thread_handler, NULL);
  34        for (int i=0; i<100; i++)
  35                pthread_yield();
  36        pthread_kill(phandle, SIGUSR1);
  37        pthread_kill(phandle, SIGUSR2);
  38        pthread_join(phandle, NULL);
  39
  40        UT_ASSERT_M("Should only receive one signal", count == 1); 
  41        UT_ASSERT_M("Signal handler run on wrong thread", sigphandle ==
  42                    phandle); 
  43        return true;
  44}
  45
  46/* <--- End definition of test cases ---> */
  47
  48struct utest utests[] = {
  49        UTEST_REG(sigmask),
  50};
  51int num_utests = sizeof(utests) / sizeof(struct utest);
  52
  53int main(int argc, char *argv[]) {
  54        char **whitelist = &argv[1];
  55        int whitelist_len = argc - 1;
  56
  57        RUN_TEST_SUITE(utests, num_utests, whitelist, whitelist_len);
  58}
  59
  60
  61