blob: 2023cf35c5b18d1e45fecd3d12e174b186a31a66 [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>
bart3c1e9d82008-06-30 17:10:29 +000012#include "../drd.h"
13
sewardjc68cbe32007-11-27 01:59:38 +000014
sewardjc68cbe32007-11-27 01:59:38 +000015static int s_debug = 0;
16
17
18static int getktid()
19{
sewardj82ae77d2007-11-27 23:39:13 +000020#ifdef __NR_gettid
sewardjc68cbe32007-11-27 01:59:38 +000021 return syscall(__NR_gettid);
sewardj82ae77d2007-11-27 23:39:13 +000022#else
23 return -1;
24#endif
sewardjc68cbe32007-11-27 01:59:38 +000025}
26
sewardj82ae77d2007-11-27 23:39:13 +000027static void print_thread_id(const char* const label)
sewardjc68cbe32007-11-27 01:59:38 +000028{
29 if (s_debug)
30 {
31 char msg[256];
sewardj82ae77d2007-11-27 23:39:13 +000032 snprintf(msg, sizeof(msg),
33 "%spid %d / kernel thread ID %d / Valgrind thread ID %d\n",
bart5f57be92008-07-01 08:48:56 +000034 label, getpid(), getktid(), vg_get_valgrind_threadid());
sewardjc68cbe32007-11-27 01:59:38 +000035 write(STDOUT_FILENO, msg, strlen(msg));
36 }
37}
38
sewardj82ae77d2007-11-27 23:39:13 +000039static void SignalHandler(const int iSignal)
40{
41 print_thread_id("Signal was delivered to ");
42}
43
sewardjc68cbe32007-11-27 01:59:38 +000044void* thread_func(void* thread_arg)
45{
sewardj82ae77d2007-11-27 23:39:13 +000046 print_thread_id("thread: ");
sewardjc68cbe32007-11-27 01:59:38 +000047
sewardj82ae77d2007-11-27 23:39:13 +000048 sleep(10);
sewardjc68cbe32007-11-27 01:59:38 +000049 //assert(result < 0 && errno == EINTR);
50
51 return 0;
52}
53
54int main(int argc, char** argv)
55{
56 int vgthreadid;
57 pthread_t threadid;
58 struct timespec tsDelay;
59
60 // Primitive argument parsing.
61 if (argc > 1)
62 s_debug = 1;
63
bart5f57be92008-07-01 08:48:56 +000064 vgthreadid = vg_get_valgrind_threadid();
sewardjc68cbe32007-11-27 01:59:38 +000065
sewardj82ae77d2007-11-27 23:39:13 +000066 print_thread_id("main: ");
sewardjc68cbe32007-11-27 01:59:38 +000067
68 {
69 struct sigaction sa;
70 memset(&sa, 0, sizeof(sa));
71 sa.sa_handler = &SignalHandler;
72 sigemptyset(&sa.sa_mask);
73 sigaction(SIGALRM, &sa, 0);
74 }
75
76 pthread_create(&threadid, 0, thread_func, 0);
77 // Wait until the thread is inside clock_nanosleep().
78 tsDelay.tv_sec = 0;
79 tsDelay.tv_nsec = 20 * 1000 * 1000;
sewardj82ae77d2007-11-27 23:39:13 +000080 nanosleep(&tsDelay, 0);
sewardjc68cbe32007-11-27 01:59:38 +000081 // And send SIGALRM to the thread.
82 pthread_kill(threadid, SIGALRM);
83 pthread_join(threadid, 0);
84
85 return 0;
86}
sewardjb1210282007-11-28 01:27:03 +000087
88#else /* !defined(_AIX) */
89#include <stdio.h>
90int main ( void ) {
91 fprintf(stderr, "This test does not compile on AIX5.\n");
92 return 0;
93}
94#endif /* !defined(_AIX) */