sewardj | b121028 | 2007-11-28 01:27:03 +0000 | [diff] [blame] | 1 | #if !defined(_AIX) |
sewardj | c68cbe3 | 2007-11-27 01:59:38 +0000 | [diff] [blame] | 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 | #include <asm/unistd.h> |
| 12 | #include "../drd_clientreq.h" |
| 13 | |
sewardj | c68cbe3 | 2007-11-27 01:59:38 +0000 | [diff] [blame] | 14 | static int s_debug = 0; |
| 15 | |
| 16 | |
| 17 | static int getktid() |
| 18 | { |
sewardj | 82ae77d | 2007-11-27 23:39:13 +0000 | [diff] [blame] | 19 | #ifdef __NR_gettid |
sewardj | c68cbe3 | 2007-11-27 01:59:38 +0000 | [diff] [blame] | 20 | return syscall(__NR_gettid); |
sewardj | 82ae77d | 2007-11-27 23:39:13 +0000 | [diff] [blame] | 21 | #else |
| 22 | return -1; |
| 23 | #endif |
sewardj | c68cbe3 | 2007-11-27 01:59:38 +0000 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | static 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 | |
sewardj | 82ae77d | 2007-11-27 23:39:13 +0000 | [diff] [blame] | 33 | static void print_thread_id(const char* const label) |
sewardj | c68cbe3 | 2007-11-27 01:59:38 +0000 | [diff] [blame] | 34 | { |
| 35 | if (s_debug) |
| 36 | { |
| 37 | char msg[256]; |
sewardj | 82ae77d | 2007-11-27 23:39:13 +0000 | [diff] [blame] | 38 | snprintf(msg, sizeof(msg), |
| 39 | "%spid %d / kernel thread ID %d / Valgrind thread ID %d\n", |
| 40 | label, getpid(), getktid(), getvgtid()); |
sewardj | c68cbe3 | 2007-11-27 01:59:38 +0000 | [diff] [blame] | 41 | write(STDOUT_FILENO, msg, strlen(msg)); |
| 42 | } |
| 43 | } |
| 44 | |
sewardj | 82ae77d | 2007-11-27 23:39:13 +0000 | [diff] [blame] | 45 | static void SignalHandler(const int iSignal) |
| 46 | { |
| 47 | print_thread_id("Signal was delivered to "); |
| 48 | } |
| 49 | |
sewardj | c68cbe3 | 2007-11-27 01:59:38 +0000 | [diff] [blame] | 50 | void* thread_func(void* thread_arg) |
| 51 | { |
sewardj | 82ae77d | 2007-11-27 23:39:13 +0000 | [diff] [blame] | 52 | print_thread_id("thread: "); |
sewardj | c68cbe3 | 2007-11-27 01:59:38 +0000 | [diff] [blame] | 53 | |
sewardj | 82ae77d | 2007-11-27 23:39:13 +0000 | [diff] [blame] | 54 | sleep(10); |
sewardj | c68cbe3 | 2007-11-27 01:59:38 +0000 | [diff] [blame] | 55 | //assert(result < 0 && errno == EINTR); |
| 56 | |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | int 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 | |
sewardj | 82ae77d | 2007-11-27 23:39:13 +0000 | [diff] [blame] | 72 | print_thread_id("main: "); |
sewardj | c68cbe3 | 2007-11-27 01:59:38 +0000 | [diff] [blame] | 73 | |
| 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; |
sewardj | 82ae77d | 2007-11-27 23:39:13 +0000 | [diff] [blame] | 86 | nanosleep(&tsDelay, 0); |
sewardj | c68cbe3 | 2007-11-27 01:59:38 +0000 | [diff] [blame] | 87 | // And send SIGALRM to the thread. |
| 88 | pthread_kill(threadid, SIGALRM); |
| 89 | pthread_join(threadid, 0); |
| 90 | |
| 91 | return 0; |
| 92 | } |
sewardj | b121028 | 2007-11-28 01:27:03 +0000 | [diff] [blame] | 93 | |
| 94 | #else /* !defined(_AIX) */ |
| 95 | #include <stdio.h> |
| 96 | int main ( void ) { |
| 97 | fprintf(stderr, "This test does not compile on AIX5.\n"); |
| 98 | return 0; |
| 99 | } |
| 100 | #endif /* !defined(_AIX) */ |