summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authordimitri staessens <dimitri.staessens@ugent.be>2017-03-30 08:06:52 +0000
committerSander Vrijders <sander.vrijders@ugent.be>2017-03-30 08:06:52 +0000
commitb2610a681dde939db5eabd22ccdb71aecf76a825 (patch)
treedda4c54850fbe23bd859f186dfe33787526aa6b9 /src/lib
parenta167185b7fe6a459a46cb71214caf37fb0c21fa8 (diff)
parent0a9c02d4aa581a397d17b96199eca14c8b137371 (diff)
downloadouroboros-b2610a681dde939db5eabd22ccdb71aecf76a825.tar.gz
ouroboros-b2610a681dde939db5eabd22ccdb71aecf76a825.zip
Merged in dstaesse/ouroboros/be-less-locks (pull request #435)
lib: Reduce locking overhead in rdrbuff
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/shm_rdrbuff.c65
1 files changed, 20 insertions, 45 deletions
diff --git a/src/lib/shm_rdrbuff.c b/src/lib/shm_rdrbuff.c
index 0976c4bd..b8d73650 100644
--- a/src/lib/shm_rdrbuff.c
+++ b/src/lib/shm_rdrbuff.c
@@ -376,9 +376,17 @@ ssize_t shm_rdrbuff_write(struct shm_rdrbuff * rdrb,
*rdrb->head = 0;
}
#endif
- sdb = get_head_ptr(rdrb);
+ sdb = get_head_ptr(rdrb);
+ sdb->flags = SDB_VALID;
+ sdb->idx = *rdrb->head;
+#ifdef SHM_RDRB_MULTI_BLOCK
+ *rdrb->head = (*rdrb->head + blocks) & ((SHM_BUFFER_SIZE) - 1);
+#else
+ *rdrb->head = (*rdrb->head + 1) & ((SHM_BUFFER_SIZE) - 1);
+#endif
+ pthread_mutex_unlock(rdrb->lock);
+
sdb->size = size;
- sdb->flags = SDB_VALID;
sdb->du_head = headspace;
sdb->du_tail = sdb->du_head + len;
#ifdef SHM_RDRB_MULTI_BLOCK
@@ -386,14 +394,6 @@ ssize_t shm_rdrbuff_write(struct shm_rdrbuff * rdrb,
#endif
memcpy(((uint8_t *) (sdb + 1)) + headspace, data, len);
- sdb->idx = *rdrb->head;
-#ifdef SHM_RDRB_MULTI_BLOCK
- *rdrb->head = (*rdrb->head + blocks) & ((SHM_BUFFER_SIZE) - 1);
-#else
- *rdrb->head = (*rdrb->head + 1) & ((SHM_BUFFER_SIZE) - 1);
-#endif
- pthread_mutex_unlock(rdrb->lock);
-
return sdb->idx;
}
@@ -457,9 +457,17 @@ ssize_t shm_rdrbuff_write_b(struct shm_rdrbuff * rdrb,
*rdrb->head = 0;
}
#endif
- sdb = get_head_ptr(rdrb);
+ sdb = get_head_ptr(rdrb);
+ sdb->flags = SDB_VALID;
+ sdb->idx = *rdrb->head;
+#ifdef SHM_RDRB_MULTI_BLOCK
+ *rdrb->head = (*rdrb->head + blocks) & ((SHM_BUFFER_SIZE) - 1);
+#else
+ *rdrb->head = (*rdrb->head + 1) & ((SHM_BUFFER_SIZE) - 1);
+#endif
+ pthread_cleanup_pop(true);
+
sdb->size = size;
- sdb->flags = SDB_VALID;
sdb->du_head = headspace;
sdb->du_tail = sdb->du_head + len;
#ifdef SHM_RDRB_MULTI_BLOCK
@@ -467,13 +475,6 @@ ssize_t shm_rdrbuff_write_b(struct shm_rdrbuff * rdrb,
#endif
memcpy(((uint8_t *) (sdb + 1)) + headspace, data, len);
- sdb->idx = *rdrb->head;
-#ifdef SHM_RDRB_MULTI_BLOCK
- *rdrb->head = (*rdrb->head + blocks) & ((SHM_BUFFER_SIZE) - 1);
-#else
- *rdrb->head = (*rdrb->head + 1) & ((SHM_BUFFER_SIZE) - 1);
-#endif
- pthread_cleanup_pop(true);
return sdb->idx;
}
@@ -489,23 +490,10 @@ ssize_t shm_rdrbuff_read(uint8_t ** dst,
assert(rdrb);
assert(idx < (SHM_BUFFER_SIZE));
-#ifdef __APPLE__
- pthread_mutex_lock(rdrb->lock);
-#else
- if (pthread_mutex_lock(rdrb->lock) == EOWNERDEAD)
- pthread_mutex_consistent(rdrb->lock);
-#endif
- if (shm_rdrb_empty(rdrb)) {
- pthread_mutex_unlock(rdrb->lock);
- return -1;
- }
-
sdb = idx_to_du_buff_ptr(rdrb, idx);
len = (ssize_t) (sdb->du_tail - sdb->du_head);
*dst = ((uint8_t *) (sdb + 1)) + sdb->du_head;
- pthread_mutex_unlock(rdrb->lock);
-
return len;
}
@@ -516,21 +504,8 @@ struct shm_du_buff * shm_rdrbuff_get(struct shm_rdrbuff * rdrb, size_t idx)
assert(rdrb);
assert(idx < (SHM_BUFFER_SIZE));
-#ifdef __APPLE__
- pthread_mutex_lock(rdrb->lock);
-#else
- if (pthread_mutex_lock(rdrb->lock) == EOWNERDEAD)
- pthread_mutex_consistent(rdrb->lock);
-#endif
- if (shm_rdrb_empty(rdrb)) {
- pthread_mutex_unlock(rdrb->lock);
- return NULL;
- }
-
sdb = idx_to_du_buff_ptr(rdrb, idx);
- pthread_mutex_unlock(rdrb->lock);
-
return sdb;
}