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.

107 lines
2.5KB

  1. /*
  2. * Copyright (C) 2004 Rui Nuno Capela, Steve Harris
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser General Public License as published by
  6. * the Free Software Foundation; either version 2.1 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * $Id$
  19. */
  20. #include <unistd.h>
  21. #include <string.h>
  22. #include <pthread.h>
  23. #include <stdarg.h>
  24. #include <stdio.h>
  25. #include <jack/messagebuffer.h>
  26. #define MB_BUFFERS 64 /* must be 2^n */
  27. static char mb_buffers[MB_BUFFERS][MB_BUFFERSIZE];
  28. static unsigned int mb_initialized = 0;
  29. static unsigned int mb_inbuffer = 0;
  30. static unsigned int mb_outbuffer = 0;
  31. static pthread_t mb_writer_thread;
  32. static pthread_mutex_t mb_write_lock;
  33. static pthread_cond_t mb_ready_cond;
  34. static void
  35. mb_flush()
  36. {
  37. while (mb_outbuffer != mb_inbuffer) {
  38. fputs(mb_buffers[mb_outbuffer], stderr);
  39. mb_outbuffer = (mb_outbuffer + 1) & (MB_BUFFERS - 1);
  40. }
  41. }
  42. static void *
  43. mb_thread_func(void *arg)
  44. {
  45. pthread_mutex_lock(&mb_write_lock);
  46. while (mb_initialized) {
  47. pthread_cond_wait(&mb_ready_cond, &mb_write_lock);
  48. mb_flush();
  49. }
  50. pthread_mutex_unlock(&mb_write_lock);
  51. return NULL;
  52. }
  53. void
  54. jack_messagebuffer_init ()
  55. {
  56. if (mb_initialized)
  57. return;
  58. pthread_mutex_init(&mb_write_lock, NULL);
  59. pthread_cond_init(&mb_ready_cond, NULL);
  60. mb_initialized = 1;
  61. if (pthread_create(&mb_writer_thread, NULL, &mb_thread_func, NULL) != 0)
  62. mb_initialized = 0;
  63. }
  64. void
  65. jack_messagebuffer_exit ()
  66. {
  67. if (!mb_initialized)
  68. return;
  69. pthread_mutex_lock(&mb_write_lock);
  70. mb_initialized = 0;
  71. pthread_cond_signal(&mb_ready_cond);
  72. pthread_mutex_unlock(&mb_write_lock);
  73. pthread_join(mb_writer_thread, NULL);
  74. mb_flush();
  75. pthread_mutex_destroy(&mb_write_lock);
  76. pthread_cond_destroy(&mb_ready_cond);
  77. }
  78. void
  79. jack_messagebuffer_add (const char *msg)
  80. {
  81. if (pthread_mutex_trylock(&mb_write_lock) == 0) {
  82. strncpy(mb_buffers[mb_inbuffer], msg, MB_BUFFERSIZE - 1);
  83. mb_inbuffer = (mb_inbuffer + 1) & (MB_BUFFERS - 1);
  84. pthread_cond_signal(&mb_ready_cond);
  85. pthread_mutex_unlock(&mb_write_lock);
  86. }
  87. }