~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to pstack/linuxthreads.c

  • Committer: brian
  • Date: 2008-06-25 05:29:13 UTC
  • Revision ID: brian@localhost.localdomain-20080625052913-6upwo0jsrl4lnapl
clean slate

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Header$ */
 
2
 
 
3
/*
 
4
 * LinuxThreads specific stuff.
 
5
 */
 
6
 
 
7
#include        <sys/types.h>
 
8
 
 
9
#include        <assert.h>
 
10
#include        <limits.h>      /* PTHREAD_THREADS_MAX */
 
11
#include        <pthread.h>
 
12
#include        <stdio.h>
 
13
#include        <unistd.h>
 
14
#include        <signal.h>
 
15
#include        <sched.h>
 
16
 
 
17
#include        "linuxthreads.h"
 
18
 
 
19
#define AT_INT(intval)  *((int32_t*)(intval))
 
20
 
 
21
/*
 
22
 * Internal LinuxThreads variables.
 
23
 * Official interface exposed to GDB.
 
24
 */
 
25
#if 1
 
26
extern volatile int             __pthread_threads_debug;
 
27
extern volatile char            __pthread_handles;
 
28
extern char                     __pthread_initial_thread;
 
29
/*extern volatile       Elf32_Sym*      __pthread_manager_thread;*/
 
30
extern const int                __pthread_sizeof_handle;
 
31
extern const int                __pthread_offsetof_descr;
 
32
extern const int                __pthread_offsetof_pid;
 
33
extern volatile int             __pthread_handles_num;
 
34
#endif /* 0 */
 
35
 
 
36
/*
 
37
 * Notify others.
 
38
 */
 
39
int
 
40
linuxthreads_notify_others(     const int       signotify)
 
41
{
 
42
        const pid_t                     mypid = getpid();
 
43
        //const pthread_t                       mytid = pthread_self();
 
44
        int                             i;
 
45
        int                             threadcount = 0;
 
46
        int                             threads[PTHREAD_THREADS_MAX];
 
47
        int                             pid;
 
48
 
 
49
        TRACE_FPRINTF((stderr, "theadcount:%d\n", __pthread_handles_num));
 
50
        if (__pthread_handles_num==2) {
 
51
                /* no threads beside the initial thread */
 
52
                return 0;
 
53
        }
 
54
        /*assert(maxthreads>=3);
 
55
        assert(maxthreads>=__pthread_handles_num+2);*/
 
56
 
 
57
        // take the initial thread with us
 
58
        pid = AT_INT(&__pthread_initial_thread + __pthread_offsetof_pid);
 
59
        if (pid!=mypid && pid!=0)
 
60
                threads[threadcount++] = pid;
 
61
        // don't know why, but always handles[0]==handles[1]
 
62
        for (i=1; i<__pthread_handles_num; ++i) {
 
63
                const int descr = AT_INT(&__pthread_handles+i*__pthread_sizeof_handle+__pthread_offsetof_descr);
 
64
                assert(descr!=0);
 
65
                pid = AT_INT(descr+__pthread_offsetof_pid);
 
66
                if (pid!=mypid && pid!=0)
 
67
                        threads[threadcount++] = pid;
 
68
        }
 
69
        /* TRACE_FPRINTF((stderr, "Stopping threads...")); */
 
70
        //for (i=0; i<threadcount; ++i) {
 
71
        //      /* TRACE_FPRINTF((stderr, "%d ", threads[i])); */
 
72
        //      fflush(stdout);
 
73
        //      kill(threads[i], SIGSTOP);      /* Tell thread to stop */
 
74
        //}
 
75
        /* TRACE_FPRINTF((stderr, " done!\n")); */
 
76
        for (i=0; i<threadcount; ++i) {
 
77
                TRACE_FPRINTF((stderr, "--- NOTIFYING %d\n", threads[i]));
 
78
                kill(threads[i], signotify);    /* Tell to print stack trace */
 
79
                /* TRACE_FPRINTF((stderr, "--- WAITING FOR %d\n", threads[i])); */
 
80
                /*pause();               Wait for confirmation. */
 
81
        }
 
82
        for (i=0; i<threadcount; ++i)
 
83
                sched_yield();
 
84
        for (i=0; i<threadcount; ++i) {
 
85
                TRACE_FPRINTF((stderr, "--- KILLING %d\n", threads[i]));
 
86
                kill(threads[i], SIGKILL);      /* Tell thread die :) */
 
87
        }
 
88
        return __pthread_handles_num;
 
89
}
 
90