+#if defined(LIBVLC_USE_PTHREAD)
+static inline unsigned long vlc_threadid (void)
+{
+ union { pthread_t th; unsigned long int i; } v = { };
+ v.th = pthread_self ();
+ return v.i;
+}
+
+#if defined(HAVE_EXECINFO_H) && defined(HAVE_BACKTRACE)
+# include <execinfo.h>
+#endif
+
+/*****************************************************************************
+ * vlc_thread_fatal: Report an error from the threading layer
+ *****************************************************************************
+ * This is mostly meant for debugging.
+ *****************************************************************************/
+void vlc_pthread_fatal (const char *action, int error,
+ const char *file, unsigned line)
+{
+ fprintf (stderr, "LibVLC fatal error %s in thread %lu at %s:%u: %d\n",
+ action, vlc_threadid (), file, line, error);
+
+ /* Sometimes strerror_r() crashes too, so make sure we print an error
+ * message before we invoke it */
+#ifdef __GLIBC__
+ /* Avoid the strerror_r() prototype brain damage in glibc */
+ errno = error;
+ fprintf (stderr, " Error message: %m at:\n");
+#else
+ char buf[1000];
+ const char *msg;
+
+ switch (strerror_r (error, buf, sizeof (buf)))
+ {
+ case 0:
+ msg = buf;
+ break;
+ case ERANGE: /* should never happen */
+ msg = "unknwon (too big to display)";
+ break;
+ default:
+ msg = "unknown (invalid error number)";
+ break;
+ }
+ fprintf (stderr, " Error message: %s\n", msg);
+#endif
+ fflush (stderr);
+
+#ifdef HAVE_BACKTRACE
+ void *stack[20];
+ int len = backtrace (stack, sizeof (stack) / sizeof (stack[0]));
+ backtrace_symbols_fd (stack, len, 2);
+#endif
+
+ abort ();
+}
+#else
+void vlc_pthread_fatal (const char *action, int error,
+ const char *file, unsigned line)
+{
+ (void)action; (void)error; (void)file; (void)line;
+ abort();
+}
+#endif
+