summaryrefslogtreecommitdiff
path: root/src/lib/shm_ap_rbuff.c
diff options
context:
space:
mode:
authorSander Vrijders <[email protected]>2016-08-09 14:51:53 +0200
committerSander Vrijders <[email protected]>2016-08-09 14:51:53 +0200
commitd7a3ac1eae57404630b39ad68b548910c88ed150 (patch)
tree4cbb537fe8d1d991f37788caed311db26e50a007 /src/lib/shm_ap_rbuff.c
parent4450f5e612a40a10f385e67d5931a91a5c326c26 (diff)
parent067c5661859f6a1f75adb0bb325d1641fed221a1 (diff)
downloadouroboros-d7a3ac1eae57404630b39ad68b548910c88ed150.tar.gz
ouroboros-d7a3ac1eae57404630b39ad68b548910c88ed150.zip
Merged in dstaesse/ouroboros/be-bugfixing (pull request #194)
lib: shm_ap_rbuff: Fix cleanup of locks
Diffstat (limited to 'src/lib/shm_ap_rbuff.c')
-rw-r--r--src/lib/shm_ap_rbuff.c49
1 files changed, 20 insertions, 29 deletions
diff --git a/src/lib/shm_ap_rbuff.c b/src/lib/shm_ap_rbuff.c
index 683d0185..62ef5cf1 100644
--- a/src/lib/shm_ap_rbuff.c
+++ b/src/lib/shm_ap_rbuff.c
@@ -247,10 +247,6 @@ void shm_ap_rbuff_destroy(struct shm_ap_rbuff * rb)
}
}
- pthread_mutex_destroy(rb->lock);
- pthread_cond_destroy(rb->add);
- pthread_cond_destroy(rb->del);
-
if (close(rb->fd) < 0)
LOG_DBG("Couldn't close shared memory.");
@@ -326,13 +322,14 @@ int shm_ap_rbuff_peek(struct shm_ap_rbuff * rb,
pthread_mutex_consistent(rb->lock);
}
- if (ret == ETIMEDOUT) {
- pthread_mutex_unlock(rb->lock);
- return -ret;
- }
+ if (ret == ETIMEDOUT)
+ break;
}
- ret = (rb->shm_base + *rb->ptr_tail)->port_id;
+ if (ret != ETIMEDOUT)
+ ret = (rb->shm_base + *rb->ptr_tail)->port_id;
+ else
+ ret = -ETIMEDOUT;
pthread_cleanup_pop(true);
@@ -361,15 +358,11 @@ struct rb_entry * shm_ap_rbuff_read(struct shm_ap_rbuff * rb)
}
e = malloc(sizeof(*e));
- if (e == NULL) {
- pthread_mutex_unlock(rb->lock);
- return NULL;
+ if (e != NULL) {
+ *e = *(rb->shm_base + *rb->ptr_tail);
+ *rb->ptr_tail = (*rb->ptr_tail + 1) & (SHM_BUFFER_SIZE -1);
}
- *e = *(rb->shm_base + *rb->ptr_tail);
-
- *rb->ptr_tail = (*rb->ptr_tail + 1) & (SHM_BUFFER_SIZE -1);
-
pthread_cleanup_pop(true);
return e;
@@ -394,7 +387,6 @@ ssize_t shm_ap_rbuff_read_port(struct shm_ap_rbuff * rb, int port_id)
*rb->ptr_tail = (*rb->ptr_tail + 1) & (SHM_BUFFER_SIZE -1);
pthread_cond_broadcast(rb->del);
-
pthread_mutex_unlock(rb->lock);
return idx;
@@ -421,7 +413,8 @@ ssize_t shm_ap_rbuff_read_port_b(struct shm_ap_rbuff * rb,
pthread_cleanup_push((void(*)(void *))pthread_mutex_unlock,
(void *) rb->lock);
- while (shm_rbuff_empty(rb) || tail_el_ptr(rb)->port_id != port_id) {
+ while ((shm_rbuff_empty(rb) || tail_el_ptr(rb)->port_id != port_id)
+ && (ret != ETIMEDOUT)) {
while (shm_rbuff_empty(rb)) {
if (timeout != NULL)
ret = pthread_cond_timedwait(rb->add,
@@ -435,10 +428,8 @@ ssize_t shm_ap_rbuff_read_port_b(struct shm_ap_rbuff * rb,
pthread_mutex_consistent(rb->lock);
}
- if (ret == ETIMEDOUT) {
- pthread_mutex_unlock(rb->lock);
- return -ret;
- }
+ if (ret == ETIMEDOUT)
+ break;
}
while (tail_el_ptr(rb)->port_id != port_id) {
@@ -454,17 +445,17 @@ ssize_t shm_ap_rbuff_read_port_b(struct shm_ap_rbuff * rb,
pthread_mutex_consistent(rb->lock);
}
- if (ret == ETIMEDOUT) {
- pthread_mutex_unlock(rb->lock);
- return -ret;
- }
+ if (ret == ETIMEDOUT)
+ break;
}
}
- idx = tail_el_ptr(rb)->index;
- *rb->ptr_tail = (*rb->ptr_tail + 1) & (SHM_BUFFER_SIZE -1);
+ if (ret != ETIMEDOUT) {
+ idx = tail_el_ptr(rb)->index;
+ *rb->ptr_tail = (*rb->ptr_tail + 1) & (SHM_BUFFER_SIZE -1);
- pthread_cond_broadcast(rb->del);
+ pthread_cond_broadcast(rb->del);
+ }
pthread_cleanup_pop(true);