-
Notifications
You must be signed in to change notification settings - Fork 0
/
ringbuffer.c
62 lines (53 loc) · 1.67 KB
/
ringbuffer.c
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
/*
gpioctl
Copyright (C) 2019 Jörn Nettingsmeier
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ringbuffer.h"
#include <jack/ringbuffer.h>
#include <pthread.h>
#include <errno.h>
#include "globals.h"
// event ringbuffer and lock
static jack_ringbuffer_t *buf;
static pthread_mutex_t buflock = PTHREAD_MUTEX_INITIALIZER;
int setup_ringbuffer(int nbytes)
{
DBG("Setting up ringbuffer of %d bytes.", nbytes);
buf = jack_ringbuffer_create(nbytes);
if (buf == NULL) {
ERR("Could not create JACK ringbuffer.");
return -ENOMEM;
}
jack_ringbuffer_mlock(buf);
return 0;
}
int shutdown_ringbuffer()
{
DBG("Shutting down ringbuffer");
jack_ringbuffer_free(buf);
return 0;
}
int ringbuffer_write(unsigned char *msg, size_t size)
{
int nbytes;
// there can be multiple writer threads, ensure only
// one can write at the same time:
pthread_mutex_lock(&buflock);
nbytes = jack_ringbuffer_write(buf, (char *)msg, size);
pthread_mutex_unlock(&buflock);
return nbytes;
}
int ringbuffer_read(unsigned char *msg, size_t size)
{
return jack_ringbuffer_read(buf, (char *)msg, size);
}