# include <sched.h>
static pthread_mutex_t once_mutex = PTHREAD_MUTEX_INITIALIZER;
+#else
+static vlc_threadvar_t cancel_key;
#endif
/**
return p_root;
}
-#ifndef NDEBUG
+#ifdef HAVE_EXECINFO_H
+# include <execinfo.h>
+#endif
+
/**
- * Object running the current thread
+ * Print a backtrace to the standard error for debugging purpose.
*/
-static vlc_threadvar_t thread_object_key;
-
-vlc_object_t *vlc_threadobj (void)
+void vlc_trace (const char *fn, const char *file, unsigned line)
{
- return vlc_threadvar_get (&thread_object_key);
-}
+ fprintf (stderr, "at %s:%u in %s\n", file, line, fn);
+ fflush (stderr); /* needed before switch to low-level I/O */
+#ifdef HAVE_BACKTRACE
+ void *stack[20];
+ int len = backtrace (stack, sizeof (stack) / sizeof (stack[0]));
+ backtrace_symbols_fd (stack, len, 2);
#endif
+#ifndef WIN32
+ fsync (2);
+#endif
+}
-vlc_threadvar_t msg_context_global_key;
-
-#if defined(LIBVLC_USE_PTHREAD)
static inline unsigned long vlc_threadid (void)
{
+#if defined(LIBVLC_USE_PTHREAD)
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>
+#elif defined (WIN32)
+ return GetCurrentThreadId ();
+
+#else
+ return 0;
+
#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,
+void vlc_thread_fatal (const char *action, int error, const char *function,
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);
+ fprintf (stderr, "LibVLC fatal error %s (%d) in thread %lu ",
+ action, error, vlc_threadid ());
+ vlc_trace (function, file, line);
/* Sometimes strerror_r() crashes too, so make sure we print an error
* message before we invoke it */
/* Avoid the strerror_r() prototype brain damage in glibc */
errno = error;
fprintf (stderr, " Error message: %m at:\n");
-#else
+#elif !defined (WIN32)
char buf[1000];
const char *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();
-}
-
-static vlc_threadvar_t cancel_key;
-#endif
/**
* Per-thread cancellation data
}
/* We should be safe now. Do all the initialization stuff we want. */
-#ifndef NDEBUG
- vlc_threadvar_create( &thread_object_key, NULL );
-#endif
- vlc_threadvar_create( &msg_context_global_key, msg_StackDestroy );
#ifndef LIBVLC_USE_PTHREAD_CANCEL
vlc_threadvar_create( &cancel_key, free );
#endif
vlc_object_release( p_root );
#ifndef LIBVLC_USE_PTHREAD
vlc_threadvar_delete( &cancel_key );
-#endif
- vlc_threadvar_delete( &msg_context_global_key );
-#ifndef NDEBUG
- vlc_threadvar_delete( &thread_object_key );
#endif
}
i_initializations--;
*p_mutex = CreateMutex( 0, FALSE, 0 );
return (*p_mutex != NULL) ? 0 : ENOMEM;
-#elif defined( HAVE_KERNEL_SCHEDULER_H )
- /* check the arguments and whether it's already been initialized */
- if( p_mutex == NULL )
- {
- return B_BAD_VALUE;
- }
-
- if( p_mutex->init == 9999 )
- {
- return EALREADY;
- }
-
- p_mutex->lock = create_sem( 1, "BeMutex" );
- if( p_mutex->lock < B_NO_ERROR )
- {
- return( -1 );
- }
-
- p_mutex->init = 9999;
- return B_OK;
-
#endif
}
CloseHandle( *p_mutex );
-#elif defined( HAVE_KERNEL_SCHEDULER_H )
- if( p_mutex->init == 9999 )
- delete_sem( p_mutex->lock );
-
- p_mutex->init = 0;
-
#endif
}
NULL ); /* unnamed */
return *p_condvar ? 0 : ENOMEM;
-#elif defined( HAVE_KERNEL_SCHEDULER_H )
- if( !p_condvar )
- {
- return B_BAD_VALUE;
- }
-
- if( p_condvar->init == 9999 )
- {
- return EALREADY;
- }
-
- p_condvar->thread = -1;
- p_condvar->init = 9999;
- return 0;
-
#endif
}
CloseHandle( *p_condvar );
-#elif defined( HAVE_KERNEL_SCHEDULER_H )
- p_condvar->init = 0;
-
#endif
}
* memory leaks and the signal functions not working (see Microsoft
* Knowledge Base, article 104641) */
HANDLE hThread;
- vlc_thread_t th = malloc (sizeof (*p_handle));
+ vlc_thread_t th = malloc (sizeof (*th));
if (th == NULL)
return ENOMEM;
free (th);
}
-#elif defined( HAVE_KERNEL_SCHEDULER_H )
- *p_handle = spawn_thread( entry, psz_name, priority, data );
- ret = resume_thread( *p_handle );
-
#endif
return ret;
}
* @param p_result [OUT] pointer to write the thread return value or NULL
* @return 0 on success, a standard error code otherwise.
*/
-int vlc_join (vlc_thread_t handle, void **result)
+void vlc_join (vlc_thread_t handle, void **result)
{
#if defined( LIBVLC_USE_PTHREAD )
- return pthread_join (handle, result);
+ int val = pthread_join (handle, result);
+ if (val)
+ vlc_thread_fatal ("joining thread", val, __func__, __FILE__, __LINE__);
#elif defined( UNDER_CE ) || defined( WIN32 )
do
if (result)
*result = handle->data;
free (handle);
- return 0;
-
-#elif defined( HAVE_KERNEL_SCHEDULER_H )
- int32_t exit_value;
- ret = (B_OK == wait_for_thread( p_priv->thread_id, &exit_value ));
- if( !ret && result )
- *result = (void *)exit_value;
- return ret;
#endif
}
void *(*func) (vlc_object_t *) = ((struct vlc_thread_boot *)data)->entry;
free (data);
-#ifndef NDEBUG
- vlc_threadvar_set (&thread_object_key, obj);
-#endif
msg_Dbg (obj, "thread started");
func (obj);
msg_Dbg (obj, "thread ended");
/*****************************************************************************
* vlc_thread_join: wait until a thread exits, inner version
*****************************************************************************/
-void __vlc_thread_join( vlc_object_t *p_this, const char * psz_file, int i_line )
+void __vlc_thread_join( vlc_object_t *p_this )
{
vlc_object_internals_t *p_priv = vlc_internals( p_this );
- int i_ret = 0;
#if defined( LIBVLC_USE_PTHREAD )
- /* Make sure we do return if we are calling vlc_thread_join()
- * from the joined thread */
- if (pthread_equal (pthread_self (), p_priv->thread_id))
- {
- msg_Warn (p_this, "joining the active thread (VLC might crash)");
- i_ret = pthread_detach (p_priv->thread_id);
- }
- else
- i_ret = vlc_join (p_priv->thread_id, NULL);
+ vlc_join (p_priv->thread_id, NULL);
#elif defined( UNDER_CE ) || defined( WIN32 )
HANDLE hThread;
DUPLICATE_SAME_ACCESS) )
{
p_priv->b_thread = false;
- i_ret = GetLastError();
- goto error;
+ return; /* We have a problem! */
}
vlc_join( p_priv->thread_id, NULL );
(double)((user_time%(60*1000000))/1000000.0) );
}
CloseHandle( hThread );
-error:
#else
- i_ret = vlc_join( p_priv->thread_id, NULL );
+ vlc_join( p_priv->thread_id, NULL );
#endif
- if( i_ret )
- {
- errno = i_ret;
- msg_Err( p_this, "thread_join(%lu) failed at %s:%d (%m)",
- (unsigned long)p_priv->thread_id, psz_file, i_line );
- }
- else
- msg_Dbg( p_this, "thread %lu joined (%s:%d)",
- (unsigned long)p_priv->thread_id, psz_file, i_line );
-
p_priv->b_thread = false;
}
va_start (ap, cmd);
vlc_cancel_t *nfo = vlc_threadvar_get (&cancel_key);
-#ifndef WIN32
if (nfo == NULL)
{
+#ifdef WIN32
+ /* Main thread - cannot be cancelled anyway */
+ return;
+#else
nfo = malloc (sizeof (*nfo));
if (nfo == NULL)
- abort ();
+ return; /* Uho! Expect problems! */
*nfo = VLC_CANCEL_INIT;
vlc_threadvar_set (&cancel_key, nfo);
- }
#endif
+ }
switch (cmd)
{