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 <linux/i2c-dev.h>
|
||||||
#include <i2c/smbus.h>
|
#include <i2c/smbus.h>
|
||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
@ -9,10 +11,11 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <pthread.h>
|
|
||||||
|
|
||||||
#include "global.h"
|
#include "global.h"
|
||||||
|
#ifndef RINGBUFFER_H
|
||||||
#include "ringbuffer.h"
|
#include "ringbuffer.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
#define IIC_DEVICE "/dev/i2c-7"
|
#define IIC_DEVICE "/dev/i2c-7"
|
||||||
#define BUFFER_BLOCKS 128
|
#define BUFFER_BLOCKS 128
|
||||||
|
@ -1,10 +1,6 @@
|
|||||||
#include <stdint.h>
|
#ifndef RINGBUFFER_H
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
#include "ringbuffer.h"
|
#include "ringbuffer.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
void _ringBufferIncReader(struct ringBuffer *buf) {
|
void _ringBufferIncReader(struct ringBuffer *buf) {
|
||||||
buf->reader += buf->blockSize;
|
buf->reader += buf->blockSize;
|
||||||
@ -34,32 +30,46 @@ void ringBufferCreate(int blocks, size_t blockSize, struct ringBuffer *out) {
|
|||||||
out->blockSize = blockSize;
|
out->blockSize = blockSize;
|
||||||
out->reader = out->buffer;
|
out->reader = out->buffer;
|
||||||
out->writer = out->buffer;
|
out->writer = out->buffer;
|
||||||
|
#ifdef RINGBUFFER_THREAD_SAFE
|
||||||
pthread_mutex_init(&out->mutex, NULL);
|
pthread_mutex_init(&out->mutex, NULL);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void ringBufferDestroy(struct ringBuffer *buf) {
|
void ringBufferDestroy(struct ringBuffer *buf) {
|
||||||
free(buf->buffer);
|
free(buf->buffer);
|
||||||
|
#ifdef RINGBUFFER_THREAD_SAFE
|
||||||
pthread_mutex_destroy(&buf->mutex);
|
pthread_mutex_destroy(&buf->mutex);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
int ringBufferRead(struct ringBuffer *buf, void *out) {
|
int ringBufferRead(struct ringBuffer *buf, void *out) {
|
||||||
|
#ifdef RINGBUFFER_THREAD_SAFE
|
||||||
pthread_mutex_lock(&buf->mutex);
|
pthread_mutex_lock(&buf->mutex);
|
||||||
|
#endif
|
||||||
if (buf->reader != buf->writer) {
|
if (buf->reader != buf->writer) {
|
||||||
// we have data to read
|
// we have data to read
|
||||||
memcpy(out, buf->reader, buf->blockSize);
|
memcpy(out, buf->reader, buf->blockSize);
|
||||||
_ringBufferIncReader(buf);
|
_ringBufferIncReader(buf);
|
||||||
|
#ifdef RINGBUFFER_THREAD_SAFE
|
||||||
pthread_mutex_unlock(&buf->mutex);
|
pthread_mutex_unlock(&buf->mutex);
|
||||||
|
#endif
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
} else {
|
||||||
// nothing to read
|
// nothing to read
|
||||||
|
#ifdef RINGBUFFER_THREAD_SAFE
|
||||||
pthread_mutex_unlock(&buf->mutex);
|
pthread_mutex_unlock(&buf->mutex);
|
||||||
|
#endif
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ringBufferWrite(struct ringBuffer *buf, void *in) {
|
void ringBufferWrite(struct ringBuffer *buf, void *in) {
|
||||||
|
#ifdef RINGBUFFER_THREAD_SAFE
|
||||||
pthread_mutex_lock(&buf->mutex);
|
pthread_mutex_lock(&buf->mutex);
|
||||||
|
#endif
|
||||||
memcpy(buf->writer, in, buf->blockSize);
|
memcpy(buf->writer, in, buf->blockSize);
|
||||||
_ringBufferIncWriter(buf);
|
_ringBufferIncWriter(buf);
|
||||||
|
#ifdef RINGBUFFER_THREAD_SAFE
|
||||||
pthread_mutex_unlock(&buf->mutex);
|
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>
|
#include <pthread.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define RINGBUFFER_H
|
||||||
|
|
||||||
struct ringBuffer {
|
struct ringBuffer {
|
||||||
void *buffer;
|
void *buffer;
|
||||||
@ -6,7 +14,9 @@ struct ringBuffer {
|
|||||||
size_t blockSize;
|
size_t blockSize;
|
||||||
void *reader;
|
void *reader;
|
||||||
void *writer;
|
void *writer;
|
||||||
|
#ifdef RINGBUFFER_THREAD_SAFE
|
||||||
pthread_mutex_t mutex;
|
pthread_mutex_t mutex;
|
||||||
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
void ringBufferCreate(int blocks, size_t blockSize, struct ringBuffer *out);
|
void ringBufferCreate(int blocks, size_t blockSize, struct ringBuffer *out);
|
||||||
|
Reference in New Issue
Block a user