Codebase list squeezelite / aaa4861
rename portaudio probe thread to monitor thread and fix mutex usage Adrian Smith 10 years ago
1 changed file(s) with 40 addition(s) and 29 deletion(s). Raw diff Collapse all Expand all
462462 }
463463 }
464464
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() {
469469 bool output_off;
470
470471 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;
471482 output_off = (output.state == OUTPUT_OFF);
472 UNLOCK;
473
474 while (probe_thread_running) {
475 LOCK;
483
484 while (monitor_thread_running) {
476485 if (output_off) {
477486 if (output.state != OUTPUT_OFF) {
478487 LOG_INFO("output on");
479 probe_thread_running = false;
480 pa.stream = NULL;
481 _pa_open();
488 break;
482489 }
483490 } else {
484491 // this is a hack to partially support hot plugging of devices
489496 pa.stream = NULL;
490497 if (pa_device_id(output.device) != -1) {
491498 LOG_INFO("device reopen");
492 probe_thread_running = false;
493 _pa_open();
494 }
495 }
499 break;
500 }
501 }
502
496503 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;
502516
503517 return 0;
504518 }
572586 }
573587 }
574588
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
577591 #if LINUX || OSX
578 pthread_create(&probe_thread, NULL, pa_probe, NULL);
592 pthread_create(&monitor_thread, NULL, pa_monitor, NULL);
579593 #endif
580594 #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
586597 }
587598 }
588599
14941505 #endif
14951506
14961507 #if PORTAUDIO
1497 probe_thread_running = false;
1508 monitor_thread_running = false;
14981509
14991510 if (pa.stream) {
15001511 if ((err = Pa_AbortStream(pa.stream)) != paNoError) {