X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=src%2Fmisc%2Fthreads.c;h=33270df4a65a75be81a6d9ba63b24a761bea1df3;hb=070e3454880e849f3ebca21d5f803cdd13351049;hp=0ab4efaf655d6adf018e6d092d1768270ce86d81;hpb=5143683b7bf2be576bf0efd3bd5a80838e902556;p=vlc diff --git a/src/misc/threads.c b/src/misc/threads.c index 0ab4efaf65..33270df4a6 100644 --- a/src/misc/threads.c +++ b/src/misc/threads.c @@ -28,13 +28,14 @@ # include "config.h" #endif -#include +#include #include "libvlc.h" #include #ifdef HAVE_UNISTD_H # include #endif +#include #define VLC_THREADS_UNINITIALIZED 0 #define VLC_THREADS_PENDING 1 @@ -62,6 +63,15 @@ libvlc_global_data_t *vlc_global( void ) return p_root; } +/** + * Object running the current thread + */ +static vlc_threadvar_t thread_object_key; + +vlc_object_t *vlc_threadobj (void) +{ + return vlc_threadvar_get (&thread_object_key); +} vlc_threadvar_t msg_context_global_key; @@ -73,6 +83,10 @@ static inline unsigned long vlc_threadid (void) return v.i; } +#if defined(HAVE_EXECINFO_H) && defined(HAVE_BACKTRACE) +# include +#endif + /***************************************************************************** * vlc_thread_fatal: Report an error from the threading layer ***************************************************************************** @@ -90,7 +104,7 @@ void vlc_pthread_fatal (const char *action, int error, #ifdef __GLIBC__ /* Avoid the strerror_r() prototype brain damage in glibc */ errno = error; - fprintf (stderr, " Error message: %m\n"); + dprintf (2, " Error message: %m at:\n"); #else char buf[1000]; const char *msg; @@ -108,9 +122,15 @@ void vlc_pthread_fatal (const char *action, int error, break; } fprintf (stderr, " Error message: %s\n", msg); + fflush (stderr); +#endif + +#ifdef HAVE_BACKTRACE + void *stack[20]; + int len = backtrace (stack, sizeof (stack) / sizeof (stack[0])); + backtrace_symbols_fd (stack, len, 2); #endif - fflush (stderr); abort (); } #else @@ -150,6 +170,7 @@ int vlc_threads_init( void ) } /* We should be safe now. Do all the initialization stuff we want. */ + vlc_threadvar_create( &thread_object_key, NULL ); vlc_threadvar_create( &msg_context_global_key, msg_StackDestroy ); } i_initializations++; @@ -181,6 +202,7 @@ void vlc_threads_end( void ) { vlc_object_release( p_root ); vlc_threadvar_delete( &msg_context_global_key ); + vlc_threadvar_delete( &thread_object_key ); } i_initializations--; @@ -411,6 +433,33 @@ void vlc_threadvar_delete (vlc_threadvar_t *p_tls) #endif } +struct vlc_thread_boot +{ + void * (*entry) (void *); + vlc_object_t *object; +}; + +#if defined (LIBVLC_USE_PTHREAD) +# define THREAD_RTYPE void * +# define THREAD_RVAL NULL +#elif defined (WIN32) +# define THREAD_RTYPE __stdcall unsigned +# define THREAD_RVAL 0 +#endif + +static THREAD_RTYPE thread_entry (void *data) +{ + vlc_object_t *obj = ((struct vlc_thread_boot *)data)->object; + void *(*func) (void *) = ((struct vlc_thread_boot *)data)->entry; + + free (data); + vlc_threadvar_set (&thread_object_key, obj); + msg_Dbg (obj, "thread started"); + func (obj); + msg_Dbg (obj, "thread ended"); + return THREAD_RVAL; +} + /***************************************************************************** * vlc_thread_create: create a thread, inner version ***************************************************************************** @@ -422,13 +471,36 @@ int __vlc_thread_create( vlc_object_t *p_this, const char * psz_file, int i_line int i_priority, bool b_wait ) { int i_ret; - void *p_data = (void *)p_this; vlc_object_internals_t *p_priv = vlc_internals( p_this ); + struct vlc_thread_boot *boot = malloc (sizeof (*boot)); + if (boot == NULL) + return errno; + boot->entry = func; + boot->object = p_this; + vlc_mutex_lock( &p_this->object_lock ); #if defined( LIBVLC_USE_PTHREAD ) - i_ret = pthread_create( &p_priv->thread_id, NULL, func, p_data ); + sigset_t set, oldset; + + /* We really don't want signals to (literaly) interrupt our blocking I/O + * system calls. SIGPIPE is especially bad, as it can be caused by remote + * peers through connected sockets. Generally, we cannot know which signals + * are handled by the main program. Also, external LibVLC bindings tend not + * to setup a proper signal mask before invoking LibVLC. + * Hence, we hereby block all signals, except those for which blocking is + * undefined, as listed below. Note that SIGKILL and SIGSTOP need not be + * listed (see the documentation for pthread_sigmask) here. */ + sigfillset (&set); + sigdelset (&set, SIGFPE); + sigdelset (&set, SIGILL); + sigdelset (&set, SIGSEGV); + sigdelset (&set, SIGBUS); + pthread_sigmask (SIG_BLOCK, &set, &oldset); + + i_ret = pthread_create( &p_priv->thread_id, NULL, thread_entry, boot ); + pthread_sigmask (SIG_SETMASK, &oldset, NULL); #ifndef __APPLE__ if( config_GetInt( p_this, "rt-priority" ) > 0 ) @@ -471,13 +543,13 @@ int __vlc_thread_create( vlc_object_t *p_this, const char * psz_file, int i_line * memory leaks and the signal functions not working (see Microsoft * Knowledge Base, article 104641) */ #if defined( UNDER_CE ) - HANDLE hThread = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)func, - (LPVOID)p_data, CREATE_SUSPENDED, + HANDLE hThread = CreateThread( NULL, 0, thread_entry, + (LPVOID)boot, CREATE_SUSPENDED, NULL ); #else HANDLE hThread = (HANDLE)(uintptr_t) - _beginthreadex( NULL, 0, (LPTHREAD_START_ROUTINE)func, - (void *)p_data, CREATE_SUSPENDED, NULL ); + _beginthreadex( NULL, 0, thread_entry, boot, + CREATE_SUSPENDED, NULL ); #endif p_priv->thread_id = hThread; ResumeThread(hThread); @@ -495,7 +567,7 @@ int __vlc_thread_create( vlc_object_t *p_this, const char * psz_file, int i_line } #elif defined( HAVE_KERNEL_SCHEDULER_H ) - p_priv->thread_id = spawn_thread( (thread_func)func, psz_name, + p_priv->thread_id = spawn_thread( (thread_func)thread_entry, psz_name, i_priority, p_data ); i_ret = resume_thread( p_priv->thread_id );