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.

84 lines
2.0KB

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