File: sigalrm.c

package info (click to toggle)
valgrind 1%3A3.3.1-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 34,452 kB
  • ctags: 27,778
  • sloc: ansic: 234,398; sh: 14,186; xml: 11,662; perl: 4,410; asm: 3,135; makefile: 3,011; exp: 625; cpp: 255; haskell: 195
file content (100 lines) | stat: -rw-r--r-- 1,917 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#if !defined(_AIX)
#include <assert.h>
#include <errno.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <asm/unistd.h>
#include "../drd_clientreq.h"

static int s_debug = 0;


static int getktid()
{
#ifdef __NR_gettid
  return syscall(__NR_gettid);
#else
  return -1;
#endif
}

static int getvgtid()
{
  int res;
  VALGRIND_DO_CLIENT_REQUEST(res, 0, VG_USERREQ__GET_THREAD_SELF, 0, 0, 0,0,0);
  return res;
}

static void print_thread_id(const char* const label)
{
  if (s_debug)
  {
    char msg[256];
    snprintf(msg, sizeof(msg),
             "%spid %d / kernel thread ID %d / Valgrind thread ID %d\n",
             label, getpid(), getktid(), getvgtid());
    write(STDOUT_FILENO, msg, strlen(msg));
  }
}

static void SignalHandler(const int iSignal)
{
  print_thread_id("Signal was delivered to ");
}

void* thread_func(void* thread_arg)
{
  print_thread_id("thread: ");

  sleep(10);
  //assert(result < 0 && errno == EINTR);

  return 0;
}

int main(int argc, char** argv)
{
  int vgthreadid;
  pthread_t threadid;
  struct timespec tsDelay;

  // Primitive argument parsing.
  if (argc > 1)
    s_debug = 1;

  vgthreadid = getvgtid();

  print_thread_id("main: ");

  {
    struct sigaction sa;
    memset(&sa, 0, sizeof(sa));
    sa.sa_handler = &SignalHandler;
    sigemptyset(&sa.sa_mask);
    sigaction(SIGALRM, &sa, 0);
  }

  pthread_create(&threadid, 0, thread_func, 0);
  // Wait until the thread is inside clock_nanosleep().
  tsDelay.tv_sec = 0;
  tsDelay.tv_nsec = 20 * 1000 * 1000;
  nanosleep(&tsDelay, 0);
  // And send SIGALRM to the thread.
  pthread_kill(threadid, SIGALRM);
  pthread_join(threadid, 0);

  return 0;
}

#else /* !defined(_AIX) */
#include <stdio.h>
int main ( void ) {
  fprintf(stderr, "This test does not compile on AIX5.\n");
  return 0;
}
#endif /* !defined(_AIX) */