Changeset 3afd1ff1ab186f1f550469e9d4f656eff566ecbf

Show
Ignore:
Timestamp:
05/08/08 20:55:05 (2 months ago)
Author:
Rémi Denis-Courmont <rem@videolan.org>
git-committer:
Rémi Denis-Courmont <rem@videolan.org> 1210272905 +0300
git-parent:

[55aeca35b861a188c4447349fb9c189a1304d4f5]

git-author:
Rémi Denis-Courmont <rem@videolan.org> 1210272905 +0300
Message:

Move pthread to the front.

If anyone wants to try on Win32, that's going to help.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • src/misc/threads.c

    r55aeca3 r3afd1ff  
    4747static volatile unsigned i_initializations = 0; 
    4848 
    49 #if defined( UNDER_CE ) 
    50 #elif defined( WIN32 ) 
    51 #elif defined( HAVE_KERNEL_SCHEDULER_H ) 
    52 #elif defined( LIBVLC_USE_PTHREAD ) 
     49#if defined( LIBVLC_USE_PTHREAD ) 
    5350static pthread_mutex_t once_mutex = PTHREAD_MUTEX_INITIALIZER; 
    5451#endif 
     
    134131    /* If we have lazy mutex initialization, use it. Otherwise, we just 
    135132     * hope nothing wrong happens. */ 
    136 #if defined( UNDER_CE ) 
    137 #elif defined( WIN32 ) 
    138 #elif defined( HAVE_KERNEL_SCHEDULER_H ) 
    139 #elif defined( LIBVLC_USE_PTHREAD ) 
     133#if defined( LIBVLC_USE_PTHREAD ) 
    140134    pthread_mutex_lock( &once_mutex ); 
    141135#endif 
     
    159153    /* If we have lazy mutex initialization support, unlock the mutex. 
    160154     * Otherwize, we are screwed. */ 
    161 #if defined( UNDER_CE ) 
    162 #elif defined( WIN32 ) 
    163 #elif defined( HAVE_KERNEL_SCHEDULER_H ) 
    164 #elif defined( LIBVLC_USE_PTHREAD ) 
     155#if defined( LIBVLC_USE_PTHREAD ) 
    165156    pthread_mutex_unlock( &once_mutex ); 
    166157#endif 
     
    176167void vlc_threads_end( void ) 
    177168{ 
    178 #if defined( UNDER_CE ) 
    179 #elif defined( WIN32 ) 
    180 #elif defined( HAVE_KERNEL_SCHEDULER_H ) 
    181 #elif defined( LIBVLC_USE_PTHREAD ) 
     169#if defined( LIBVLC_USE_PTHREAD ) 
    182170    pthread_mutex_lock( &once_mutex ); 
    183171#endif 
     
    189177    i_initializations--; 
    190178 
    191 #if defined( UNDER_CE ) 
    192 #elif defined( WIN32 ) 
    193 #elif defined( HAVE_KERNEL_SCHEDULER_H ) 
    194 #elif defined( LIBVLC_USE_PTHREAD ) 
     179#if defined( LIBVLC_USE_PTHREAD ) 
    195180    pthread_mutex_unlock( &once_mutex ); 
    196181#endif 
     
    207192int vlc_mutex_init( vlc_mutex_t *p_mutex ) 
    208193{ 
    209 #if defined( UNDER_CE ) 
    210     InitializeCriticalSection( &p_mutex->csection ); 
    211     return 0; 
    212  
    213 #elif defined( WIN32 ) 
    214     *p_mutex = CreateMutex( 0, FALSE, 0 ); 
    215     return (*p_mutex != NULL) ? 0 : ENOMEM; 
    216  
    217 #elif defined( HAVE_KERNEL_SCHEDULER_H ) 
    218     /* check the arguments and whether it's already been initialized */ 
    219     if( p_mutex == NULL ) 
    220     { 
    221         return B_BAD_VALUE; 
    222     } 
    223  
    224     if( p_mutex->init == 9999 ) 
    225     { 
    226         return EALREADY; 
    227     } 
    228  
    229     p_mutex->lock = create_sem( 1, "BeMutex" ); 
    230     if( p_mutex->lock < B_NO_ERROR ) 
    231     { 
    232         return( -1 ); 
    233     } 
    234  
    235     p_mutex->init = 9999; 
    236     return B_OK; 
    237  
    238 #elif defined( LIBVLC_USE_PTHREAD ) 
     194#if defined( LIBVLC_USE_PTHREAD ) 
    239195    pthread_mutexattr_t attr; 
    240196    int                 i_result; 
     
    253209    pthread_mutexattr_destroy( &attr ); 
    254210    return i_result; 
    255 #endif 
    256 
    257  
    258 /***************************************************************************** 
    259  * vlc_mutex_init: initialize a recursive mutex (Do not use) 
    260  *****************************************************************************/ 
    261 int vlc_mutex_init_recursive( vlc_mutex_t *p_mutex ) 
    262 
    263 #if defined( WIN32 ) 
    264     /* Create mutex returns a recursive mutex */ 
     211#elif defined( UNDER_CE ) 
     212    InitializeCriticalSection( &p_mutex->csection ); 
     213    return 0; 
     214 
     215#elif defined( WIN32 ) 
    265216    *p_mutex = CreateMutex( 0, FALSE, 0 ); 
    266217    return (*p_mutex != NULL) ? 0 : ENOMEM; 
    267 #elif defined( LIBVLC_USE_PTHREAD ) 
     218 
     219#elif defined( HAVE_KERNEL_SCHEDULER_H ) 
     220    /* check the arguments and whether it's already been initialized */ 
     221    if( p_mutex == NULL ) 
     222    { 
     223        return B_BAD_VALUE; 
     224    } 
     225 
     226    if( p_mutex->init == 9999 ) 
     227    { 
     228        return EALREADY; 
     229    } 
     230 
     231    p_mutex->lock = create_sem( 1, "BeMutex" ); 
     232    if( p_mutex->lock < B_NO_ERROR ) 
     233    { 
     234        return( -1 ); 
     235    } 
     236 
     237    p_mutex->init = 9999; 
     238    return B_OK; 
     239 
     240#endif 
     241
     242 
     243/***************************************************************************** 
     244 * vlc_mutex_init: initialize a recursive mutex (Do not use) 
     245 *****************************************************************************/ 
     246int vlc_mutex_init_recursive( vlc_mutex_t *p_mutex ) 
     247
     248#if defined( LIBVLC_USE_PTHREAD ) 
    268249    pthread_mutexattr_t attr; 
    269250    int                 i_result; 
     
    274255    pthread_mutexattr_destroy( &attr ); 
    275256    return( i_result ); 
     257#elif defined( WIN32 ) 
     258    /* Create mutex returns a recursive mutex */ 
     259    *p_mutex = CreateMutex( 0, FALSE, 0 ); 
     260    return (*p_mutex != NULL) ? 0 : ENOMEM; 
    276261#else 
    277262# error Unimplemented! 
     
    285270void __vlc_mutex_destroy( const char * psz_file, int i_line, vlc_mutex_t *p_mutex ) 
    286271{ 
    287 #if defined( UNDER_CE ) 
     272#if defined( LIBVLC_USE_PTHREAD ) 
     273    int val = pthread_mutex_destroy( p_mutex ); 
     274    VLC_THREAD_ASSERT ("destroying mutex"); 
     275 
     276#elif defined( UNDER_CE ) 
    288277    VLC_UNUSED( psz_file); VLC_UNUSED( i_line ); 
    289278 
     
    301290    p_mutex->init = 0; 
    302291 
    303 #elif defined( LIBVLC_USE_PTHREAD ) 
    304     int val = pthread_mutex_destroy( p_mutex ); 
    305     VLC_THREAD_ASSERT ("destroying mutex"); 
    306  
    307292#endif 
    308293} 
     
    313298int __vlc_cond_init( vlc_cond_t *p_condvar ) 
    314299{ 
    315 #if defined( UNDER_CE ) || defined( WIN32 ) 
    316     /* Initialize counter */ 
    317     p_condvar->i_waiting_threads = 0; 
    318  
    319     /* Create an auto-reset event. */ 
    320     p_condvar->event = CreateEvent( NULL,   /* no security */ 
    321                                     FALSE,  /* auto-reset event */ 
    322                                     FALSE,  /* start non-signaled */ 
    323                                     NULL ); /* unnamed */ 
    324     return !p_condvar->event; 
    325  
    326 #elif defined( HAVE_KERNEL_SCHEDULER_H ) 
    327     if( !p_condvar ) 
    328     { 
    329         return B_BAD_VALUE; 
    330     } 
    331  
    332     if( p_condvar->init == 9999 ) 
    333     { 
    334         return EALREADY; 
    335     } 
    336  
    337     p_condvar->thread = -1; 
    338     p_condvar->init = 9999; 
    339     return 0; 
    340  
    341 #elif defined( LIBVLC_USE_PTHREAD ) 
     300#if defined( LIBVLC_USE_PTHREAD ) 
    342301    pthread_condattr_t attr; 
    343302    int ret; 
     
    360319    return ret; 
    361320 
     321#elif defined( UNDER_CE ) || defined( WIN32 ) 
     322    /* Initialize counter */ 
     323    p_condvar->i_waiting_threads = 0; 
     324 
     325    /* Create an auto-reset event. */ 
     326    p_condvar->event = CreateEvent( NULL,   /* no security */ 
     327                                    FALSE,  /* auto-reset event */ 
     328                                    FALSE,  /* start non-signaled */ 
     329                                    NULL ); /* unnamed */ 
     330    return !p_condvar->event; 
     331 
     332#elif defined( HAVE_KERNEL_SCHEDULER_H ) 
     333    if( !p_condvar ) 
     334    { 
     335        return B_BAD_VALUE; 
     336    } 
     337 
     338    if( p_condvar->init == 9999 ) 
     339    { 
     340        return EALREADY; 
     341    } 
     342 
     343    p_condvar->thread = -1; 
     344    p_condvar->init = 9999; 
     345    return 0; 
     346 
    362347#endif 
    363348} 
     
    368353void __vlc_cond_destroy( const char * psz_file, int i_line, vlc_cond_t *p_condvar ) 
    369354{ 
    370 #if defined( UNDER_CE ) || defined( WIN32 ) 
     355#if defined( LIBVLC_USE_PTHREAD ) 
     356    int val = pthread_cond_destroy( p_condvar ); 
     357    VLC_THREAD_ASSERT ("destroying condition"); 
     358 
     359#elif defined( UNDER_CE ) || defined( WIN32 ) 
    371360    VLC_UNUSED( psz_file); VLC_UNUSED( i_line ); 
    372361 
     
    376365    p_condvar->init = 0; 
    377366 
    378 #elif defined( LIBVLC_USE_PTHREAD ) 
    379     int val = pthread_cond_destroy( p_condvar ); 
    380     VLC_THREAD_ASSERT ("destroying condition"); 
    381  
    382367#endif 
    383368} 
     
    390375    int i_ret = -1; 
    391376 
    392 #if defined( HAVE_KERNEL_SCHEDULER_H
    393 # error Unimplemented! 
     377#if defined( LIBVLC_USE_PTHREAD
     378    i_ret =  pthread_key_create( p_tls, NULL ); 
    394379#elif defined( UNDER_CE ) 
    395380#elif defined( WIN32 ) 
    396381    *p_tls = TlsAlloc(); 
    397382    i_ret = (*p_tls == INVALID_HANDLE_VALUE) ? EAGAIN : 0; 
    398  
    399 #elif defined( LIBVLC_USE_PTHREAD ) 
    400     i_ret =  pthread_key_create( p_tls, NULL ); 
     383#else 
     384# error Unimplemented! 
    401385#endif 
    402386    return i_ret; 
     
    419403    vlc_mutex_lock( &p_this->object_lock ); 
    420404 
    421 #if defined( WIN32 ) || defined( UNDER_CE ) 
     405#if defined( LIBVLC_USE_PTHREAD ) 
     406    i_ret = pthread_create( &p_priv->thread_id, NULL, func, p_data ); 
     407 
     408#ifndef __APPLE__ 
     409    if( config_GetInt( p_this, "rt-priority" ) > 0 ) 
     410#endif 
     411    { 
     412        int i_error, i_policy; 
     413        struct sched_param param; 
     414 
     415        memset( &param, 0, sizeof(struct sched_param) ); 
     416        if( config_GetType( p_this, "rt-offset" ) ) 
     417            i_priority += config_GetInt( p_this, "rt-offset" ); 
     418        if( i_priority <= 0 ) 
     419        { 
     420            param.sched_priority = (-1) * i_priority; 
     421            i_policy = SCHED_OTHER; 
     422        } 
     423        else 
     424        { 
     425            param.sched_priority = i_priority; 
     426            i_policy = SCHED_RR; 
     427        } 
     428        if( (i_error = pthread_setschedparam( p_priv->thread_id, 
     429                                               i_policy, &param )) ) 
     430        { 
     431            errno = i_error; 
     432            msg_Warn( p_this, "couldn't set thread priority (%s:%d): %m", 
     433                      psz_file, i_line ); 
     434            i_priority = 0; 
     435        } 
     436    } 
     437#ifndef __APPLE__ 
     438    else 
     439        i_priority = 0; 
     440#endif 
     441 
     442#elif defined( WIN32 ) || defined( UNDER_CE ) 
    422443    { 
    423444        /* When using the MSVCRT C library you have to use the _beginthreadex 
     
    458479    i_ret = resume_thread( p_priv->thread_id ); 
    459480 
    460 #elif defined( LIBVLC_USE_PTHREAD ) 
    461     i_ret = pthread_create( &p_priv->thread_id, NULL, func, p_data ); 
    462  
    463 #ifndef __APPLE__ 
    464     if( config_GetInt( p_this, "rt-priority" ) ) 
    465 #endif 
     481#endif 
     482 
     483    if( i_ret == 0 ) 
     484    { 
     485        if( b_wait ) 
     486        { 
     487            msg_Dbg( p_this, "waiting for thread completion" ); 
     488            vlc_object_wait( p_this ); 
     489        } 
     490 
     491        p_priv->b_thread = true; 
     492 
     493#if defined( WIN32 ) || defined( UNDER_CE ) 
     494        msg_Dbg( p_this, "thread %u (%s) created at priority %d (%s:%d)", 
     495                 (unsigned int)p_priv->thread_id.id, psz_name, i_priority, 
     496                 psz_file, i_line ); 
     497#else 
     498        msg_Dbg( p_this, "thread %u (%s) created at priority %d (%s:%d)", 
     499                 (unsigned int)p_priv->thread_id, psz_name, i_priority, 
     500                 psz_file, i_line ); 
     501#endif 
     502    } 
     503    else 
     504    { 
     505        errno = i_ret; 
     506        msg_Err( p_this, "%s thread could not be created at %s:%d (%m)", 
     507                         psz_name, psz_file, i_line ); 
     508    } 
     509 
     510    vlc_mutex_unlock( &p_this->object_lock ); 
     511    return i_ret; 
     512
     513 
     514/***************************************************************************** 
     515 * vlc_thread_set_priority: set the priority of the current thread when we 
     516 * couldn't set it in vlc_thread_create (for instance for the main thread) 
     517 *****************************************************************************/ 
     518int __vlc_thread_set_priority( vlc_object_t *p_this, const char * psz_file, 
     519                               int i_line, int i_priority ) 
     520
     521    vlc_object_internals_t *p_priv = vlc_internals( p_this ); 
     522 
     523#if defined( LIBVLC_USE_PTHREAD ) 
     524# ifndef __APPLE__ 
     525    if( config_GetInt( p_this, "rt-priority" ) > 0 ) 
     526# endif 
    466527    { 
    467528        int i_error, i_policy; 
     
    470531        memset( &param, 0, sizeof(struct sched_param) ); 
    471532        if( config_GetType( p_this, "rt-offset" ) ) 
    472         { 
    473533            i_priority += config_GetInt( p_this, "rt-offset" ); 
    474         } 
    475534        if( i_priority <= 0 ) 
    476535        { 
     
    483542            i_policy = SCHED_RR; 
    484543        } 
     544        if( !p_priv->thread_id ) 
     545            p_priv->thread_id = pthread_self(); 
    485546        if( (i_error = pthread_setschedparam( p_priv->thread_id, 
    486547                                               i_policy, &param )) ) 
     
    492553        } 
    493554    } 
    494 #ifndef __APPLE__ 
    495     else 
    496     { 
    497         i_priority = 0; 
    498     } 
    499 #endif 
    500  
    501 #endif 
    502  
    503     if( i_ret == 0 ) 
    504     { 
    505         if( b_wait ) 
    506         { 
    507             msg_Dbg( p_this, "waiting for thread completion" ); 
    508             vlc_object_wait( p_this ); 
    509         } 
    510  
    511         p_priv->b_thread = true; 
    512  
    513 #if defined( WIN32 ) || defined( UNDER_CE ) 
    514         msg_Dbg( p_this, "thread %u (%s) created at priority %d (%s:%d)", 
    515                  (unsigned int)p_priv->thread_id.id, psz_name, 
    516          i_priority, psz_file, i_line ); 
    517 #else 
    518         msg_Dbg( p_this, "thread %u (%s) created at priority %d (%s:%d)", 
    519                  (unsigned int)p_priv->thread_id, psz_name, i_priority, 
    520                  psz_file, i_line ); 
    521 #endif 
    522  
    523  
    524         vlc_mutex_unlock( &p_this->object_lock ); 
    525     } 
    526     else 
    527     { 
    528         errno = i_ret; 
    529         msg_Err( p_this, "%s thread could not be created at %s:%d (%m)", 
    530                          psz_name, psz_file, i_line ); 
    531         vlc_mutex_unlock( &p_this->object_lock ); 
    532     } 
    533  
    534     return i_ret; 
    535 
    536  
    537 /***************************************************************************** 
    538  * vlc_thread_set_priority: set the priority of the current thread when we 
    539  * couldn't set it in vlc_thread_create (for instance for the main thread) 
    540  *****************************************************************************/ 
    541 int __vlc_thread_set_priority( vlc_object_t *p_this, const char * psz_file, 
    542                                int i_line, int i_priority ) 
    543 
    544     vlc_object_internals_t *p_priv = vlc_internals( p_this ); 
    545 #if defined( WIN32 ) || defined( UNDER_CE ) 
     555 
     556#elif defined( WIN32 ) || defined( UNDER_CE ) 
    546557    VLC_UNUSED( psz_file); VLC_UNUSED( i_line ); 
    547558 
     
    554565    } 
    555566 
    556 #elif defined( LIBVLC_USE_PTHREAD ) 
    557 # ifndef __APPLE__ 
    558     if( config_GetInt( p_this, "rt-priority" ) > 0 ) 
    559 # endif 
    560     { 
    561         int i_error, i_policy; 
    562         struct sched_param param; 
    563  
    564         memset( &param, 0, sizeof(struct sched_param) ); 
    565         if( config_GetType( p_this, "rt-offset" ) ) 
    566         { 
    567             i_priority += config_GetInt( p_this, "rt-offset" ); 
    568         } 
    569         if( i_priority <= 0 ) 
    570         { 
    571             param.sched_priority = (-1) * i_priority; 
    572             i_policy = SCHED_OTHER; 
    573         } 
    574         else 
    575         { 
    576             param.sched_priority = i_priority; 
    577             i_policy = SCHED_RR; 
    578         } 
    579         if( !p_priv->thread_id ) 
    580             p_priv->thread_id = pthread_self(); 
    581         if( (i_error = pthread_setschedparam( p_priv->thread_id, 
    582                                                i_policy, &param )) ) 
    583         { 
    584             errno = i_error; 
    585             msg_Warn( p_this, "couldn't set thread priority (%s:%d): %m", 
    586                       psz_file, i_line ); 
    587             i_priority = 0; 
    588         } 
    589     } 
    590567#endif 
    591568 
     
    607584{ 
    608585    vlc_object_internals_t *p_priv = vlc_internals( p_this ); 
    609  
    610 #if defined( UNDER_CE ) || defined( WIN32 ) 
     586    int i_ret = 0; 
     587 
     588#if defined( LIBVLC_USE_PTHREAD ) 
     589    /* Make sure we do return if we are calling vlc_thread_join() 
     590     * from the joined thread */ 
     591    if (pthread_equal (pthread_self (), p_priv->thread_id)) 
     592        i_ret = pthread_detach (p_priv->thread_id); 
     593    else 
     594        i_ret = pthread_join (p_priv->thread_id, NULL); 
     595 
     596#elif defined( UNDER_CE ) || defined( WIN32 ) 
    611597    HMODULE hmodule; 
    612598    BOOL (WINAPI *OurGetThreadTimes)( HANDLE, FILETIME*, FILETIME*, 
     
    677663    CloseHandle( hThread ); 
    678664 
    679 #else /* !defined(WIN32) */ 
    680  
    681     int i_ret = 0; 
    682  
    683 #if defined( HAVE_KERNEL_SCHEDULER_H ) 
     665#elif defined( HAVE_KERNEL_SCHEDULER_H ) 
    684666    int32_t exit_value; 
    685667    i_ret = (B_OK == wait_for_thread( p_priv->thread_id, &exit_value )); 
    686668 
    687 #elif defined( LIBVLC_USE_PTHREAD ) 
    688     /* Make sure we do return if we are calling vlc_thread_join() 
    689      * from the joined thread */ 
    690     if (pthread_equal (pthread_self (), p_priv->thread_id)) 
    691         i_ret = pthread_detach (p_priv->thread_id); 
    692     else 
    693         i_ret = pthread_join (p_priv->thread_id, NULL); 
    694669#endif 
    695670 
     
    701676    } 
    702677    else 
    703     { 
    704678        msg_Dbg( p_this, "thread %u joined (%s:%d)", 
    705679                         (unsigned int)p_priv->thread_id, psz_file, i_line ); 
    706     } 
    707  
    708 #endif 
    709680 
    710681    p_priv->b_thread = false; 
    711682} 
    712