blob: 85987ab203d15784f1a033644d17296f2b06c8dd [file] [log] [blame]
bart070d1a22009-01-16 12:07:52 +00001#include "../../config.h"
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>
bart070d1a22009-01-16 12:07:52 +000011#ifdef HAVE_ASM_UNISTD_H
12#include <asm/unistd.h> // __NR_gettid
13#endif
bart3c1e9d82008-06-30 17:10:29 +000014#include "../drd.h"
15
sewardjc68cbe32007-11-27 01:59:38 +000016
sewardjc68cbe32007-11-27 01:59:38 +000017static int s_debug = 0;
18
19
20static int getktid()
21{
sewardj82ae77d2007-11-27 23:39:13 +000022#ifdef __NR_gettid
sewardjc68cbe32007-11-27 01:59:38 +000023 return syscall(__NR_gettid);
sewardj82ae77d2007-11-27 23:39:13 +000024#else
25 return -1;
26#endif
sewardjc68cbe32007-11-27 01:59:38 +000027}
28
sewardj82ae77d2007-11-27 23:39:13 +000029static void print_thread_id(const char* const label)
sewardjc68cbe32007-11-27 01:59:38 +000030{
31 if (s_debug)
32 {
33 char msg[256];
sewardj82ae77d2007-11-27 23:39:13 +000034 snprintf(msg, sizeof(msg),
35 "%spid %d / kernel thread ID %d / Valgrind thread ID %d\n",
bartd45d9952009-05-31 18:53:54 +000036 label, getpid(), getktid(), DRD_GET_VALGRIND_THREADID);
sewardjc68cbe32007-11-27 01:59:38 +000037 write(STDOUT_FILENO, msg, strlen(msg));
38 }
39}
40
sewardj82ae77d2007-11-27 23:39:13 +000041static void SignalHandler(const int iSignal)
42{
43 print_thread_id("Signal was delivered to ");
44}
45
sewardjc68cbe32007-11-27 01:59:38 +000046void* thread_func(void* thread_arg)
47{
sewardj82ae77d2007-11-27 23:39:13 +000048 print_thread_id("thread: ");
sewardjc68cbe32007-11-27 01:59:38 +000049
sewardj82ae77d2007-11-27 23:39:13 +000050 sleep(10);
sewardjc68cbe32007-11-27 01:59:38 +000051 //assert(result < 0 && errno == EINTR);
52
53 return 0;
54}
55
56int main(int argc, char** argv)
57{
sewardjc68cbe32007-11-27 01:59:38 +000058 pthread_t threadid;
59 struct timespec tsDelay;
60
61 // Primitive argument parsing.
62 if (argc > 1)
63 s_debug = 1;
64
sewardj82ae77d2007-11-27 23:39:13 +000065 print_thread_id("main: ");
sewardjc68cbe32007-11-27 01:59:38 +000066
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
bart12c4b342012-02-02 10:35:18 +000075 if (pthread_create(&threadid, 0, thread_func, 0) != 0) {
76 fprintf(stderr, "Thread creation failed\n");
77 return 1;
78 }
sewardjc68cbe32007-11-27 01:59:38 +000079 // Wait until the thread is inside clock_nanosleep().
80 tsDelay.tv_sec = 0;
81 tsDelay.tv_nsec = 20 * 1000 * 1000;
sewardj82ae77d2007-11-27 23:39:13 +000082 nanosleep(&tsDelay, 0);
sewardjc68cbe32007-11-27 01:59:38 +000083 // And send SIGALRM to the thread.
84 pthread_kill(threadid, SIGALRM);
85 pthread_join(threadid, 0);
86
87 return 0;
88}