summaryrefslogtreecommitdiff
path: root/src/lib/shm_ap_rbuff.c
diff options
context:
space:
mode:
authorSander Vrijders <[email protected]>2016-05-22 15:31:45 +0200
committerSander Vrijders <[email protected]>2016-05-22 15:31:45 +0200
commit0200dcb157be0e62eb495bc870cdac03130c2aa1 (patch)
treefe6459aadc00e81ef1252392dc07e009e3e8888e /src/lib/shm_ap_rbuff.c
parent745cc666e34d7e0c29822615987ce02832ac5a8c (diff)
parentdfe5a46fb5315112d1173ac983ffc416dc3ecf18 (diff)
downloadouroboros-0200dcb157be0e62eb495bc870cdac03130c2aa1.tar.gz
ouroboros-0200dcb157be0e62eb495bc870cdac03130c2aa1.zip
Merged in dstaesse/ouroboros/be-dealloc-full (pull request #105)
lib, ipcpd, irmd: fixes deallocation and fast path
Diffstat (limited to 'src/lib/shm_ap_rbuff.c')
-rw-r--r--src/lib/shm_ap_rbuff.c44
1 files changed, 26 insertions, 18 deletions
diff --git a/src/lib/shm_ap_rbuff.c b/src/lib/shm_ap_rbuff.c
index 18fedc88..1cfafeda 100644
--- a/src/lib/shm_ap_rbuff.c
+++ b/src/lib/shm_ap_rbuff.c
@@ -41,6 +41,8 @@
#define shm_rbuff_used(rb)((*rb->ptr_head + SHM_RBUFF_SIZE - *rb->ptr_tail) \
& (SHM_RBUFF_SIZE - 1))
#define shm_rbuff_free(rb)(shm_rbuff_used(rb) + 1 < SHM_RBUFF_SIZE)
+#define head_el_ptr (rb->shm_base + *rb->ptr_head)
+#define tail_el_ptr (rb->shm_base + *rb->ptr_tail)
struct shm_ap_rbuff {
struct rb_entry * shm_base; /* start of entry */
@@ -229,7 +231,7 @@ int shm_ap_rbuff_write(struct shm_ap_rbuff * rb, struct rb_entry * e)
return -1;
}
- *(rb->shm_base + *rb->ptr_head) = *e;
+ *head_el_ptr = *e;
*rb->ptr_head = (*rb->ptr_head + 1) & (SHM_RBUFF_SIZE -1);
pthread_mutex_unlock(rb->shm_mutex);
@@ -237,50 +239,56 @@ int shm_ap_rbuff_write(struct shm_ap_rbuff * rb, struct rb_entry * e)
return 0;
}
-
-int shm_ap_rbuff_peek(struct shm_ap_rbuff * rb)
+struct rb_entry * shm_ap_rbuff_read(struct shm_ap_rbuff * rb)
{
- int port_id = -1;
+ struct rb_entry * e = NULL;
+
+ if (rb == NULL)
+ return NULL;
pthread_mutex_lock(rb->shm_mutex);
if (shm_rbuff_used(rb) == 0) {
pthread_mutex_unlock(rb->shm_mutex);
- return -7; /* -EAGAIN */
+ return NULL;
}
- port_id = (rb->shm_base + *rb->ptr_tail)->port_id;
+ e = malloc(sizeof(*e));
+ if (e == NULL) {
+ pthread_mutex_unlock(rb->shm_mutex);
+ return NULL;
+ }
+
+ *e = *(rb->shm_base + *rb->ptr_tail);
+
+ *rb->ptr_tail = (*rb->ptr_tail + 1) & (SHM_RBUFF_SIZE -1);
pthread_mutex_unlock(rb->shm_mutex);
- return port_id;
+ return e;
}
-struct rb_entry * shm_ap_rbuff_read(struct shm_ap_rbuff * rb)
+ssize_t shm_ap_rbuff_read_port(struct shm_ap_rbuff * rb, int port_id)
{
- struct rb_entry * e = NULL;
-
- if (rb == NULL)
- return NULL;
+ ssize_t idx = -1;
pthread_mutex_lock(rb->shm_mutex);
if (shm_rbuff_used(rb) == 0) {
pthread_mutex_unlock(rb->shm_mutex);
- return NULL;
+ return -1;
}
- e = malloc(sizeof(*e));
- if (e == NULL) {
+ if (tail_el_ptr->port_id != port_id) {
pthread_mutex_unlock(rb->shm_mutex);
- return NULL;
+ return -1;
}
- *e = *(rb->shm_base + *rb->ptr_tail);
+ idx = tail_el_ptr->index;
*rb->ptr_tail = (*rb->ptr_tail + 1) & (SHM_RBUFF_SIZE -1);
pthread_mutex_unlock(rb->shm_mutex);
- return e;
+ return idx;
}