thread.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/kthread.h>
  3. #include <linux/wait.h>
  4. #include "spk_types.h"
  5. #include "speakup.h"
  6. #include "spk_priv.h"
  7. DECLARE_WAIT_QUEUE_HEAD(speakup_event);
  8. EXPORT_SYMBOL_GPL(speakup_event);
  9. int speakup_thread(void *data)
  10. {
  11. unsigned long flags;
  12. int should_break;
  13. struct bleep our_sound;
  14. our_sound.active = 0;
  15. our_sound.freq = 0;
  16. our_sound.jiffies = 0;
  17. mutex_lock(&spk_mutex);
  18. while (1) {
  19. DEFINE_WAIT(wait);
  20. while (1) {
  21. spin_lock_irqsave(&speakup_info.spinlock, flags);
  22. our_sound = spk_unprocessed_sound;
  23. spk_unprocessed_sound.active = 0;
  24. prepare_to_wait(&speakup_event, &wait,
  25. TASK_INTERRUPTIBLE);
  26. should_break = kthread_should_stop() ||
  27. our_sound.active ||
  28. (synth && synth->catch_up && synth->alive &&
  29. (speakup_info.flushing ||
  30. !synth_buffer_empty()));
  31. spin_unlock_irqrestore(&speakup_info.spinlock, flags);
  32. if (should_break)
  33. break;
  34. mutex_unlock(&spk_mutex);
  35. schedule();
  36. mutex_lock(&spk_mutex);
  37. }
  38. finish_wait(&speakup_event, &wait);
  39. if (kthread_should_stop())
  40. break;
  41. if (our_sound.active)
  42. kd_mksound(our_sound.freq, our_sound.jiffies);
  43. if (synth && synth->catch_up && synth->alive) {
  44. /*
  45. * It is up to the callee to take the lock, so that it
  46. * can sleep whenever it likes
  47. */
  48. synth->catch_up(synth);
  49. }
  50. speakup_start_ttys();
  51. }
  52. mutex_unlock(&spk_mutex);
  53. return 0;
  54. }