Home | History | Annotate | Download | only in tests
      1 #include "../../config.h"
      2 #include <assert.h>
      3 #include <errno.h>
      4 #include <pthread.h>
      5 #include <signal.h>
      6 #include <stdio.h>
      7 #include <stdlib.h>
      8 #include <string.h>
      9 #include <time.h>
     10 #include <unistd.h>
     11 #ifdef HAVE_ASM_UNISTD_H
     12 #include <asm/unistd.h> // __NR_gettid
     13 #endif
     14 #include "../drd.h"
     15 
     16 
     17 static int s_debug = 0;
     18 
     19 
     20 static int getktid()
     21 {
     22 #ifdef __NR_gettid
     23   return syscall(__NR_gettid);
     24 #else
     25   return -1;
     26 #endif
     27 }
     28 
     29 static void print_thread_id(const char* const label)
     30 {
     31   if (s_debug)
     32   {
     33     char msg[256];
     34     snprintf(msg, sizeof(msg),
     35              "%spid %d / kernel thread ID %d / Valgrind thread ID %d\n",
     36              label, getpid(), getktid(), DRD_GET_VALGRIND_THREADID);
     37     write(STDOUT_FILENO, msg, strlen(msg));
     38   }
     39 }
     40 
     41 static void SignalHandler(const int iSignal)
     42 {
     43   print_thread_id("Signal was delivered to ");
     44 }
     45 
     46 void* thread_func(void* thread_arg)
     47 {
     48   print_thread_id("thread: ");
     49 
     50   sleep(10);
     51   //assert(result < 0 && errno == EINTR);
     52 
     53   return 0;
     54 }
     55 
     56 int main(int argc, char** argv)
     57 {
     58   pthread_t threadid;
     59   struct timespec tsDelay;
     60 
     61   // Primitive argument parsing.
     62   if (argc > 1)
     63     s_debug = 1;
     64 
     65   print_thread_id("main: ");
     66 
     67   {
     68     struct sigaction sa;
     69     memset(&sa, 0, sizeof(sa));
     70     sa.sa_handler = &SignalHandler;
     71     sigemptyset(&sa.sa_mask);
     72     sigaction(SIGALRM, &sa, 0);
     73   }
     74 
     75   pthread_create(&threadid, 0, thread_func, 0);
     76   // Wait until the thread is inside clock_nanosleep().
     77   tsDelay.tv_sec = 0;
     78   tsDelay.tv_nsec = 20 * 1000 * 1000;
     79   nanosleep(&tsDelay, 0);
     80   // And send SIGALRM to the thread.
     81   pthread_kill(threadid, SIGALRM);
     82   pthread_join(threadid, 0);
     83 
     84   return 0;
     85 }
     86