summaryrefslogtreecommitdiff
path: root/src/lib/shm_ap_rbuff.c
diff options
context:
space:
mode:
authordimitri staessens <[email protected]>2016-05-22 14:53:22 +0200
committerdimitri staessens <[email protected]>2016-05-22 15:04:20 +0200
commitdfe5a46fb5315112d1173ac983ffc416dc3ecf18 (patch)
treefe6459aadc00e81ef1252392dc07e009e3e8888e /src/lib/shm_ap_rbuff.c
parent745cc666e34d7e0c29822615987ce02832ac5a8c (diff)
downloadouroboros-dfe5a46fb5315112d1173ac983ffc416dc3ecf18.tar.gz
ouroboros-dfe5a46fb5315112d1173ac983ffc416dc3ecf18.zip
lib, ipcpd, irmd: fixes deallocation and fast path
The fast path has been rewritten to have certainty to read the correct flow. Deallocation will not release port_id's or fd's until they are explicitly released locally.
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;
}