summaryrefslogtreecommitdiff
path: root/libjack/messagebuffer.c
blob: 082f2f15a4c4177cda2c059373574bd83a75621e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
 * messagebuffer.c -- realtime-safe message handling for jackd.
 *
 *  This interface is included in libjack so backend drivers can use
 *  it, *not* for external client processes.  It implements the
 *  VERBOSE() and MESSAGE() macros in a realtime-safe manner.
 */

/*
 *  Copyright (C) 2004 Rui Nuno Capela, Steve Harris
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation; either version 2.1 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 */

#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <stdarg.h>
#include <stdio.h>

#include "messagebuffer.h"
#include "atomicity.h"
#include "internal.h"

/* MB_NEXT() relies on the fact that MB_BUFFERS is a power of two */
#define MB_BUFFERS      128
#define MB_NEXT(index) ((index + 1) & (MB_BUFFERS - 1))
#define MB_BUFFERSIZE   256             /* message length limit */

static char mb_buffers[MB_BUFFERS][MB_BUFFERSIZE];
static volatile unsigned int mb_initialized = 0;
static volatile unsigned int mb_inbuffer = 0;
static volatile unsigned int mb_outbuffer = 0;
static volatile _Atomic_word mb_overruns = 0;
static pthread_t mb_writer_thread;
static pthread_mutex_t mb_write_lock;
static pthread_cond_t mb_ready_cond;
static void (*mb_thread_init_callback)(void*) = 0;
static void* mb_thread_init_callback_arg = 0;

static void
mb_flush ()
{
	/* called WITHOUT the mb_write_lock */
	while (mb_outbuffer != mb_inbuffer) {
		jack_info (mb_buffers[mb_outbuffer]);
		mb_outbuffer = MB_NEXT (mb_outbuffer);
	}
}

static void *
mb_thread_func (void *arg)
{
	/* The mutex is only to eliminate collisions between multiple
	 * writer threads and protect the condition variable. */
	pthread_mutex_lock (&mb_write_lock);

	while (mb_initialized) {
		pthread_cond_wait (&mb_ready_cond, &mb_write_lock);

		if (mb_thread_init_callback) {
			/* the client asked for all threads to run a thread
			   initialization callback, which includes us.
			 */
			mb_thread_init_callback (mb_thread_init_callback_arg);
			mb_thread_init_callback = 0;

			/* note that we've done it */
			pthread_cond_signal (&mb_ready_cond);
		}

		/* releasing the mutex reduces contention */
		pthread_mutex_unlock (&mb_write_lock);
		mb_flush ();
		pthread_mutex_lock (&mb_write_lock);
	}

	pthread_mutex_unlock (&mb_write_lock);

	return NULL;
}

void
jack_messagebuffer_init ()
{
	if (mb_initialized) {
		return;
	}

	pthread_mutex_init (&mb_write_lock, NULL);
	pthread_cond_init (&mb_ready_cond, NULL);

	mb_overruns = 0;
	mb_initialized = 1;

	if (jack_thread_creator (&mb_writer_thread, NULL, &mb_thread_func, NULL) != 0) {
		mb_initialized = 0;
	}
}

void
jack_messagebuffer_exit ()
{
	if (!mb_initialized) {
		return;
	}

	pthread_mutex_lock (&mb_write_lock);
	mb_initialized = 0;
	pthread_cond_signal (&mb_ready_cond);
	pthread_mutex_unlock (&mb_write_lock);

	pthread_join (mb_writer_thread, NULL);
	mb_flush ();

	if (mb_overruns) {
		jack_error ("WARNING: %d message buffer overruns!",
			    mb_overruns);
	}

	pthread_mutex_destroy (&mb_write_lock);
	pthread_cond_destroy (&mb_ready_cond);
}


void
jack_messagebuffer_add (const char *fmt, ...)
{
	char msg[MB_BUFFERSIZE];
	va_list ap;

	/* format the message first, to reduce lock contention */
	va_start (ap, fmt);
	vsnprintf (msg, MB_BUFFERSIZE, fmt, ap);
	va_end (ap);

	if (!mb_initialized) {
		/* Unable to print message with realtime safety.
		 * Complain and print it anyway. */
		fprintf (stderr, "ERROR: messagebuffer not initialized: %s",
			 msg);
		return;
	}

	if (pthread_mutex_trylock (&mb_write_lock) == 0) {
		strncpy (mb_buffers[mb_inbuffer], msg, MB_BUFFERSIZE);
		mb_inbuffer = MB_NEXT (mb_inbuffer);
		pthread_cond_signal (&mb_ready_cond);
		pthread_mutex_unlock (&mb_write_lock);
	} else {                        /* lock collision */
		exchange_and_add (&mb_overruns, 1);
	}
}

void
jack_messagebuffer_thread_init (void (*cb)(void*), void* arg)
{
	pthread_mutex_lock (&mb_write_lock);

	/* set up the callback */
	mb_thread_init_callback_arg = arg;
	mb_thread_init_callback = cb;

	/* wake msg buffer thread */
	pthread_cond_signal (&mb_ready_cond);

	/* wait for it to be done */
	pthread_cond_wait (&mb_ready_cond, &mb_write_lock);

	/* and we're done */
	pthread_mutex_unlock (&mb_write_lock);
}