| 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 ) |
|---|
| 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 | *****************************************************************************/ |
|---|
| | 246 | int vlc_mutex_init_recursive( vlc_mutex_t *p_mutex ) |
|---|
| | 247 | { |
|---|
| | 248 | #if defined( LIBVLC_USE_PTHREAD ) |
|---|
| 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 ) |
|---|
| | 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 | |
|---|
| 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( ¶m, 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, ¶m )) ) |
|---|
| | 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 ) |
|---|
| 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 | *****************************************************************************/ |
|---|
| | 518 | int __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 |
|---|
| 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 ) |
|---|
| 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( ¶m, 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, ¶m )) ) |
|---|
| 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 | | } |
|---|
| 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 ) |
|---|