Skip to content
Snippets Groups Projects
Commit 05018f2d authored by Alexandru-Daniel BARBU's avatar Alexandru-Daniel BARBU
Browse files

Merge branch 'solutions' into 'master'

Finished function dequeue. Pushing to branch solutions from now on.

See merge request !1
parents c39a14a2 9596751c
No related branches found
No related tags found
1 merge request!1Finished function dequeue. Pushing to branch solutions from now on.
Pipeline #90222 passed
...@@ -82,22 +82,23 @@ ssize_t ring_buffer_dequeue(so_ring_buffer_t *ring, void *data, size_t size) ...@@ -82,22 +82,23 @@ ssize_t ring_buffer_dequeue(so_ring_buffer_t *ring, void *data, size_t size)
// try getting the mutex and locking it // try getting the mutex and locking it
pthread_mutex_lock(&ring->mutex); pthread_mutex_lock(&ring->mutex);
// we have space and we are running so we copi the data inside ring buffer // we have something and we are running so we copy the data from
memcpy(ring->data + ring->write_pos, data, size); // the ring buffer to the data parameter
memcpy(data, ring->data + ring->read_pos, size);
// lenght is increased // lenght is decreased
ring->len ++; ring->len --;
// write possition is increased and looped if necessarily // read possition is increased and looped if necessarily
ring->write_pos ++; ring->read_pos ++;
if (ring->write_pos >= ring->cap) if (ring->read_pos >= ring->cap)
ring->write_pos %= ring->cap; // operaation might fail sometimes based on my web research ring->read_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 // notify sleeping threads that the buffer
// that can be removed // has room for one element to be added
sem_post(&ring->rb_empty); sem_post(&ring->rb_full);
// unlock the mutex of my adding operation // unlock the mutex of my removing operation
pthread_mutex_unlock(&ring->mutex); pthread_mutex_unlock(&ring->mutex);
return -1; 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