blob: e1d0a6838a3aba6f20b22a9faf683cb8ddacb281 [file] [log] [blame]
sewardjb1210282007-11-28 01:27:03 +00001#if !defined(_AIX)
sewardjc68cbe32007-11-27 01:59:38 +00002#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#include <asm/unistd.h>
12#include "../drd_clientreq.h"
13
sewardjc68cbe32007-11-27 01:59:38 +000014static int s_debug = 0;
15
16
17static int getktid()
18{
sewardj82ae77d2007-11-27 23:39:13 +000019#ifdef __NR_gettid
sewardjc68cbe32007-11-27 01:59:38 +000020 return syscall(__NR_gettid);
sewardj82ae77d2007-11-27 23:39:13 +000021#else
22 return -1;
23#endif
sewardjc68cbe32007-11-27 01:59:38 +000024}
25
26static int getvgtid()
27{
28 int res;
29 VALGRIND_DO_CLIENT_REQUEST(res, 0, VG_USERREQ__GET_THREAD_SELF, 0, 0, 0,0,0);
30 return res;
31}
32
sewardj82ae77d2007-11-27 23:39:13 +000033static void print_thread_id(const char* const label)
sewardjc68cbe32007-11-27 01:59:38 +000034{
35 if (s_debug)
36 {
37 char msg[256];
sewardj82ae77d2007-11-27 23:39:13 +000038 snprintf(msg, sizeof(msg),
39 "%spid %d / kernel thread ID %d / Valgrind thread ID %d\n",
40 label, getpid(), getktid(), getvgtid());
sewardjc68cbe32007-11-27 01:59:38 +000041 write(STDOUT_FILENO, msg, strlen(msg));
42 }
43}
44
sewardj82ae77d2007-11-27 23:39:13 +000045static void SignalHandler(const int iSignal)
46{
47 print_thread_id("Signal was delivered to ");
48}
49
sewardjc68cbe32007-11-27 01:59:38 +000050void* thread_func(void* thread_arg)
51{
sewardj82ae77d2007-11-27 23:39:13 +000052 print_thread_id("thread: ");
sewardjc68cbe32007-11-27 01:59:38 +000053
sewardj82ae77d2007-11-27 23:39:13 +000054 sleep(10);
sewardjc68cbe32007-11-27 01:59:38 +000055 //assert(result < 0 && errno == EINTR);
56
57 return 0;
58}
59
60int main(int argc, char** argv)
61{
62 int vgthreadid;
63 pthread_t threadid;
64 struct timespec tsDelay;
65
66 // Primitive argument parsing.
67 if (argc > 1)
68 s_debug = 1;
69
70 vgthreadid = getvgtid();
71
sewardj82ae77d2007-11-27 23:39:13 +000072 print_thread_id("main: ");
sewardjc68cbe32007-11-27 01:59:38 +000073
74 {
75 struct sigaction sa;
76 memset(&sa, 0, sizeof(sa));
77 sa.sa_handler = &SignalHandler;
78 sigemptyset(&sa.sa_mask);
79 sigaction(SIGALRM, &sa, 0);
80 }
81
82 pthread_create(&threadid, 0, thread_func, 0);
83 // Wait until the thread is inside clock_nanosleep().
84 tsDelay.tv_sec = 0;
85 tsDelay.tv_nsec = 20 * 1000 * 1000;
sewardj82ae77d2007-11-27 23:39:13 +000086 nanosleep(&tsDelay, 0);
sewardjc68cbe32007-11-27 01:59:38 +000087 // And send SIGALRM to the thread.
88 pthread_kill(threadid, SIGALRM);
89 pthread_join(threadid, 0);
90
91 return 0;
92}
sewardjb1210282007-11-28 01:27:03 +000093
94#else /* !defined(_AIX) */
95#include <stdio.h>
96int main ( void ) {
97 fprintf(stderr, "This test does not compile on AIX5.\n");
98 return 0;
99}
100#endif /* !defined(_AIX) */