summaryrefslogtreecommitdiff
path: root/src/lib/shm_rbuff_ll.c
diff options
context:
space:
mode:
authordimitri staessens <[email protected]>2017-04-01 21:08:17 +0000
committerSander Vrijders <[email protected]>2017-04-01 21:08:17 +0000
commita240900525c0ffba92588a3deca88dfec82125e3 (patch)
tree9a15aeced5cc0409418bb726a5aa438b309b6eef /src/lib/shm_rbuff_ll.c
parent4aa99753a5fa9afa3230d4eea166a4fa4c21dbbd (diff)
parent4c393500bcf2b35166b98f0def4c767db712cc9e (diff)
downloadouroboros-a240900525c0ffba92588a3deca88dfec82125e3.tar.gz
ouroboros-a240900525c0ffba92588a3deca88dfec82125e3.zip
Merged in dstaesse/ouroboros/be-rbuff-ll (pull request #451)
lib: Fix some issues in lockless rbuff
Diffstat (limited to 'src/lib/shm_rbuff_ll.c')
-rw-r--r--src/lib/shm_rbuff_ll.c19
1 files changed, 12 insertions, 7 deletions
diff --git a/src/lib/shm_rbuff_ll.c b/src/lib/shm_rbuff_ll.c
index b5b8bed2..d777de8b 100644
--- a/src/lib/shm_rbuff_ll.c
+++ b/src/lib/shm_rbuff_ll.c
@@ -228,6 +228,8 @@ int shm_rbuff_write(struct shm_rbuff * rb,
size_t ohead;
size_t nhead;
+ bool was_empty = false;
+
assert(rb);
assert(idx < SHM_BUFFER_SIZE);
@@ -238,11 +240,9 @@ int shm_rbuff_write(struct shm_rbuff * rb,
return -EAGAIN;
if (shm_rbuff_empty(rb))
- pthread_cond_broadcast(rb->add);
+ was_empty = true;
- ohead = RB_HEAD;
-
- *head_el_ptr(rb) = (ssize_t) idx;
+ nhead = RB_HEAD;
do {
ohead = nhead;
@@ -250,6 +250,11 @@ int shm_rbuff_write(struct shm_rbuff * rb,
nhead = __sync_val_compare_and_swap(rb->head, ohead, nhead);
} while (nhead != ohead);
+ *(rb->shm_base + nhead) = (ssize_t) idx;
+
+ if (was_empty)
+ pthread_cond_broadcast(rb->add);
+
return 0;
}
@@ -263,7 +268,7 @@ ssize_t shm_rbuff_read(struct shm_rbuff * rb)
if (shm_rbuff_empty(rb))
return -EAGAIN;
- otail = RB_TAIL;
+ ntail = RB_TAIL;
do {
otail = ntail;
@@ -273,7 +278,7 @@ ssize_t shm_rbuff_read(struct shm_rbuff * rb)
pthread_cond_broadcast(rb->del);
- return *tail_el_ptr(rb);
+ return *(rb->shm_base + ntail);
}
ssize_t shm_rbuff_read_b(struct shm_rbuff * rb,
@@ -348,7 +353,7 @@ void shm_rbuff_fini(struct shm_rbuff * rb)
assert(__sync_fetch_and_add(rb->acl, 0) == RB_CLOSED);
- if (shm_rbuff_empty)
+ if (shm_rbuff_empty(rb))
return;
#ifdef __APPLE__