1 #include <minix/mthread.h>
6 /*===========================================================================*
8 *===========================================================================*/
10 void mthread_debug_f(const char *file
, int line
, const char *msg
)
12 /* Print debug message */
13 printf("MTH (%s:%d): %s\n", file
, line
, msg
);
17 /*===========================================================================*
19 *===========================================================================*/
21 void mthread_panic_f(const char *file
, int line
, const char *msg
)
23 /* Print panic message to stdout and exit */
28 printf("mthread panic (%s:%d): ", file
, line
);
31 fflush(stdout
); /* Force debug print to screen */
32 *((int *) sf
) = 1; /* Cause segfault to generate trace */
36 void mthread_panic_s(void)
42 *((int *) sf
) = 1; /* Cause segfault to generate trace */
48 /*===========================================================================*
50 *===========================================================================*/
52 void mthread_verify_f(char *file
, int line
)
54 /* Verify library state. It is assumed this function is never called from
55 * a spawned thread, but from the 'main' thread. The library should be
56 * quiescent; no mutexes, conditions, or threads in use. All threads are to
61 int threads_ok
= 1, conditions_ok
= 1, mutexes_ok
= 1, attributes_ok
= 1;
63 for (t
= (mthread_thread_t
) 0; threads_ok
&& t
< no_threads
; t
++) {
64 tcb
= mthread_find_tcb(t
);
65 if (tcb
->m_state
!= MS_DEAD
) threads_ok
= 0;
68 conditions_ok
= mthread_cond_verify();
69 mutexes_ok
= mthread_mutex_verify();
70 attributes_ok
= mthread_attr_verify();
72 printf("(%s:%d) VERIFY ", file
, line
);
73 printf("| threads: %s |", (threads_ok
? "ok": "NOT ok"));
74 printf("| cond: %s |", (conditions_ok
? "ok": "NOT ok"));
75 printf("| mutex: %s |", (mutexes_ok
? "ok": "NOT ok"));
76 printf("| attr: %s |", (attributes_ok
? "ok": "NOT ok"));
79 if(!threads_ok
|| !conditions_ok
|| !mutexes_ok
)
80 mthread_panic("Library state corrupt\n");
84 /*===========================================================================*
86 *===========================================================================*/
87 void mthread_stats(void)
91 int st_run
, st_dead
, st_cond
, st_mutex
, st_exit
;
92 st_run
= st_dead
= st_cond
= st_mutex
= st_exit
= 0;
94 for (t
= (mthread_thread_t
) 0; t
< no_threads
; t
++) {
95 tcb
= mthread_find_tcb(t
);
96 switch(tcb
->m_state
) {
97 case MS_RUNNABLE
: st_run
++; break;
98 case MS_DEAD
: st_dead
++; break;
99 case MS_MUTEX
: st_mutex
++; break;
100 case MS_CONDITION
: st_cond
++; break;
101 case MS_EXITING
: st_exit
++; break;
102 default: mthread_panic("Unknown state");
106 printf("Pool: %-5d In use: %-5d R: %-5d D: %-5d M: %-5d C: %-5d E: %-5d\n",
107 no_threads
, used_threads
, st_run
, st_dead
, st_mutex
, st_cond
,
114 /*===========================================================================*
115 * mthread_stacktrace *
116 *===========================================================================*/
117 void mthread_stacktrace(mthread_thread_t t
)
119 unsigned long bp
, hbp
, pc
;
123 struct stackframe_s
*frame
;
125 printf("thread %d: ", t
);
127 tcb
= mthread_find_tcb(t
);
128 ctx
= &tcb
->m_context
;
129 mtx
= &ctx
->uc_mcontext
;
130 frame
= &mtx
->mc_p_reg
;
134 pc
= ((unsigned long *) bp
)[1];
135 hbp
= ((unsigned long *) bp
)[0];
137 printf("0x%lx ", (unsigned long) pc
);
139 if (hbp
!= 0 && hbp
<= bp
) {
141 printf("0x%lx ", (unsigned long) pc
);
150 /*===========================================================================*
151 * mthread_stacktraces *
152 *===========================================================================*/
153 void mthread_stacktraces(void)
157 mthread_stacktrace(MAIN_THREAD
);
159 for (t
= (mthread_thread_t
) 0; t
< no_threads
; t
++)
160 mthread_stacktrace(t
);