Skip to content
Snippets Groups Projects
Commit c39a14a2 authored by Barbu Alexandru Daniel's avatar Barbu Alexandru Daniel
Browse files

I have written the enqueue function. Accidentally pushing to main, Sorry SO team! :////


Signed-off-by: default avatarBarbu Alexandru Daniel <barbualex.daniel2004@gmail.com>
parent bbb714ab
No related branches found
No related tags found
No related merge requests found
Pipeline #90220 passed
......@@ -2,9 +2,19 @@
#include "ring_buffer.h"
#define RB_DATA_SIZE sizeof(char)
int is_initialised;
pthread_mutex_t init_mutex = PTHREAD_MUTEX_INITIALIZER;
/*
* @brief Initialises the ring buffer
*
* If ring buffer is not initialised it gets initialised with most values set to 0.
* Operation should be thread safe because of the init_mutex mutex.
*
* @note data field of ring buffer has the size of cap * packet_size
*/
int ring_buffer_init(so_ring_buffer_t *ring, size_t cap)
{
/* TODO: implement ring_buffer_init */
......@@ -21,7 +31,7 @@ int ring_buffer_init(so_ring_buffer_t *ring, size_t cap)
ring->read_pos = 0;
ring->write_pos = 0;
ring->data = calloc(cap, sizeof(char));
ring->data = calloc(cap, RB_DATA_SIZE);
is_initialised = 1;
......@@ -35,20 +45,60 @@ int ring_buffer_init(so_ring_buffer_t *ring, size_t cap)
ssize_t ring_buffer_enqueue(so_ring_buffer_t *ring, void *data, size_t size)
{
/* TODO: implement ring_buffer_enqueue */
(void) ring;
(void) data;
(void) size;
// wait for the buffer to have space to add element in it
sem_wait(&ring->rb_full);
// try getting the mutex and locking it
pthread_mutex_lock(&ring->mutex);
// we have space and we are running so we copi the data inside ring buffer
memcpy(ring->data + ring->write_pos, data, size);
// lenght is increased
ring->len ++;
// write possition is increased and looped if necessarily
ring->write_pos ++;
if (ring->write_pos >= ring->cap)
ring->write_pos %= ring->cap; // operaation might fail sometimes based on my web research
// notify sleeping threads that the buffer has at least one element in it
// that can be removed
sem_post(&ring->rb_empty);
// unlock the mutex of my adding operation
pthread_mutex_unlock(&ring->mutex);
return -1;
}
ssize_t ring_buffer_dequeue(so_ring_buffer_t *ring, void *data, size_t size)
{
/* TODO: Implement ring_buffer_dequeue */
(void) ring;
(void) data;
(void) size;
// wait for the buffer to have at least one element inside
sem_wait(&ring->rb_empty);
// try getting the mutex and locking it
pthread_mutex_lock(&ring->mutex);
// we have space and we are running so we copi the data inside ring buffer
memcpy(ring->data + ring->write_pos, data, size);
// lenght is increased
ring->len ++;
// write possition is increased and looped if necessarily
ring->write_pos ++;
if (ring->write_pos >= ring->cap)
ring->write_pos %= ring->cap; // operaation might fail sometimes based on my web research
// notify sleeping threads that the buffer has at least one element in it
// that can be removed
sem_post(&ring->rb_empty);
// unlock the mutex of my adding operation
pthread_mutex_unlock(&ring->mutex);
return -1;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment