2 * messagebuffer.c -- realtime-safe message handling for jackd.
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.
10 * Copyright (C) 2004 Rui Nuno Capela, Steve Harris
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License as published by
14 * the Free Software Foundation; either version 2.1 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
34 #include <jack/messagebuffer.h>
35 #include <jack/atomicity.h>
36 #include <jack/internal.h>
38 /* MB_NEXT() relies on the fact that MB_BUFFERS is a power of two */
39 #define MB_BUFFERS 128
40 #define MB_NEXT(index) ((index+1) & (MB_BUFFERS-1))
41 #define MB_BUFFERSIZE 256 /* message length limit */
43 static char mb_buffers
[MB_BUFFERS
][MB_BUFFERSIZE
];
44 static volatile unsigned int mb_initialized
= 0;
45 static volatile unsigned int mb_inbuffer
= 0;
46 static volatile unsigned int mb_outbuffer
= 0;
47 static volatile _Atomic_word mb_overruns
= 0;
48 static pthread_t mb_writer_thread
;
49 static pthread_mutex_t mb_write_lock
;
50 static pthread_cond_t mb_ready_cond
;
55 /* called WITHOUT the mb_write_lock */
56 while (mb_outbuffer
!= mb_inbuffer
) {
57 jack_info(mb_buffers
[mb_outbuffer
]);
58 mb_outbuffer
= MB_NEXT(mb_outbuffer
);
63 mb_thread_func(void *arg
)
65 /* The mutex is only to eliminate collisions between multiple
66 * writer threads and protect the condition variable. */
67 pthread_mutex_lock(&mb_write_lock
);
69 while (mb_initialized
) {
70 pthread_cond_wait(&mb_ready_cond
, &mb_write_lock
);
72 /* releasing the mutex reduces contention */
73 pthread_mutex_unlock(&mb_write_lock
);
75 pthread_mutex_lock(&mb_write_lock
);
78 pthread_mutex_unlock(&mb_write_lock
);
84 jack_messagebuffer_init ()
89 pthread_mutex_init(&mb_write_lock
, NULL
);
90 pthread_cond_init(&mb_ready_cond
, NULL
);
95 if (pthread_create(&mb_writer_thread
, NULL
, &mb_thread_func
, NULL
) != 0)
100 jack_messagebuffer_exit ()
105 pthread_mutex_lock(&mb_write_lock
);
107 pthread_cond_signal(&mb_ready_cond
);
108 pthread_mutex_unlock(&mb_write_lock
);
110 pthread_join(mb_writer_thread
, NULL
);
114 jack_error("WARNING: %d message buffer overruns!",
117 pthread_mutex_destroy(&mb_write_lock
);
118 pthread_cond_destroy(&mb_ready_cond
);
123 jack_messagebuffer_add (const char *fmt
, ...)
125 char msg
[MB_BUFFERSIZE
];
128 /* format the message first, to reduce lock contention */
130 vsnprintf(msg
, MB_BUFFERSIZE
, fmt
, ap
);
133 if (!mb_initialized
) {
134 /* Unable to print message with realtime safety.
135 * Complain and print it anyway. */
136 fprintf(stderr
, "ERROR: messagebuffer not initialized: %s",
141 if (pthread_mutex_trylock(&mb_write_lock
) == 0) {
142 strncpy(mb_buffers
[mb_inbuffer
], msg
, MB_BUFFERSIZE
);
143 mb_inbuffer
= MB_NEXT(mb_inbuffer
);
144 pthread_cond_signal(&mb_ready_cond
);
145 pthread_mutex_unlock(&mb_write_lock
);
146 } else { /* lock collision */
147 atomic_add(&mb_overruns
, 1);