Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implemented task 2 #1

Merged
merged 1 commit into from
Jul 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 77 additions & 4 deletions src/ringbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,52 @@
#include <string.h>
#include <sys/types.h>

#define TIMEOUT 1

void ringbuffer_init(rbctx_t *context, void *buffer_location,
size_t buffer_size) {
context->begin = (uint8_t *)buffer_location;
context->end = context->begin + buffer_size;
context->read = context->begin;
context->write = context->begin;

pthread_mutex_init(&context->mutex_read, NULL);
pthread_mutex_init(&context->mutex_write, NULL);
pthread_cond_init(&context->signal_read, NULL);
pthread_cond_init(&context->signal_write, NULL);
}

int ringbuffer_write(rbctx_t *context, void *message, size_t message_len) {
if (message_len >= context->end - context->begin) {
return OUTPUT_BUFFER_TOO_SMALL;
pthread_mutex_lock(&context->mutex_write);

struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
ts.tv_sec += TIMEOUT;
int rt = 0;

size_t free_space =
(context->write > context->read)
? (context->end - context->write + context->read - context->begin - 1)
: (context->read - context->write - 1);

while (free_space < message_len + 1) {
rt = pthread_cond_timedwait(&context->signal_write, &context->mutex_write,
&ts);
if (rt != 0) {
pthread_mutex_unlock(&context->mutex_write);
return RBUF_TIMEOUT;
}
free_space = (context->write > context->read)
? (context->end - context->write + context->read -
context->begin - 1)
: (context->read - context->write - 1);
}

context->write[0] = (uint8_t)message_len;
context->write += 1;
if (context->write >= context->end) {
context->write = context->begin;
}

for (size_t i = 0; i < message_len; i++) {
context->write[0] = ((char *)message)[i];
Expand All @@ -29,19 +60,53 @@ int ringbuffer_write(rbctx_t *context, void *message, size_t message_len) {
context->write = context->begin;
}
}

pthread_mutex_unlock(&context->mutex_write);
pthread_cond_signal(&context->signal_read);
return SUCCESS;
}

int ringbuffer_read(rbctx_t *context, void *buffer, size_t *buffer_len) {
pthread_mutex_lock(&context->mutex_read);

if (context->read == context->write) {
return RINGBUFFER_EMPTY; // temporary solution
pthread_mutex_unlock(&context->mutex_read);
return RINGBUFFER_EMPTY;
}

uint8_t message_len = context->read[0];
if (message_len > *buffer_len) {
pthread_mutex_unlock(&context->mutex_read);
return OUTPUT_BUFFER_TOO_SMALL;
}

struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
ts.tv_sec += TIMEOUT;
int rt = 0;

size_t read_available =
(context->read > context->write)
? (context->end - context->read + context->write - context->begin - 1)
: (context->write - context->read - 1);

while (read_available < message_len) {
rt = pthread_cond_timedwait(&context->signal_read, &context->mutex_read,
&ts);
if (rt != 0) {
pthread_mutex_unlock(&context->mutex_read);
return RBUF_TIMEOUT;
}
read_available = (context->read > context->write)
? (context->end - context->read + context->write -
context->begin - 1)
: (context->write - context->read - 1);
}

context->read += 1;
if (context->read >= context->end) {
context->read = context->begin;
}

*buffer_len = message_len;
for (size_t i = 0; i < message_len; i++) {
Expand All @@ -52,7 +117,15 @@ int ringbuffer_read(rbctx_t *context, void *buffer, size_t *buffer_len) {
context->read = context->begin;
}
}

pthread_mutex_unlock(&context->mutex_read);
pthread_cond_signal(&context->signal_write);
return SUCCESS;
}

void ringbuffer_destroy(rbctx_t *context) {}
void ringbuffer_destroy(rbctx_t *context) {
pthread_mutex_destroy(&context->mutex_read);
pthread_mutex_destroy(&context->mutex_write);
pthread_cond_destroy(&context->signal_read);
pthread_cond_destroy(&context->signal_write);
}
Loading