#include <jack/jack.h>
#include <alsa/asoundlib.h>
#include <alsa/pcm_external.h>
+#include <pthread.h>
#define MAX_PERIODS_MULTIPLE 64
int fd;
int activated; /* jack is activated? */
+ pthread_mutex_t running_mutex;
int running; /* jack is running? */
snd_pcm_jack_port_list_t **port_names;
free(jack->port_names);
jack->port_names = NULL;
}
+ pthread_mutex_destroy (&jack->running_mutex);
if (jack->fd >= 0)
close(jack->fd);
if (jack->io.poll_fd >= 0)
snd_pcm_uframes_t xfer = 0;
unsigned int channel;
- if (!jack->running)
+ if (pthread_mutex_trylock (&jack->running_mutex) == EBUSY) {
+ /* Note that locking should only ever fail if
+ * snd_pcm_jack_start or snd_pcm_jack_stop is called at the
+ * same time, in which case dropping the current buffer is not
+ * an issue. */
return 0;
+ }
+ if (!jack->running) {
+ pthread_mutex_unlock (&jack->running_mutex);
+ return 0;
+ }
for (channel = 0; channel < io->channels; channel++) {
jack->areas[channel].addr =
pcm_poll_unblock_check(io); /* unblock socket for polling if needed */
+ pthread_mutex_unlock (&jack->running_mutex);
+
return 0;
}
static int snd_pcm_jack_start(snd_pcm_ioplug_t *io)
{
snd_pcm_jack_t *jack = io->private_data;
-
- jack->running = jack->activated;
+ pthread_mutex_lock (&jack->running_mutex);
+ jack->running = 1;
+ pthread_mutex_unlock (&jack->running_mutex);
/*
* Since the processing of jack_activate() and jack_connect() take a
* while longer, snd_pcm_jack_start() was blocked.
static int snd_pcm_jack_stop(snd_pcm_ioplug_t *io)
{
snd_pcm_jack_t *jack = io->private_data;
-
+ pthread_mutex_lock (&jack->running_mutex);
jack->running = 0;
+ pthread_mutex_unlock (&jack->running_mutex);
return 0;
}
jack_deactivate(jack->client);
jack->activated = 0;
}
- jack->running = 0;
#if 0
unsigned i;
for (i = 0; i < io->channels; i++) {
if (!jack)
return -ENOMEM;
+ pthread_mutex_init (&jack->running_mutex, NULL);
+
jack->fd = -1;
jack->io.poll_fd = -1;
jack->use_period_alignment = use_period_alignment;