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.

88 lines
1.9KB

  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 32 /* 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 void
  33. mb_flush()
  34. {
  35. while (mb_outbuffer != mb_inbuffer) {
  36. fputs(mb_buffers[mb_outbuffer], stderr);
  37. mb_outbuffer = (mb_outbuffer + 1) & (MB_BUFFERS - 1);
  38. }
  39. }
  40. static void *
  41. mb_thread_func(void *arg)
  42. {
  43. while (mb_initialized) {
  44. usleep(1000);
  45. mb_flush();
  46. }
  47. return NULL;
  48. }
  49. void
  50. jack_messagebuffer_init ()
  51. {
  52. if (mb_initialized)
  53. return;
  54. mb_initialized = 1;
  55. if (pthread_create(&mb_writer_thread, NULL, &mb_thread_func, NULL) != 0)
  56. mb_initialized = 0;
  57. }
  58. void
  59. jack_messagebuffer_exit ()
  60. {
  61. if (!mb_initialized)
  62. return;
  63. mb_initialized = 0;
  64. pthread_join(mb_writer_thread, NULL);
  65. mb_flush();
  66. }
  67. void
  68. jack_messagebuffer_add (const char *msg)
  69. {
  70. strncpy(mb_buffers[mb_inbuffer], msg, MB_BUFFERSIZE - 1);
  71. mb_inbuffer = (mb_inbuffer + 1) & (MB_BUFFERS - 1);
  72. }