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.

186 lines
4.7KB

  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 "messagebuffer.h"
  32. #include "atomicity.h"
  33. #include "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 (*mb_thread_init_callback)(void*) = 0;
  47. static void* mb_thread_init_callback_arg = 0;
  48. static void
  49. mb_flush ()
  50. {
  51. /* called WITHOUT the mb_write_lock */
  52. while (mb_outbuffer != mb_inbuffer) {
  53. jack_info (mb_buffers[mb_outbuffer]);
  54. mb_outbuffer = MB_NEXT (mb_outbuffer);
  55. }
  56. }
  57. static void *
  58. mb_thread_func (void *arg)
  59. {
  60. /* The mutex is only to eliminate collisions between multiple
  61. * writer threads and protect the condition variable. */
  62. pthread_mutex_lock (&mb_write_lock);
  63. while (mb_initialized) {
  64. pthread_cond_wait (&mb_ready_cond, &mb_write_lock);
  65. if (mb_thread_init_callback) {
  66. /* the client asked for all threads to run a thread
  67. initialization callback, which includes us.
  68. */
  69. mb_thread_init_callback (mb_thread_init_callback_arg);
  70. mb_thread_init_callback = 0;
  71. /* note that we've done it */
  72. pthread_cond_signal (&mb_ready_cond);
  73. }
  74. /* releasing the mutex reduces contention */
  75. pthread_mutex_unlock (&mb_write_lock);
  76. mb_flush ();
  77. pthread_mutex_lock (&mb_write_lock);
  78. }
  79. pthread_mutex_unlock (&mb_write_lock);
  80. return NULL;
  81. }
  82. void
  83. jack_messagebuffer_init ()
  84. {
  85. if (mb_initialized) {
  86. return;
  87. }
  88. pthread_mutex_init (&mb_write_lock, NULL);
  89. pthread_cond_init (&mb_ready_cond, NULL);
  90. mb_overruns = 0;
  91. mb_initialized = 1;
  92. if (jack_thread_creator (&mb_writer_thread, NULL, &mb_thread_func, NULL) != 0) {
  93. mb_initialized = 0;
  94. }
  95. }
  96. void
  97. jack_messagebuffer_exit ()
  98. {
  99. if (!mb_initialized) {
  100. return;
  101. }
  102. pthread_mutex_lock (&mb_write_lock);
  103. mb_initialized = 0;
  104. pthread_cond_signal (&mb_ready_cond);
  105. pthread_mutex_unlock (&mb_write_lock);
  106. pthread_join (mb_writer_thread, NULL);
  107. mb_flush ();
  108. if (mb_overruns) {
  109. jack_error ("WARNING: %d message buffer overruns!",
  110. mb_overruns);
  111. }
  112. pthread_mutex_destroy (&mb_write_lock);
  113. pthread_cond_destroy (&mb_ready_cond);
  114. }
  115. void
  116. jack_messagebuffer_add (const char *fmt, ...)
  117. {
  118. char msg[MB_BUFFERSIZE];
  119. va_list ap;
  120. /* format the message first, to reduce lock contention */
  121. va_start (ap, fmt);
  122. vsnprintf (msg, MB_BUFFERSIZE, fmt, ap);
  123. va_end (ap);
  124. if (!mb_initialized) {
  125. /* Unable to print message with realtime safety.
  126. * Complain and print it anyway. */
  127. fprintf (stderr, "ERROR: messagebuffer not initialized: %s",
  128. msg);
  129. return;
  130. }
  131. if (pthread_mutex_trylock (&mb_write_lock) == 0) {
  132. strncpy (mb_buffers[mb_inbuffer], msg, MB_BUFFERSIZE);
  133. mb_inbuffer = MB_NEXT (mb_inbuffer);
  134. pthread_cond_signal (&mb_ready_cond);
  135. pthread_mutex_unlock (&mb_write_lock);
  136. } else { /* lock collision */
  137. exchange_and_add (&mb_overruns, 1);
  138. }
  139. }
  140. void
  141. jack_messagebuffer_thread_init (void (*cb)(void*), void* arg)
  142. {
  143. pthread_mutex_lock (&mb_write_lock);
  144. /* set up the callback */
  145. mb_thread_init_callback_arg = arg;
  146. mb_thread_init_callback = cb;
  147. /* wake msg buffer thread */
  148. pthread_cond_signal (&mb_ready_cond);
  149. /* wait for it to be done */
  150. pthread_cond_wait (&mb_ready_cond, &mb_write_lock);
  151. /* and we're done */
  152. pthread_mutex_unlock (&mb_write_lock);
  153. }