jack1 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
3.9KB

  1. /*
  2. * messagebuffer.c -- realtime-safe message handling for jackd.
  3. *
  4. * This interface is included in libjack so backend drivers can use
  5. * it, *not* for external client processes. It implements the
  6. * VERBOSE() and MESSAGE() macros in a realtime-safe manner.
  7. */
  8. /*
  9. * Copyright (C) 2004 Rui Nuno Capela, Steve Harris
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU Lesser General Public License as published by
  13. * the Free Software Foundation; either version 2.1 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  24. *
  25. */
  26. #include <unistd.h>
  27. #include <string.h>
  28. #include <pthread.h>
  29. #include <stdarg.h>
  30. #include <stdio.h>
  31. #include <jack/messagebuffer.h>
  32. #include <jack/atomicity.h>
  33. #include <jack/internal.h>
  34. /* MB_NEXT() relies on the fact that MB_BUFFERS is a power of two */
  35. #define MB_BUFFERS 128
  36. #define MB_NEXT(index) ((index+1) & (MB_BUFFERS-1))
  37. #define MB_BUFFERSIZE 256 /* message length limit */
  38. static char mb_buffers[MB_BUFFERS][MB_BUFFERSIZE];
  39. static volatile unsigned int mb_initialized = 0;
  40. static volatile unsigned int mb_inbuffer = 0;
  41. static volatile unsigned int mb_outbuffer = 0;
  42. static volatile _Atomic_word mb_overruns = 0;
  43. static pthread_t mb_writer_thread;
  44. static pthread_mutex_t mb_write_lock;
  45. static pthread_cond_t mb_ready_cond;
  46. static void
  47. mb_flush()
  48. {
  49. /* called WITHOUT the mb_write_lock */
  50. while (mb_outbuffer != mb_inbuffer) {
  51. jack_info(mb_buffers[mb_outbuffer]);
  52. mb_outbuffer = MB_NEXT(mb_outbuffer);
  53. }
  54. }
  55. static void *
  56. mb_thread_func(void *arg)
  57. {
  58. /* The mutex is only to eliminate collisions between multiple
  59. * writer threads and protect the condition variable. */
  60. pthread_mutex_lock(&mb_write_lock);
  61. while (mb_initialized) {
  62. pthread_cond_wait(&mb_ready_cond, &mb_write_lock);
  63. /* releasing the mutex reduces contention */
  64. pthread_mutex_unlock(&mb_write_lock);
  65. mb_flush();
  66. pthread_mutex_lock(&mb_write_lock);
  67. }
  68. pthread_mutex_unlock(&mb_write_lock);
  69. return NULL;
  70. }
  71. void
  72. jack_messagebuffer_init ()
  73. {
  74. if (mb_initialized)
  75. return;
  76. pthread_mutex_init(&mb_write_lock, NULL);
  77. pthread_cond_init(&mb_ready_cond, NULL);
  78. mb_overruns = 0;
  79. mb_initialized = 1;
  80. if (jack_thread_creator (&mb_writer_thread, NULL, &mb_thread_func, NULL) != 0)
  81. mb_initialized = 0;
  82. }
  83. void
  84. jack_messagebuffer_exit ()
  85. {
  86. if (!mb_initialized)
  87. return;
  88. pthread_mutex_lock(&mb_write_lock);
  89. mb_initialized = 0;
  90. pthread_cond_signal(&mb_ready_cond);
  91. pthread_mutex_unlock(&mb_write_lock);
  92. pthread_join(mb_writer_thread, NULL);
  93. mb_flush();
  94. if (mb_overruns)
  95. jack_error("WARNING: %d message buffer overruns!",
  96. mb_overruns);
  97. pthread_mutex_destroy(&mb_write_lock);
  98. pthread_cond_destroy(&mb_ready_cond);
  99. }
  100. void
  101. jack_messagebuffer_add (const char *fmt, ...)
  102. {
  103. char msg[MB_BUFFERSIZE];
  104. va_list ap;
  105. /* format the message first, to reduce lock contention */
  106. va_start(ap, fmt);
  107. vsnprintf(msg, MB_BUFFERSIZE, fmt, ap);
  108. va_end(ap);
  109. if (!mb_initialized) {
  110. /* Unable to print message with realtime safety.
  111. * Complain and print it anyway. */
  112. fprintf(stderr, "ERROR: messagebuffer not initialized: %s",
  113. msg);
  114. return;
  115. }
  116. if (pthread_mutex_trylock(&mb_write_lock) == 0) {
  117. strncpy(mb_buffers[mb_inbuffer], msg, MB_BUFFERSIZE);
  118. mb_inbuffer = MB_NEXT(mb_inbuffer);
  119. pthread_cond_signal(&mb_ready_cond);
  120. pthread_mutex_unlock(&mb_write_lock);
  121. } else { /* lock collision */
  122. atomic_add(&mb_overruns, 1);
  123. }
  124. }