Compare commits
5 Commits
52e2c96e0e
...
main
Author | SHA1 | Date | |
---|---|---|---|
8cbd64034b | |||
99c2f4f0d4 | |||
095fca6b3d | |||
69d537bddd | |||
11146fb632 |
@ -1,3 +1,5 @@
|
||||
#define RINGBUFFER_THREAD_SAFE
|
||||
|
||||
#include <linux/i2c-dev.h>
|
||||
#include <i2c/smbus.h>
|
||||
#include <sys/ioctl.h>
|
||||
@ -9,10 +11,11 @@
|
||||
#include <stdint.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#include "global.h"
|
||||
#ifndef RINGBUFFER_H
|
||||
#include "ringbuffer.h"
|
||||
#endif
|
||||
|
||||
#define IIC_DEVICE "/dev/i2c-7"
|
||||
#define BUFFER_BLOCKS 128
|
||||
|
@ -1,10 +1,6 @@
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#ifndef RINGBUFFER_H
|
||||
#include "ringbuffer.h"
|
||||
#endif
|
||||
|
||||
void _ringBufferIncReader(struct ringBuffer *buf) {
|
||||
buf->reader += buf->blockSize;
|
||||
@ -34,32 +30,46 @@ void ringBufferCreate(int blocks, size_t blockSize, struct ringBuffer *out) {
|
||||
out->blockSize = blockSize;
|
||||
out->reader = out->buffer;
|
||||
out->writer = out->buffer;
|
||||
#ifdef RINGBUFFER_THREAD_SAFE
|
||||
pthread_mutex_init(&out->mutex, NULL);
|
||||
#endif
|
||||
}
|
||||
|
||||
void ringBufferDestroy(struct ringBuffer *buf) {
|
||||
free(buf->buffer);
|
||||
#ifdef RINGBUFFER_THREAD_SAFE
|
||||
pthread_mutex_destroy(&buf->mutex);
|
||||
#endif
|
||||
}
|
||||
|
||||
int ringBufferRead(struct ringBuffer *buf, void *out) {
|
||||
#ifdef RINGBUFFER_THREAD_SAFE
|
||||
pthread_mutex_lock(&buf->mutex);
|
||||
#endif
|
||||
if (buf->reader != buf->writer) {
|
||||
// we have data to read
|
||||
memcpy(out, buf->reader, buf->blockSize);
|
||||
_ringBufferIncReader(buf);
|
||||
#ifdef RINGBUFFER_THREAD_SAFE
|
||||
pthread_mutex_unlock(&buf->mutex);
|
||||
#endif
|
||||
return 0;
|
||||
} else {
|
||||
// nothing to read
|
||||
#ifdef RINGBUFFER_THREAD_SAFE
|
||||
pthread_mutex_unlock(&buf->mutex);
|
||||
#endif
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
void ringBufferWrite(struct ringBuffer *buf, void *in) {
|
||||
#ifdef RINGBUFFER_THREAD_SAFE
|
||||
pthread_mutex_lock(&buf->mutex);
|
||||
#endif
|
||||
memcpy(buf->writer, in, buf->blockSize);
|
||||
_ringBufferIncWriter(buf);
|
||||
#ifdef RINGBUFFER_THREAD_SAFE
|
||||
pthread_mutex_unlock(&buf->mutex);
|
||||
#endif
|
||||
}
|
||||
|
@ -1,4 +1,12 @@
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifdef RINGBUFFER_THREAD_SAFE
|
||||
#include <pthread.h>
|
||||
#endif
|
||||
|
||||
#define RINGBUFFER_H
|
||||
|
||||
struct ringBuffer {
|
||||
void *buffer;
|
||||
@ -6,7 +14,9 @@ struct ringBuffer {
|
||||
size_t blockSize;
|
||||
void *reader;
|
||||
void *writer;
|
||||
#ifdef RINGBUFFER_THREAD_SAFE
|
||||
pthread_mutex_t mutex;
|
||||
#endif
|
||||
};
|
||||
|
||||
void ringBufferCreate(int blocks, size_t blockSize, struct ringBuffer *out);
|
||||
|
Reference in New Issue
Block a user