| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
#ifdef HAVE_CONFIG_H |
|---|
| 29 |
# include "config.h" |
|---|
| 30 |
#endif |
|---|
| 31 |
|
|---|
| 32 |
#include <vlc_common.h> |
|---|
| 33 |
|
|---|
| 34 |
#include "libvlc.h" |
|---|
| 35 |
#include <stdarg.h> |
|---|
| 36 |
#include <assert.h> |
|---|
| 37 |
#ifdef HAVE_UNISTD_H |
|---|
| 38 |
# include <unistd.h> |
|---|
| 39 |
#endif |
|---|
| 40 |
#include <signal.h> |
|---|
| 41 |
|
|---|
| 42 |
#define VLC_THREADS_UNINITIALIZED 0 |
|---|
| 43 |
#define VLC_THREADS_PENDING 1 |
|---|
| 44 |
#define VLC_THREADS_ERROR 2 |
|---|
| 45 |
#define VLC_THREADS_READY 3 |
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
static volatile unsigned i_initializations = 0; |
|---|
| 51 |
|
|---|
| 52 |
#if defined( LIBVLC_USE_PTHREAD ) |
|---|
| 53 |
# include <sched.h> |
|---|
| 54 |
|
|---|
| 55 |
static pthread_mutex_t once_mutex = PTHREAD_MUTEX_INITIALIZER; |
|---|
| 56 |
#else |
|---|
| 57 |
static vlc_threadvar_t cancel_key; |
|---|
| 58 |
#endif |
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
static libvlc_global_data_t *p_root; |
|---|
| 65 |
|
|---|
| 66 |
libvlc_global_data_t *vlc_global( void ) |
|---|
| 67 |
{ |
|---|
| 68 |
assert( i_initializations > 0 ); |
|---|
| 69 |
return p_root; |
|---|
| 70 |
} |
|---|
| 71 |
|
|---|
| 72 |
#ifdef HAVE_EXECINFO_H |
|---|
| 73 |
# include <execinfo.h> |
|---|
| 74 |
#endif |
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
void vlc_trace (const char *fn, const char *file, unsigned line) |
|---|
| 80 |
{ |
|---|
| 81 |
fprintf (stderr, "at %s:%u in %s\n", file, line, fn); |
|---|
| 82 |
fflush (stderr); |
|---|
| 83 |
#ifdef HAVE_BACKTRACE |
|---|
| 84 |
void *stack[20]; |
|---|
| 85 |
int len = backtrace (stack, sizeof (stack) / sizeof (stack[0])); |
|---|
| 86 |
backtrace_symbols_fd (stack, len, 2); |
|---|
| 87 |
#endif |
|---|
| 88 |
#ifndef WIN32 |
|---|
| 89 |
fsync (2); |
|---|
| 90 |
#endif |
|---|
| 91 |
} |
|---|
| 92 |
|
|---|
| 93 |
static inline unsigned long vlc_threadid (void) |
|---|
| 94 |
{ |
|---|
| 95 |
#if defined(LIBVLC_USE_PTHREAD) |
|---|
| 96 |
union { pthread_t th; unsigned long int i; } v = { }; |
|---|
| 97 |
v.th = pthread_self (); |
|---|
| 98 |
return v.i; |
|---|
| 99 |
|
|---|
| 100 |
#elif defined (WIN32) |
|---|
| 101 |
return GetCurrentThreadId (); |
|---|
| 102 |
|
|---|
| 103 |
#else |
|---|
| 104 |
return 0; |
|---|
| 105 |
|
|---|
| 106 |
#endif |
|---|
| 107 |
} |
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 |
static void |
|---|
| 115 |
vlc_thread_fatal (const char *action, int error, |
|---|
| 116 |
const char *function, const char *file, unsigned line) |
|---|
| 117 |
{ |
|---|
| 118 |
fprintf (stderr, "LibVLC fatal error %s (%d) in thread %lu ", |
|---|
| 119 |
action, error, vlc_threadid ()); |
|---|
| 120 |
vlc_trace (function, file, line); |
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
#ifdef __GLIBC__ |
|---|
| 125 |
|
|---|
| 126 |
errno = error; |
|---|
| 127 |
fprintf (stderr, " Error message: %m\n"); |
|---|
| 128 |
#elif !defined (WIN32) |
|---|
| 129 |
char buf[1000]; |
|---|
| 130 |
const char *msg; |
|---|
| 131 |
|
|---|
| 132 |
switch (strerror_r (error, buf, sizeof (buf))) |
|---|
| 133 |
{ |
|---|
| 134 |
case 0: |
|---|
| 135 |
msg = buf; |
|---|
| 136 |
break; |
|---|
| 137 |
case ERANGE: |
|---|
| 138 |
msg = "unknwon (too big to display)"; |
|---|
| 139 |
break; |
|---|
| 140 |
default: |
|---|
| 141 |
msg = "unknown (invalid error number)"; |
|---|
| 142 |
break; |
|---|
| 143 |
} |
|---|
| 144 |
fprintf (stderr, " Error message: %s\n", msg); |
|---|
| 145 |
#endif |
|---|
| 146 |
fflush (stderr); |
|---|
| 147 |
|
|---|
| 148 |
abort (); |
|---|
| 149 |
} |
|---|
| 150 |
|
|---|
| 151 |
#ifndef NDEBUG |
|---|
| 152 |
# define VLC_THREAD_ASSERT( action ) \ |
|---|
| 153 |
if (val) vlc_thread_fatal (action, val, __func__, __FILE__, __LINE__) |
|---|
| 154 |
#else |
|---|
| 155 |
# define VLC_THREAD_ASSERT( action ) ((void)val) |
|---|
| 156 |
#endif |
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 |
#ifndef LIBVLC_USE_PTHREAD_CANCEL |
|---|
| 162 |
typedef struct vlc_cancel_t |
|---|
| 163 |
{ |
|---|
| 164 |
vlc_cleanup_t *cleaners; |
|---|
| 165 |
bool killable; |
|---|
| 166 |
bool killed; |
|---|
| 167 |
} vlc_cancel_t; |
|---|
| 168 |
|
|---|
| 169 |
# define VLC_CANCEL_INIT { NULL, true, false } |
|---|
| 170 |
#endif |
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 |
|
|---|
| 175 |
|
|---|
| 176 |
|
|---|
| 177 |
|
|---|
| 178 |
|
|---|
| 179 |
int vlc_threads_init( void ) |
|---|
| 180 |
{ |
|---|
| 181 |
int i_ret = VLC_SUCCESS; |
|---|
| 182 |
|
|---|
| 183 |
|
|---|
| 184 |
|
|---|
| 185 |
#if defined( LIBVLC_USE_PTHREAD ) |
|---|
| 186 |
pthread_mutex_lock( &once_mutex ); |
|---|
| 187 |
#endif |
|---|
| 188 |
|
|---|
| 189 |
if( i_initializations == 0 ) |
|---|
| 190 |
{ |
|---|
| 191 |
p_root = vlc_custom_create( (vlc_object_t *)NULL, sizeof( *p_root ), |
|---|
| 192 |
VLC_OBJECT_GENERIC, "root" ); |
|---|
| 193 |
if( p_root == NULL ) |
|---|
| 194 |
{ |
|---|
| 195 |
i_ret = VLC_ENOMEM; |
|---|
| 196 |
goto out; |
|---|
| 197 |
} |
|---|
| 198 |
|
|---|
| 199 |
|
|---|
| 200 |
#ifndef LIBVLC_USE_PTHREAD_CANCEL |
|---|
| 201 |
vlc_threadvar_create( &cancel_key, free ); |
|---|
| 202 |
#endif |
|---|
| 203 |
} |
|---|
| 204 |
i_initializations++; |
|---|
| 205 |
|
|---|
| 206 |
out: |
|---|
| 207 |
|
|---|
| 208 |
|
|---|
| 209 |
#if defined( LIBVLC_USE_PTHREAD ) |
|---|
| 210 |
pthread_mutex_unlock( &once_mutex ); |
|---|
| 211 |
#endif |
|---|
| 212 |
|
|---|
| 213 |
return i_ret; |
|---|
| 214 |
} |
|---|
| 215 |
|
|---|
| 216 |
|
|---|
| 217 |
|
|---|
| 218 |
|
|---|
| 219 |
|
|---|
| 220 |
|
|---|
| 221 |
void vlc_threads_end( void ) |
|---|
| 222 |
{ |
|---|
| 223 |
#if defined( LIBVLC_USE_PTHREAD ) |
|---|
| 224 |
pthread_mutex_lock( &once_mutex ); |
|---|
| 225 |
#endif |
|---|
| 226 |
|
|---|
| 227 |
assert( i_initializations > 0 ); |
|---|
| 228 |
|
|---|
| 229 |
if( i_initializations == 1 ) |
|---|
| 230 |
{ |
|---|
| 231 |
vlc_object_release( p_root ); |
|---|
| 232 |
#ifndef LIBVLC_USE_PTHREAD |
|---|
| 233 |
vlc_threadvar_delete( &cancel_key ); |
|---|
| 234 |
#endif |
|---|
| 235 |
} |
|---|
| 236 |
i_initializations--; |
|---|
| 237 |
|
|---|
| 238 |
#if defined( LIBVLC_USE_PTHREAD ) |
|---|
| 239 |
pthread_mutex_unlock( &once_mutex ); |
|---|
| 240 |
#endif |
|---|
| 241 |
} |
|---|
| 242 |
|
|---|
| 243 |
#if defined (__GLIBC__) && (__GLIBC_MINOR__ < 6) |
|---|
| 244 |
|
|---|
| 245 |
int pthread_mutexattr_setkind_np( pthread_mutexattr_t *attr, int kind ); |
|---|
| 246 |
#endif |
|---|
| 247 |
|
|---|
| 248 |
|
|---|
| 249 |
|
|---|
| 250 |
|
|---|
| 251 |
int vlc_mutex_init( vlc_mutex_t *p_mutex ) |
|---|
| 252 |
{ |
|---|
| 253 |
#if defined( LIBVLC_USE_PTHREAD ) |
|---|
| 254 |
pthread_mutexattr_t attr; |
|---|
| 255 |
int i_result; |
|---|
| 256 |
|
|---|
| 257 |
pthread_mutexattr_init( &attr ); |
|---|
| 258 |
|
|---|
| 259 |
# ifndef NDEBUG |
|---|
| 260 |
|
|---|
| 261 |
# if defined (__GLIBC__) && (__GLIBC_MINOR__ < 6) |
|---|
| 262 |
pthread_mutexattr_setkind_np( &attr, PTHREAD_MUTEX_ERRORCHECK_NP ); |
|---|
| 263 |
# else |
|---|
| 264 |
pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_ERRORCHECK ); |
|---|
| 265 |
# endif |
|---|
| 266 |
# endif |
|---|
| 267 |
i_result = pthread_mutex_init( p_mutex, &attr ); |
|---|
| 268 |
pthread_mutexattr_destroy( &attr ); |
|---|
| 269 |
return i_result; |
|---|
| 270 |
|
|---|
| 271 |
#elif defined( WIN32 ) |
|---|
| 272 |
InitializeCriticalSection (&p_mutex->mutex); |
|---|
| 273 |
p_mutex->recursive = false; |
|---|
| 274 |
return 0; |
|---|
| 275 |
|
|---|
| 276 |
#endif |
|---|
| 277 |
} |
|---|
| 278 |
|
|---|
| 279 |
|
|---|
| 280 |
|
|---|
| 281 |
|
|---|
| 282 |
int vlc_mutex_init_recursive( vlc_mutex_t *p_mutex ) |
|---|
| 283 |
{ |
|---|
| 284 |
#if defined( LIBVLC_USE_PTHREAD ) |
|---|
| 285 |
pthread_mutexattr_t attr; |
|---|
| 286 |
int i_result; |
|---|
| 287 |
|
|---|
| 288 |
pthread_mutexattr_init( &attr ); |
|---|
| 289 |
# if defined (__GLIBC__) && (__GLIBC_MINOR__ < 6) |
|---|
| 290 |
pthread_mutexattr_setkind_np( &attr, PTHREAD_MUTEX_RECURSIVE_NP ); |
|---|
| 291 |
# else |
|---|
| 292 |
pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_RECURSIVE ); |
|---|
| 293 |
# endif |
|---|
| 294 |
i_result = pthread_mutex_init( p_mutex, &attr ); |
|---|
| 295 |
pthread_mutexattr_destroy( &attr ); |
|---|
| 296 |
return( i_result ); |
|---|
| 297 |
|
|---|
| 298 |
#elif defined( WIN32 ) |
|---|
| 299 |
InitializeCriticalSection( &p_mutex->mutex ); |
|---|
| 300 |
p_mutex->owner = 0; |
|---|
| 301 |
p_mutex->recursion = 0; |
|---|
| 302 |
p_mutex->recursive = true; |
|---|
| 303 |
return 0; |
|---|
| 304 |
|
|---|
| 305 |
#endif |
|---|
| 306 |
} |
|---|
| 307 |
|
|---|
| 308 |
|
|---|
| 309 |
|
|---|
| 310 |
|
|---|
| 311 |
|
|---|
| 312 |
|
|---|
| 313 |
|
|---|
| 314 |
|
|---|
| 315 |
void vlc_mutex_destroy (vlc_mutex_t *p_mutex) |
|---|
| 316 |
{ |
|---|
| 317 |
#if defined( LIBVLC_USE_PTHREAD ) |
|---|
| 318 |
int val = pthread_mutex_destroy( p_mutex ); |
|---|
| 319 |
VLC_THREAD_ASSERT ("destroying mutex"); |
|---|
| 320 |
|
|---|
| 321 |
#elif defined( WIN32 ) |
|---|
| 322 |
DeleteCriticalSection (&p_mutex->mutex); |
|---|
| 323 |
|
|---|
| 324 |
#endif |
|---|
| 325 |
} |
|---|
| 326 |
|
|---|
| 327 |
|
|---|
| 328 |
|
|---|
| 329 |
|
|---|
| 330 |
|
|---|
| 331 |
|
|---|
| 332 |
|
|---|
| 333 |
|
|---|
| 334 |
|
|---|
| 335 |
void vlc_mutex_lock (vlc_mutex_t *p_mutex) |
|---|
| 336 |
{ |
|---|
| 337 |
#if defined(LIBVLC_USE_PTHREAD) |
|---|
| 338 |
int val = pthread_mutex_lock( p_mutex ); |
|---|
| 339 |
VLC_THREAD_ASSERT ("locking mutex"); |
|---|
| 340 |
|
|---|
| 341 |
#elif defined( WIN32 ) |
|---|
| 342 |
if (p_mutex->recursive) |
|---|
| 343 |
{ |
|---|
| 344 |
DWORD self = GetCurrentThreadId (); |
|---|
| 345 |
|
|---|
| 346 |
if ((DWORD)InterlockedCompareExchange (&p_mutex->owner, self, |
|---|
| 347 |
self) == self) |
|---|
| 348 |
{ |
|---|
| 349 |
p_mutex->recursion++; |
|---|
| 350 |
return; |
|---|
| 351 |
} |
|---|
| 352 |
|
|---|
| 353 |
|
|---|
| 354 |
EnterCriticalSection (&p_mutex->mutex); |
|---|
| 355 |
self = InterlockedExchange (&p_mutex->owner, self); |
|---|
| 356 |
assert (self == 0); |
|---|
| 357 |
return; |
|---|
| 358 |
} |
|---|
| 359 |
|
|---|
| 360 |
EnterCriticalSection (&p_mutex->mutex); |
|---|
| 361 |
|
|---|
| 362 |
#endif |
|---|
| 363 |
} |
|---|
| 364 |
|
|---|
| 365 |
|
|---|
| 366 |
|
|---|
| 367 |
|
|---|
| 368 |
|
|---|
| 369 |
|
|---|
| 370 |
void vlc_mutex_unlock (vlc_mutex_t *p_mutex) |
|---|
| 371 |
{ |
|---|
| 372 |
#if defined(LIBVLC_USE_PTHREAD) |
|---|
| 373 |
int val = pthread_mutex_unlock( p_mutex ); |
|---|
| 374 |
VLC_THREAD_ASSERT ("unlocking mutex"); |
|---|
| 375 |
|
|---|
| 376 |
#elif defined( WIN32 ) |
|---|
| 377 |
if (p_mutex->recursive) |
|---|
| 378 |
{ |
|---|
| 379 |
if (p_mutex->recursion != 0) |
|---|
| 380 |
{ |
|---|
| 381 |
p_mutex->recursion--; |
|---|
| 382 |
return; |
|---|
| 383 |
} |
|---|
| 384 |
|
|---|
| 385 |
|
|---|
| 386 |
InterlockedExchange (&p_mutex->owner, 0); |
|---|
| 387 |
|
|---|
| 388 |
} |
|---|
| 389 |
LeaveCriticalSection (&p_mutex->mutex); |
|---|
| 390 |
|
|---|
| 391 |
#endif |
|---|
| 392 |
} |
|---|
| 393 |
|
|---|
| 394 |
|
|---|
| 395 |
|
|---|
| 396 |
|
|---|
| 397 |
int vlc_cond_init( vlc_cond_t *p_condvar ) |
|---|
| 398 |
{ |
|---|
| 399 |
#if defined( LIBVLC_USE_PTHREAD ) |
|---|
| 400 |
pthread_condattr_t attr; |
|---|
| 401 |
int ret; |
|---|
| 402 |
|
|---|
| 403 |
ret = pthread_condattr_init (&attr); |
|---|
| 404 |
if (ret) |
|---|
| 405 |
return ret; |
|---|
| 406 |
|
|---|
| 407 |
# if !defined (_POSIX_CLOCK_SELECTION) |
|---|
| 408 |
|
|---|
| 409 |
# define _POSIX_CLOCK_SELECTION (-1) |
|---|
| 410 |
# endif |
|---|
| 411 |
# if (_POSIX_CLOCK_SELECTION >= 0) |
|---|
| 412 |
|
|---|
| 413 |
pthread_condattr_setclock (&attr, CLOCK_MONOTONIC); |
|---|
| 414 |
# endif |
|---|
| 415 |
|
|---|
| 416 |
ret = pthread_cond_init (p_condvar, &attr); |
|---|
| 417 |
pthread_condattr_destroy (&attr); |
|---|
| 418 |
return ret; |
|---|
| 419 |
|
|---|
| 420 |
#elif defined( WIN32 ) |
|---|
| 421 |
|
|---|
| 422 |
*p_condvar = CreateEvent( NULL, TRUE, FALSE, NULL ); |
|---|
| 423 |
return *p_condvar ? 0 : ENOMEM; |
|---|
| 424 |
|
|---|
| 425 |
#endif |
|---|
| 426 |
} |
|---|
| 427 |
|
|---|
| 428 |
|
|---|
| 429 |
|
|---|
| 430 |
|
|---|
| 431 |
|
|---|
| 432 |
|
|---|
| 433 |
void vlc_cond_destroy (vlc_cond_t *p_condvar) |
|---|
| 434 |
{ |
|---|
| 435 |
#if defined( LIBVLC_USE_PTHREAD ) |
|---|
| 436 |
int val = pthread_cond_destroy( p_condvar ); |
|---|
| 437 |
VLC_THREAD_ASSERT ("destroying condition"); |
|---|
| 438 |
|
|---|
| 439 |
#elif defined( WIN32 ) |
|---|
| 440 |
CloseHandle( *p_condvar ); |
|---|
| 441 |
|
|---|
| 442 |
#endif |
|---|
| 443 |
} |
|---|
| 444 |
|
|---|
| 445 |
|
|---|
| 446 |
|
|---|
| 447 |
|
|---|
| 448 |
|
|---|
| 449 |
void vlc_cond_signal (vlc_cond_t *p_condvar) |
|---|
| 450 |
{ |
|---|
| 451 |
#if defined(LIBVLC_USE_PTHREAD) |
|---|
| 452 |
int val = pthread_cond_signal( p_condvar ); |
|---|
| 453 |
VLC_THREAD_ASSERT ("signaling condition variable"); |
|---|
| 454 |
|
|---|
| 455 |
#elif defined( WIN32 ) |
|---|
| 456 |
|
|---|
| 457 |
|
|---|
| 458 |
|
|---|
| 459 |
|
|---|
| 460 |
|
|---|
| 461 |
SetEvent (*p_condvar); |
|---|
| 462 |
|
|---|
| 463 |
#endif |
|---|
| 464 |
} |
|---|
| 465 |
|
|---|
| 466 |
|
|---|
| 467 |
|
|---|
| 468 |
|
|---|
| 469 |
|
|---|
| 470 |
void vlc_cond_broadcast (vlc_cond_t *p_condvar) |
|---|
| 471 |
{ |
|---|
| 472 |
#if defined (LIBVLC_USE_PTHREAD) |
|---|
| 473 |
pthread_cond_broadcast (p_condvar); |
|---|
| 474 |
|
|---|
| 475 |
#elif defined (WIN32) |
|---|
| 476 |
SetEvent (*p_condvar); |
|---|
| 477 |
|
|---|
| 478 |
#endif |
|---|
| 479 |
} |
|---|
| 480 |
|
|---|
| 481 |
#ifdef UNDER_CE |
|---|
| 482 |
# warning FIXME |
|---|
| 483 |
# define WaitForMultipleObjectsEx(a,b,c) WaitForMultipleObjects(a,b) |
|---|
| 484 |
#endif |
|---|
| 485 |
|
|---|
| 486 |
|
|---|
| 487 |
|
|---|
| 488 |
|
|---|
| 489 |
|
|---|
| 490 |
|
|---|
| 491 |
|
|---|
| 492 |
|
|---|
| 493 |
|
|---|
| 494 |
|
|---|
| 495 |
|
|---|
| 496 |
|
|---|
| 497 |
|
|---|
| 498 |
|
|---|
| 499 |
|
|---|
| 500 |
|
|---|
| 501 |
|
|---|
| 502 |
|
|---|
| 503 |
|
|---|
| 504 |
|
|---|
| 505 |
|
|---|
| 506 |
|
|---|
| 507 |
|
|---|
| 508 |
|
|---|
| 509 |
|
|---|
| 510 |
|
|---|
| 511 |
|
|---|
| 512 |
|
|---|
| 513 |
|
|---|
| 514 |
|
|---|
| 515 |
|
|---|
| 516 |
|
|---|
| 517 |
|
|---|
| 518 |
|
|---|
| 519 |
|
|---|
| 520 |
void vlc_cond_wait (vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex) |
|---|
| 521 |
{ |
|---|
| 522 |
#if defined(LIBVLC_USE_PTHREAD) |
|---|
| 523 |
int val = pthread_cond_wait( p_condvar, p_mutex ); |
|---|
| 524 |
VLC_THREAD_ASSERT ("waiting on condition"); |
|---|
| 525 |
|
|---|
| 526 |
#elif defined( WIN32 ) |
|---|
| 527 |
DWORD result; |
|---|
| 528 |
|
|---|
| 529 |
assert (!p_mutex->recursive); |
|---|
| 530 |
do |
|---|
| 531 |
{ |
|---|
| 532 |
vlc_testcancel (); |
|---|
| 533 |
LeaveCriticalSection (&p_mutex->mutex); |
|---|
| 534 |
result = WaitForSingleObjectEx (*p_condvar, INFINITE, TRUE); |
|---|
| 535 |
EnterCriticalSection (&p_mutex->mutex); |
|---|
| 536 |
} |
|---|
| 537 |
while (result == WAIT_IO_COMPLETION); |
|---|
| 538 |
|
|---|
| 539 |
ResetEvent (*p_condvar); |
|---|
| 540 |
|
|---|
| 541 |
#endif |
|---|
| 542 |
} |
|---|
| 543 |
|
|---|
| 544 |
|
|---|
| 545 |
|
|---|
| 546 |
|
|---|
| 547 |
|
|---|
| 548 |
|
|---|
| 549 |
|
|---|
| 550 |
|
|---|
| 551 |
|
|---|
| 552 |
|
|---|
| 553 |
|
|---|
| 554 |
|
|---|
| 555 |
int vlc_cond_timedwait (vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex, |
|---|
| 556 |
mtime_t deadline) |
|---|
| 557 |
{ |
|---|
| 558 |
#if defined(LIBVLC_USE_PTHREAD) |
|---|
| 559 |
lldiv_t d = lldiv( deadline, CLOCK_FREQ ); |
|---|
| 560 |
struct timespec ts = { d.quot, d.rem * (1000000000 / CLOCK_FREQ) }; |
|---|
| 561 |
|
|---|
| 562 |
int val = pthread_cond_timedwait (p_condvar, p_mutex, &ts); |
|---|
| 563 |
if (val != ETIMEDOUT) |
|---|
| 564 |
VLC_THREAD_ASSERT ("timed-waiting on condition"); |
|---|
| 565 |
return val; |
|---|
| 566 |
|
|---|
| 567 |
#elif defined( WIN32 ) |
|---|
| 568 |
DWORD result; |
|---|
| 569 |
|
|---|
| 570 |
assert (!p_mutex->recursive); |
|---|
| 571 |
do |
|---|
| 572 |
{ |
|---|
| 573 |
vlc_testcancel (); |
|---|
| 574 |
|
|---|
| 575 |
mtime_t total = (deadline - mdate ())/1000; |
|---|
| 576 |
if( total < 0 ) |
|---|
| 577 |
total = 0; |
|---|
| 578 |
|
|---|
| 579 |
DWORD delay = (total > 0x7fffffff) ? 0x7fffffff : total; |
|---|
| 580 |
LeaveCriticalSection (&p_mutex->mutex); |
|---|
| 581 |
result = WaitForSingleObjectEx (*p_condvar, delay, TRUE); |
|---|
| 582 |
EnterCriticalSection (&p_mutex->mutex); |
|---|
| 583 |
} |
|---|
| 584 |
while (result == WAIT_IO_COMPLETION); |
|---|
| 585 |
|
|---|
| 586 |
ResetEvent (*p_condvar); |
|---|
| 587 |
|
|---|
| 588 |
return (result == WAIT_OBJECT_0) ? 0 : ETIMEDOUT; |
|---|
| 589 |
|
|---|
| 590 |
#endif |
|---|
| 591 |
} |
|---|
| 592 |
|
|---|
| 593 |
|
|---|
| 594 |
|
|---|
| 595 |
|
|---|
| 596 |
int vlc_threadvar_create( vlc_threadvar_t *p_tls, void (*destr) (void *) ) |
|---|
| 597 |
{ |
|---|
| 598 |
int i_ret; |
|---|
| 599 |
|
|---|
| 600 |
#if defined( LIBVLC_USE_PTHREAD ) |
|---|
| 601 |
i_ret = pthread_key_create( p_tls, destr ); |
|---|
| 602 |
#elif defined( UNDER_CE ) |
|---|
| 603 |
i_ret = ENOSYS; |
|---|
| 604 |
#elif defined( WIN32 ) |
|---|
| 605 |
|
|---|
| 606 |
*p_tls = TlsAlloc(); |
|---|
| 607 |
i_ret = (*p_tls == TLS_OUT_OF_INDEXES) ? EAGAIN : 0; |
|---|
| 608 |
#else |
|---|
| 609 |
# error Unimplemented! |
|---|
| 610 |
#endif |
|---|
| 611 |
return i_ret; |
|---|
| 612 |
} |
|---|
| 613 |
|
|---|
| 614 |
void vlc_threadvar_delete (vlc_threadvar_t *p_tls) |
|---|
| 615 |
{ |
|---|
| 616 |
#if defined( LIBVLC_USE_PTHREAD ) |
|---|
| 617 |
pthread_key_delete (*p_tls); |
|---|
| 618 |
#elif defined( UNDER_CE ) |
|---|
| 619 |
#elif defined( WIN32 ) |
|---|
| 620 |
TlsFree (*p_tls); |
|---|
| 621 |
#else |
|---|
| 622 |
# error Unimplemented! |
|---|
| 623 |
#endif |
|---|
| 624 |
} |
|---|
| 625 |
|
|---|
| 626 |
#if defined (LIBVLC_USE_PTHREAD) |
|---|
| 627 |
#elif defined (WIN32) |
|---|
| 628 |
static unsigned __stdcall vlc_entry (void *data) |
|---|
| 629 |
{ |
|---|
| 630 |
vlc_cancel_t cancel_data = VLC_CANCEL_INIT; |
|---|
| 631 |
vlc_thread_t self = data; |
|---|
| 632 |
|
|---|
| 633 |
vlc_threadvar_set (&cancel_key, &cancel_data); |
|---|
| 634 |
self->data = self->entry (self->data); |
|---|
| 635 |
return 0; |
|---|
| 636 |
} |
|---|
| 637 |
#endif |
|---|
| 638 |
|
|---|
| 639 |
|
|---|
| 640 |
|
|---|
| 641 |
|
|---|
| 642 |
|
|---|
| 643 |
|
|---|
| 644 |
|
|---|
| 645 |
|
|---|
| 646 |
|
|---|
| 647 |
|
|---|
| 648 |
int vlc_clone (vlc_thread_t *p_handle, void * (*entry) (void *), void *data, |
|---|
| 649 |
int priority) |
|---|
| 650 |
{ |
|---|
| 651 |
int ret; |
|---|
| 652 |
|
|---|
| 653 |
#if defined( LIBVLC_USE_PTHREAD ) |
|---|
| 654 |
pthread_attr_t attr; |
|---|
| 655 |
pthread_attr_init (&attr); |
|---|
| 656 |
|
|---|
| 657 |
|
|---|
| 658 |
|
|---|
| 659 |
|
|---|
| 660 |
|
|---|
| 661 |
|
|---|
| 662 |
|
|---|
| 663 |
|
|---|
| 664 |
|
|---|
| 665 |
|
|---|
| 666 |
|
|---|
| 667 |
sigset_t oldset; |
|---|
| 668 |
{ |
|---|
| 669 |
sigset_t set; |
|---|
| 670 |
sigemptyset (&set); |
|---|
| 671 |
sigdelset (&set, SIGHUP); |
|---|
| 672 |
sigaddset (&set, SIGINT); |
|---|
| 673 |
sigaddset (&set, SIGQUIT); |
|---|
| 674 |
sigaddset (&set, SIGTERM); |
|---|
| 675 |
|
|---|
| 676 |
sigaddset (&set, SIGPIPE); |
|---|
| 677 |
pthread_sigmask (SIG_BLOCK, &set, &oldset); |
|---|
| 678 |
} |
|---|
| 679 |
{ |
|---|
| 680 |
struct sched_param sp = { .sched_priority = priority, }; |
|---|
| 681 |
int policy; |
|---|
| 682 |
|
|---|
| 683 |
if (sp.sched_priority <= 0) |
|---|
| 684 |
sp.sched_priority += sched_get_priority_max (policy = SCHED_OTHER); |
|---|
| 685 |
else |
|---|
| 686 |
sp.sched_priority += sched_get_priority_min (policy = SCHED_RR); |
|---|
| 687 |
|
|---|
| 688 |
pthread_attr_setschedpolicy (&attr, policy); |
|---|
| 689 |
pthread_attr_setschedparam (&attr, &sp); |
|---|
| 690 |
} |
|---|
| 691 |
|
|---|
| 692 |
|
|---|
| 693 |
|
|---|
| 694 |
|
|---|
| 695 |
|
|---|
| 696 |
|
|---|
| 697 |
|
|---|
| 698 |
|
|---|
| 699 |
|
|---|
| 700 |
|
|---|
| 701 |
|
|---|
| 702 |
|
|---|
| 703 |
|
|---|
| 704 |
#define VLC_STACKSIZE (128 * sizeof (void *) * 1024) |
|---|
| 705 |
|
|---|
| 706 |
#ifdef VLC_STACKSIZE |
|---|
| 707 |
ret = pthread_attr_setstacksize (&attr, VLC_STACKSIZE); |
|---|
| 708 |
assert (ret == 0); |
|---|
| 709 |
#endif |
|---|
| 710 |
|
|---|
| 711 |
ret = pthread_create (p_handle, &attr, entry, data); |
|---|
| 712 |
pthread_sigmask (SIG_SETMASK, &oldset, NULL); |
|---|
| 713 |
pthread_attr_destroy (&attr); |
|---|
| 714 |
|
|---|
| 715 |
#elif defined( WIN32 ) || defined( UNDER_CE ) |
|---|
| 716 |
|
|---|
| 717 |
|
|---|
| 718 |
|
|---|
| 719 |
|
|---|
| 720 |
HANDLE hThread; |
|---|
| 721 |
vlc_thread_t th = malloc (sizeof (*th)); |
|---|
| 722 |
|
|---|
| 723 |
if (th == NULL) |
|---|
| 724 |
return ENOMEM; |
|---|
| 725 |
|
|---|
| 726 |
th->data = data; |
|---|
| 727 |
th->entry = entry; |
|---|
| 728 |
#if defined( UNDER_CE ) |
|---|
| 729 |
hThread = CreateThread (NULL, 0, vlc_entry, th, CREATE_SUSPENDED, NULL); |
|---|
| 730 |
#else |
|---|
| 731 |
hThread = (HANDLE)(uintptr_t) |
|---|
| 732 |
_beginthreadex (NULL, 0, vlc_entry, th, CREATE_SUSPENDED, NULL); |
|---|
| 733 |
#endif |
|---|
| 734 |
|
|---|
| 735 |
if (hThread) |
|---|
| 736 |
{ |
|---|
| 737 |
|
|---|
| 738 |
|
|---|
| 739 |
if (!DuplicateHandle (GetCurrentProcess (), hThread, |
|---|
| 740 |
GetCurrentProcess (), &th->handle, 0, FALSE, |
|---|
| 741 |
DUPLICATE_SAME_ACCESS)) |
|---|
| 742 |
{ |
|---|
| 743 |
CloseHandle (hThread); |
|---|
| 744 |
free (th); |
|---|
| 745 |
return ENOMEM; |
|---|
| 746 |
} |
|---|
| 747 |
|
|---|
| 748 |
ResumeThread (hThread); |
|---|
| 749 |
if (priority) |
|---|
| 750 |
SetThreadPriority (hThread, priority); |
|---|
| 751 |
|
|---|
| 752 |
ret = 0; |
|---|
| 753 |
*p_handle = th; |
|---|
| 754 |
} |
|---|
| 755 |
else |
|---|
| 756 |
{ |
|---|
| 757 |
ret = errno; |
|---|
| 758 |
free (th); |
|---|
| 759 |
} |
|---|
| 760 |
|
|---|
| 761 |
#endif |
|---|
| 762 |
return ret; |
|---|
| 763 |
} |
|---|
| 764 |
|
|---|
| 765 |
#if defined (WIN32) |
|---|
| 766 |
|
|---|
| 767 |
static void CALLBACK vlc_cancel_self (ULONG_PTR dummy) |
|---|
| 768 |
{ |
|---|
| 769 |
(void)dummy; |
|---|
| 770 |
vlc_control_cancel (VLC_DO_CANCEL); |
|---|
| 771 |
} |
|---|
| 772 |
#endif |
|---|
| 773 |
|
|---|
| 774 |
|
|---|
| 775 |
|
|---|
| 776 |
|
|---|
| 777 |
|
|---|
| 778 |
|
|---|
| 779 |
|
|---|
| 780 |
|
|---|
| 781 |
void vlc_cancel (vlc_thread_t thread_id) |
|---|
| 782 |
{ |
|---|
| 783 |
#if defined (LIBVLC_USE_PTHREAD_CANCEL) |
|---|
| 784 |
pthread_cancel (thread_id); |
|---|
| 785 |
#elif defined (WIN32) |
|---|
| 786 |
QueueUserAPC (vlc_cancel_self, thread_id->handle, 0); |
|---|
| 787 |
#else |
|---|
| 788 |
# warning vlc_cancel is not implemented! |
|---|
| 789 |
#endif |
|---|
| 790 |
} |
|---|
| 791 |
|
|---|
| 792 |
|
|---|
| 793 |
|
|---|
| 794 |
|
|---|
| 795 |
|
|---|
| 796 |
|
|---|
| 797 |
|
|---|
| 798 |
|
|---|
| 799 |
|
|---|
| 800 |
|
|---|
| 801 |
void vlc_join (vlc_thread_t handle, void **result) |
|---|
| 802 |
{ |
|---|
| 803 |
#if defined( LIBVLC_USE_PTHREAD ) |
|---|
| 804 |
int val = pthread_join (handle, result); |
|---|
| 805 |
if (val) |
|---|
| 806 |
vlc_thread_fatal ("joining thread", val, __func__, __FILE__, __LINE__); |
|---|
| 807 |
|
|---|
| 808 |
#elif defined( UNDER_CE ) || defined( WIN32 ) |
|---|
| 809 |
do |
|---|
| 810 |
vlc_testcancel (); |
|---|
| 811 |
while (WaitForSingleObjectEx (handle->handle, INFINITE, TRUE) |
|---|
| 812 |
== WAIT_IO_COMPLETION); |
|---|
| 813 |
|
|---|
| 814 |
CloseHandle (handle->handle); |
|---|
| 815 |
if (result) |
|---|
| 816 |
*result = handle->data; |
|---|
| 817 |
free (handle); |
|---|
| 818 |
|
|---|
| 819 |
#endif |
|---|
| 820 |
} |
|---|
| 821 |
|
|---|
| 822 |
|
|---|
| 823 |
struct vlc_thread_boot |
|---|
| 824 |
{ |
|---|
| 825 |
void * (*entry) (vlc_object_t *); |
|---|
| 826 |
vlc_object_t *object; |
|---|
| 827 |
}; |
|---|
| 828 |
|
|---|
| 829 |
static void *thread_entry (void *data) |
|---|
| 830 |
{ |
|---|
| 831 |
vlc_object_t *obj = ((struct vlc_thread_boot *)data)->object; |
|---|
| 832 |
void *(*func) (vlc_object_t *) = ((struct vlc_thread_boot *)data)->entry; |
|---|
| 833 |
|
|---|
| 834 |
free (data); |
|---|
| 835 |
msg_Dbg (obj, "thread started"); |
|---|
| 836 |
func (obj); |
|---|
| 837 |
msg_Dbg (obj, "thread ended"); |
|---|
| 838 |
|
|---|
| 839 |
return NULL; |
|---|
| 840 |
} |
|---|
| 841 |
|
|---|
| 842 |
|
|---|
| 843 |
|
|---|
| 844 |
|
|---|
| 845 |
|
|---|
| 846 |
|
|---|
| 847 |
|
|---|
| 848 |
int __vlc_thread_create( vlc_object_t *p_this, const char * psz_file, int i_line, |
|---|
| 849 |
const char *psz_name, void * ( *func ) ( vlc_object_t * ), |
|---|
| 850 |
int i_priority, bool b_wait ) |
|---|
| 851 |
{ |
|---|
| 852 |
int i_ret; |
|---|
| 853 |
vlc_object_internals_t *p_priv = vlc_internals( p_this ); |
|---|
| 854 |
|
|---|
| 855 |
struct vlc_thread_boot *boot = malloc (sizeof (*boot)); |
|---|
| 856 |
if (boot == NULL) |
|---|
| 857 |
return errno; |
|---|
| 858 |
boot->entry = func; |
|---|
| 859 |
boot->object = p_this; |
|---|
| 860 |
|
|---|
| 861 |
vlc_object_lock( p_this ); |
|---|
| 862 |
|
|---|
| 863 |
|
|---|
| 864 |
assert( !p_priv->b_thread ); |
|---|
| 865 |
|
|---|
| 866 |
#if defined( LIBVLC_USE_PTHREAD ) |
|---|
| 867 |
#ifndef __APPLE__ |
|---|
| 868 |
if( config_GetInt( p_this, "rt-priority" ) > 0 ) |
|---|
| 869 |
#endif |
|---|
| 870 |
{ |
|---|
| 871 |
|
|---|
| 872 |
if( config_GetType( p_this, "rt-offset" ) ) |
|---|
| 873 |
i_priority += config_GetInt( p_this, "rt-offset" ); |
|---|
| 874 |
} |
|---|
| 875 |
#endif |
|---|
| 876 |
|
|---|
| 877 |
i_ret = vlc_clone( &p_priv->thread_id, thread_entry, boot, i_priority ); |
|---|
| 878 |
if( i_ret == 0 ) |
|---|
| 879 |
{ |
|---|
| 880 |
if( b_wait ) |
|---|
| 881 |
{ |
|---|
| 882 |
msg_Dbg( p_this, "waiting for thread initialization" ); |
|---|
| 883 |
vlc_object_wait( p_this ); |
|---|
| 884 |
} |
|---|
| 885 |
|
|---|
| 886 |
p_priv->b_thread = true; |
|---|
| 887 |
msg_Dbg( p_this, "thread %lu (%s) created at priority %d (%s:%d)", |
|---|
| 888 |
(unsigned long)p_priv->thread_id, psz_name, i_priority, |
|---|
| 889 |
psz_file, i_line ); |
|---|
| 890 |
} |
|---|
| 891 |
else |
|---|
| 892 |
{ |
|---|
| 893 |
errno = i_ret; |
|---|
| 894 |
msg_Err( p_this, "%s thread could not be created at %s:%d (%m)", |
|---|
| 895 |
psz_name, psz_file, i_line ); |
|---|
| 896 |
} |
|---|
| 897 |
|
|---|
| 898 |
vlc_object_unlock( p_this ); |
|---|
| 899 |
return i_ret; |
|---|
| 900 |
} |
|---|
| 901 |
|
|---|
| 902 |
|
|---|
| 903 |
|
|---|
| 904 |
|
|---|
| 905 |
|
|---|
| 906 |
int __vlc_thread_set_priority( vlc_object_t *p_this, const char * psz_file, |
|---|
| 907 |
int i_line, int i_priority ) |
|---|
| 908 |
{ |
|---|
| 909 |
vlc_object_internals_t *p_priv = vlc_internals( p_this ); |
|---|
| 910 |
|
|---|
| 911 |
if( !p_priv->b_thread ) |
|---|
| 912 |
{ |
|---|
| 913 |
msg_Err( p_this, "couldn't set priority of non-existent thread" ); |
|---|
| 914 |
return ESRCH; |
|---|
| 915 |
} |
|---|
| 916 |
|
|---|
| 917 |
#if defined( LIBVLC_USE_PTHREAD ) |
|---|
| 918 |
# ifndef __APPLE__ |
|---|
| 919 |
if( config_GetInt( p_this, "rt-priority" ) > 0 ) |
|---|
| 920 |
# endif |
|---|
| 921 |
{ |
|---|
| 922 |
int i_error, i_policy; |
|---|
| 923 |
struct sched_param param; |
|---|
| 924 |
|
|---|
| 925 |
memset( ¶m, 0, sizeof(struct sched_param) ); |
|---|
| 926 |
if( config_GetType( p_this, "rt-offset" ) ) |
|---|
| 927 |
i_priority += config_GetInt( p_this, "rt-offset" ); |
|---|
| 928 |
if( i_priority <= 0 ) |
|---|
| 929 |
{ |
|---|
| 930 |
param.sched_priority = (-1) * i_priority; |
|---|
| 931 |
i_policy = SCHED_OTHER; |
|---|
| 932 |
} |
|---|
| 933 |
else |
|---|
| 934 |
{ |
|---|
| 935 |
param.sched_priority = i_priority; |
|---|
| 936 |
i_policy = SCHED_RR; |
|---|