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.

151 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. /* MB_NEXT() relies on the fact that MB_BUFFERS is a power of two */
  34. #define MB_BUFFERS 128
  35. #define MB_NEXT(index) ((index+1) & (MB_BUFFERS-1))
  36. #define MB_BUFFERSIZE 256 /* message length limit */
  37. static char mb_buffers[MB_BUFFERS][MB_BUFFERSIZE];
  38. static volatile unsigned int mb_initialized = 0;
  39. static volatile unsigned int mb_inbuffer = 0;
  40. static volatile unsigned int mb_outbuffer = 0;
  41. static volatile _Atomic_word mb_overruns = 0;
  42. static pthread_t mb_writer_thread;
  43. static pthread_mutex_t mb_write_lock;
  44. static pthread_cond_t mb_ready_cond;
  45. static void
  46. mb_flush()
  47. {
  48. /* called WITHOUT the mb_write_lock */
  49. while (mb_outbuffer != mb_inbuffer) {
  50. fputs(mb_buffers[mb_outbuffer], stderr);
  51. mb_outbuffer = MB_NEXT(mb_outbuffer);
  52. }
  53. }
  54. static void *
  55. mb_thread_func(void *arg)
  56. {
  57. /* The mutex is only to eliminate collisions between multiple
  58. * writer threads and protect the condition variable. */
  59. pthread_mutex_lock(&mb_write_lock);
  60. while (mb_initialized) {
  61. pthread_cond_wait(&mb_ready_cond, &mb_write_lock);
  62. /* releasing the mutex reduces contention */
  63. pthread_mutex_unlock(&mb_write_lock);
  64. mb_flush();
  65. pthread_mutex_lock(&mb_write_lock);
  66. }
  67. pthread_mutex_unlock(&mb_write_lock);
  68. return NULL;
  69. }
  70. void
  71. jack_messagebuffer_init ()
  72. {
  73. if (mb_initialized)
  74. return;
  75. pthread_mutex_init(&mb_write_lock, NULL);
  76. pthread_cond_init(&mb_ready_cond, NULL);
  77. mb_overruns = 0;
  78. mb_initialized = 1;
  79. if (pthread_create(&mb_writer_thread, NULL, &mb_thread_func, NULL) != 0)
  80. mb_initialized = 0;
  81. }
  82. void
  83. jack_messagebuffer_exit ()
  84. {
  85. if (!mb_initialized)
  86. return;
  87. pthread_mutex_lock(&mb_write_lock);
  88. mb_initialized = 0;
  89. pthread_cond_signal(&mb_ready_cond);
  90. pthread_mutex_unlock(&mb_write_lock);
  91. pthread_join(mb_writer_thread, NULL);
  92. mb_flush();
  93. if (mb_overruns)
  94. fprintf(stderr, "WARNING: %d message buffer overruns!\n",
  95. mb_overruns);
  96. else
  97. fprintf(stderr, "no message buffer overruns\n");
  98. pthread_mutex_destroy(&mb_write_lock);
  99. pthread_cond_destroy(&mb_ready_cond);
  100. }
  101. void
  102. jack_messagebuffer_add (const char *fmt, ...)
  103. {
  104. char msg[MB_BUFFERSIZE];
  105. va_list ap;
  106. /* format the message first, to reduce lock contention */
  107. va_start(ap, fmt);
  108. vsnprintf(msg, MB_BUFFERSIZE, fmt, ap);
  109. va_end(ap);
  110. if (!mb_initialized) {
  111. /* Unable to print message with realtime safety.
  112. * Complain and print it anyway. */
  113. fprintf(stderr, "ERROR: messagebuffer not initialized: %s",
  114. msg);
  115. return;
  116. }
  117. if (pthread_mutex_trylock(&mb_write_lock) == 0) {
  118. strncpy(mb_buffers[mb_inbuffer], msg, MB_BUFFERSIZE);
  119. mb_inbuffer = MB_NEXT(mb_inbuffer);
  120. pthread_cond_signal(&mb_ready_cond);
  121. pthread_mutex_unlock(&mb_write_lock);
  122. } else { /* lock collision */
  123. atomic_add(&mb_overruns, 1);
  124. }
  125. }