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.

182 lines
4.6KB

  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. pthread_mutex_init(&mb_write_lock, NULL);
  88. pthread_cond_init(&mb_ready_cond, NULL);
  89. mb_overruns = 0;
  90. mb_initialized = 1;
  91. if (jack_thread_creator (&mb_writer_thread, NULL, &mb_thread_func, NULL) != 0)
  92. mb_initialized = 0;
  93. }
  94. void
  95. jack_messagebuffer_exit ()
  96. {
  97. if (!mb_initialized)
  98. return;
  99. pthread_mutex_lock(&mb_write_lock);
  100. mb_initialized = 0;
  101. pthread_cond_signal(&mb_ready_cond);
  102. pthread_mutex_unlock(&mb_write_lock);
  103. pthread_join(mb_writer_thread, NULL);
  104. mb_flush();
  105. if (mb_overruns)
  106. jack_error("WARNING: %d message buffer overruns!",
  107. mb_overruns);
  108. pthread_mutex_destroy(&mb_write_lock);
  109. pthread_cond_destroy(&mb_ready_cond);
  110. }
  111. void
  112. jack_messagebuffer_add (const char *fmt, ...)
  113. {
  114. char msg[MB_BUFFERSIZE];
  115. va_list ap;
  116. /* format the message first, to reduce lock contention */
  117. va_start(ap, fmt);
  118. vsnprintf(msg, MB_BUFFERSIZE, fmt, ap);
  119. va_end(ap);
  120. if (!mb_initialized) {
  121. /* Unable to print message with realtime safety.
  122. * Complain and print it anyway. */
  123. fprintf(stderr, "ERROR: messagebuffer not initialized: %s",
  124. msg);
  125. return;
  126. }
  127. if (pthread_mutex_trylock(&mb_write_lock) == 0) {
  128. strncpy(mb_buffers[mb_inbuffer], msg, MB_BUFFERSIZE);
  129. mb_inbuffer = MB_NEXT(mb_inbuffer);
  130. pthread_cond_signal(&mb_ready_cond);
  131. pthread_mutex_unlock(&mb_write_lock);
  132. } else { /* lock collision */
  133. atomic_add(&mb_overruns, 1);
  134. }
  135. }
  136. void
  137. jack_messagebuffer_thread_init (void (*cb)(void*), void* arg)
  138. {
  139. pthread_mutex_lock (&mb_write_lock);
  140. /* set up the callback */
  141. mb_thread_init_callback_arg = arg;
  142. mb_thread_init_callback = cb;
  143. /* wake msg buffer thread */
  144. pthread_cond_signal(&mb_ready_cond);
  145. /* wait for it to be done */
  146. pthread_cond_wait(&mb_ready_cond, &mb_write_lock);
  147. /* and we're done */
  148. pthread_mutex_unlock (&mb_write_lock);
  149. }