@@ -64,16 +64,21 @@ fn panic_debug(line_no int, file string, mod string, fn_name string, s string) {
6464 } $else {
6565 // vfmt off
6666 // Note: be carefull to not allocate here, avoid string interpolation
67+ flush_stdout ()
6768 eprintln ('================ V panic ================' )
6869 eprint (' module: ' ); eprintln (mod)
6970 eprint (' function: ' ); eprint (fn_name); eprintln ('()' )
7071 eprint (' message: ' ); eprintln (s)
7172 eprint (' file: ' ); eprint (file); eprint (':' );
72- C.fprintf (C.stderr, c '%d \n ' , line_no)
73+ C.fprintf (C.stderr, c '%d \n ' , line_no)
7374 eprint (' v hash: ' ); eprintln (vcurrent_hash ())
75+ $if ! vinix && ! native {
76+ eprint (' pid: ' ); C.fprintf (C.stderr, c '%p \n ' , voidptr (v_getpid ()))
77+ eprint (' tid: ' ); C.fprintf (C.stderr, c '%p \n ' , voidptr (v_gettid ()))
78+ }
7479 eprintln ('=========================================' )
75- // vfmt on
7680 flush_stdout ()
81+ // vfmt on
7782 $if native {
7883 C.exit (1 ) // TODO: native backtraces
7984 } $else $if exit_after_panic_message ? {
@@ -129,11 +134,18 @@ pub fn panic(s string) {
129134 $if freestanding {
130135 bare_panic (s)
131136 } $else {
137+ // vfmt off
138+ flush_stdout ()
132139 eprint ('V panic: ' )
133140 eprintln (s)
134- eprint ('v hash: ' )
141+ eprint (' v hash: ' )
135142 eprintln (vcurrent_hash ())
143+ $if ! vinix && ! native {
144+ eprint (' pid: ' ); C.fprintf (C.stderr, c '%p \n ' , voidptr (v_getpid ()))
145+ eprint (' tid: ' ); C.fprintf (C.stderr, c '%p \n ' , voidptr (v_gettid ()))
146+ }
136147 flush_stdout ()
148+ // vfmt on
137149 $if native {
138150 C.exit (1 ) // TODO: native backtraces
139151 } $else $if exit_after_panic_message ? {
@@ -948,3 +960,34 @@ pub fn arguments() []string {
948960 }
949961 return res
950962}
963+
964+ // v_getpid returns a process identifier. It is a number that is guaranteed to
965+ // remain the same while the current process is running. It may or may not be
966+ // equal to the value of v_gettid(). Note: it is *NOT equal on Windows*.
967+ pub fn v_getpid () u64 {
968+ $if windows {
969+ return u64 (C.GetCurrentProcessId ())
970+ } $else {
971+ return u64 (C.getpid ())
972+ }
973+ }
974+
975+ // v_gettid retuns a thread identifier. It is a number that is guaranteed to not
976+ // change, while the current thread is running. Different threads, running at
977+ // the same time in the same process, have different thread ids. There is no
978+ // such guarantee for threads running in different processes.
979+ // Important: this will be the same number returned by v_getpid(), but only on
980+ // non windows systems, when the current thread is the main one. It is best to
981+ // *avoid relying on this equivalence*, and use v_gettid and v_getpid only for
982+ // tracing and debugging multithreaded issues, but *NOT for logic decisions*.
983+ pub fn v_gettid () u64 {
984+ $if windows {
985+ return u64 (C.GetCurrentThreadId ())
986+ } $else $if linux && ! musl ? {
987+ return u64 (C.gettid ())
988+ } $else $if threads {
989+ return u64 (C.pthread_self ())
990+ } $else {
991+ return v_getpid ()
992+ }
993+ }
0 commit comments