rename portaudio probe thread to monitor thread and fix mutex usage
Adrian Smith
10 years ago
462 | 462 | } |
463 | 463 | } |
464 | 464 | |
465 | static thread_type probe_thread; | |
466 | bool probe_thread_running = false; | |
467 | ||
468 | static void *pa_probe() { | |
465 | static thread_type monitor_thread; | |
466 | bool monitor_thread_running = false; | |
467 | ||
468 | static void *pa_monitor() { | |
469 | 469 | bool output_off; |
470 | ||
470 | 471 | LOCK; |
472 | ||
473 | if (monitor_thread_running) { | |
474 | LOG_DEBUG("monitor thread already running"); | |
475 | UNLOCK; | |
476 | return 0; | |
477 | } | |
478 | ||
479 | LOG_DEBUG("start monitor thread"); | |
480 | ||
481 | monitor_thread_running = true; | |
471 | 482 | output_off = (output.state == OUTPUT_OFF); |
472 | UNLOCK; | |
473 | ||
474 | while (probe_thread_running) { | |
475 | LOCK; | |
483 | ||
484 | while (monitor_thread_running) { | |
476 | 485 | if (output_off) { |
477 | 486 | if (output.state != OUTPUT_OFF) { |
478 | 487 | LOG_INFO("output on"); |
479 | probe_thread_running = false; | |
480 | pa.stream = NULL; | |
481 | _pa_open(); | |
488 | break; | |
482 | 489 | } |
483 | 490 | } else { |
484 | 491 | // this is a hack to partially support hot plugging of devices |
489 | 496 | pa.stream = NULL; |
490 | 497 | if (pa_device_id(output.device) != -1) { |
491 | 498 | LOG_INFO("device reopen"); |
492 | probe_thread_running = false; | |
493 | _pa_open(); | |
494 | } | |
495 | } | |
499 | break; | |
500 | } | |
501 | } | |
502 | ||
496 | 503 | UNLOCK; |
497 | ||
498 | if (probe_thread_running) { | |
499 | sleep(output_off ? 1 : 5); | |
500 | } | |
501 | } | |
504 | sleep(output_off ? 1 : 5); | |
505 | LOCK; | |
506 | } | |
507 | ||
508 | LOG_DEBUG("end monitor thread"); | |
509 | ||
510 | monitor_thread_running = false; | |
511 | pa.stream = NULL; | |
512 | ||
513 | _pa_open(); | |
514 | ||
515 | UNLOCK; | |
502 | 516 | |
503 | 517 | return 0; |
504 | 518 | } |
572 | 586 | } |
573 | 587 | } |
574 | 588 | |
575 | if (err && !probe_thread_running) { | |
576 | // create a thread to probe for the device | |
589 | if (err && !monitor_thread_running) { | |
590 | // create a thread to check for output state change or device return | |
577 | 591 | #if LINUX || OSX |
578 | pthread_create(&probe_thread, NULL, pa_probe, NULL); | |
592 | pthread_create(&monitor_thread, NULL, pa_monitor, NULL); | |
579 | 593 | #endif |
580 | 594 | #if WIN |
581 | probe_thread = CreateThread(NULL, OUTPUT_THREAD_STACK_SIZE, (LPTHREAD_START_ROUTINE)&pa_probe, NULL, 0, NULL); | |
582 | #endif | |
583 | probe_thread_running = true; | |
584 | UNLOCK; | |
585 | return; | |
595 | monitor_thread = CreateThread(NULL, OUTPUT_THREAD_STACK_SIZE, (LPTHREAD_START_ROUTINE)&pa_monitor, NULL, 0, NULL); | |
596 | #endif | |
586 | 597 | } |
587 | 598 | } |
588 | 599 | |
1494 | 1505 | #endif |
1495 | 1506 | |
1496 | 1507 | #if PORTAUDIO |
1497 | probe_thread_running = false; | |
1508 | monitor_thread_running = false; | |
1498 | 1509 | |
1499 | 1510 | if (pa.stream) { |
1500 | 1511 | if ((err = Pa_AbortStream(pa.stream)) != paNoError) { |