summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authordimitri staessens <dimitri.staessens@intec.ugent.be>2016-08-31 14:29:32 +0200
committerdimitri staessens <dimitri.staessens@intec.ugent.be>2016-08-31 14:29:32 +0200
commit857c014a40a75eda574af8b2002b1c794bc0975c (patch)
tree721c6c6a7b394dc758ffa12bb3a1261d9817207f /src/lib
parent1071824054828b9be36f50416f858c49c9acd701 (diff)
parent63b63e0f6ab9f743fc34623bb1caa9852227f046 (diff)
downloadouroboros-857c014a40a75eda574af8b2002b1c794bc0975c.tar.gz
ouroboros-857c014a40a75eda574af8b2002b1c794bc0975c.zip
Merge branch 'be' of bitbucket.org:ouroboros-rina/ouroboros into be
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/CMakeLists.txt10
-rw-r--r--src/lib/shm_ap_rbuff.c59
-rw-r--r--src/lib/shm_rdrbuff.c57
-rw-r--r--src/lib/time_utils.c12
4 files changed, 104 insertions, 34 deletions
diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt
index 8c058dd8..14e7051a 100644
--- a/src/lib/CMakeLists.txt
+++ b/src/lib/CMakeLists.txt
@@ -11,9 +11,13 @@ protobuf_generate_c(DIF_CONFIG_PROTO_SRCS DIF_CONFIG_PROTO_HDRS
protobuf_generate_c(CDAP_PROTO_SRCS CDAP_PROTO_HDRS
cdap.proto)
-find_library(LIBRT_LIBRARIES rt)
-if(NOT LIBRT_LIBRARIES)
- message(FATAL_ERROR "librt not found")
+if(NOT APPLE)
+ find_library(LIBRT_LIBRARIES rt)
+ if(NOT LIBRT_LIBRARIES)
+ message(FATAL_ERROR "librt not found")
+ endif()
+else()
+ set(LIBRT_LIBRARIES "")
endif()
find_library(LIBPTHREAD_LIBRARIES pthread)
diff --git a/src/lib/shm_ap_rbuff.c b/src/lib/shm_ap_rbuff.c
index f21b1e86..77e288a8 100644
--- a/src/lib/shm_ap_rbuff.c
+++ b/src/lib/shm_ap_rbuff.c
@@ -131,13 +131,17 @@ struct shm_ap_rbuff * shm_ap_rbuff_create()
rb->del = rb->add + 1;
pthread_mutexattr_init(&mattr);
+#ifndef __APPLE__
pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST);
+#endif
pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
pthread_mutex_init(rb->lock, &mattr);
pthread_condattr_init(&cattr);
pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_SHARED);
+#ifndef __APPLE__
pthread_condattr_setclock(&cattr, PTHREAD_COND_CLOCK);
+#endif
pthread_cond_init(rb->add, &cattr);
pthread_cond_init(rb->del, &cattr);
@@ -264,11 +268,14 @@ int shm_ap_rbuff_write(struct shm_ap_rbuff * rb, struct rb_entry * e)
if (rb == NULL || e == NULL)
return -1;
+#ifdef __APPLE__
+ pthread_mutex_lock(rb->lock);
+#else
if (pthread_mutex_lock(rb->lock) == EOWNERDEAD) {
LOG_DBG("Recovering dead mutex.");
pthread_mutex_consistent(rb->lock);
}
-
+#endif
if (!shm_rbuff_free(rb)) {
pthread_mutex_unlock(rb->lock);
return -1;
@@ -291,12 +298,14 @@ int shm_ap_rbuff_peek_idx(struct shm_ap_rbuff * rb)
if (rb == NULL)
return -EINVAL;
-
+#ifdef __APPLE__
+ pthread_mutex_lock(rb->lock);
+#else
if (pthread_mutex_lock(rb->lock) == EOWNERDEAD) {
LOG_DBG("Recovering dead mutex.");
pthread_mutex_consistent(rb->lock);
}
-
+#endif
if (shm_rbuff_empty(rb)) {
pthread_mutex_unlock(rb->lock);
return -1;
@@ -325,12 +334,14 @@ int shm_ap_rbuff_peek_b(struct shm_ap_rbuff * rb,
pthread_cleanup_push((void(*)(void *))pthread_mutex_unlock,
(void *) rb->lock);
-
+#ifdef __APPLE__
+ pthread_mutex_lock(rb->lock);
+#else
if (pthread_mutex_lock(rb->lock) == EOWNERDEAD) {
LOG_DBG("Recovering dead mutex.");
pthread_mutex_consistent(rb->lock);
}
-
+#endif
while (shm_rbuff_empty(rb)) {
if (timeout != NULL)
ret = pthread_cond_timedwait(rb->add,
@@ -338,12 +349,12 @@ int shm_ap_rbuff_peek_b(struct shm_ap_rbuff * rb,
&abstime);
else
ret = pthread_cond_wait(rb->add, rb->lock);
-
+#ifndef __APPLE__
if (ret == EOWNERDEAD) {
LOG_DBG("Recovering dead mutex.");
pthread_mutex_consistent(rb->lock);
}
-
+#endif
if (ret == ETIMEDOUT)
break;
}
@@ -368,17 +379,23 @@ struct rb_entry * shm_ap_rbuff_read(struct shm_ap_rbuff * rb)
pthread_cleanup_push((void(*)(void *))pthread_mutex_unlock,
(void *) rb->lock);
+#ifdef __APPLE__
+ pthread_mutex_lock(rb->lock);
+#else
if (pthread_mutex_lock(rb->lock) == EOWNERDEAD) {
LOG_DBG("Recovering dead mutex.");
pthread_mutex_consistent(rb->lock);
}
-
+#endif
while (shm_rbuff_empty(rb))
+#ifdef __APPLE__
+ pthread_cond_wait(rb->add, rb->lock);
+#else
if (pthread_cond_wait(rb->add, rb->lock) == EOWNERDEAD) {
- LOG_DBG("Recovering dead mutex.");
- pthread_mutex_consistent(rb->lock);
- }
-
+ LOG_DBG("Recovering dead mutex.");
+ pthread_mutex_consistent(rb->lock);
+ }
+#endif
e = malloc(sizeof(*e));
if (e != NULL) {
*e = *(rb->shm_base + *rb->ptr_tail);
@@ -394,11 +411,14 @@ ssize_t shm_ap_rbuff_read_port(struct shm_ap_rbuff * rb, int port_id)
{
ssize_t idx = -1;
+#ifdef __APPLE__
+ pthread_mutex_lock(rb->lock);
+#else
if (pthread_mutex_lock(rb->lock) == EOWNERDEAD) {
LOG_DBG("Recovering dead mutex.");
pthread_mutex_consistent(rb->lock);
}
-
+#endif
if (shm_rbuff_empty(rb) || tail_el_ptr(rb)->port_id != port_id) {
pthread_mutex_unlock(rb->lock);
return -1;
@@ -422,11 +442,14 @@ ssize_t shm_ap_rbuff_read_port_b(struct shm_ap_rbuff * rb,
int ret = 0;
ssize_t idx = -1;
+#ifdef __APPLE__
+ pthread_mutex_lock(rb->lock);
+#else
if (pthread_mutex_lock(rb->lock) == EOWNERDEAD) {
LOG_DBG("Recovering dead mutex.");
pthread_mutex_consistent(rb->lock);
}
-
+#endif
if (timeout != NULL) {
clock_gettime(PTHREAD_COND_CLOCK, &abstime);
ts_add(&abstime, timeout, &abstime);
@@ -444,12 +467,12 @@ ssize_t shm_ap_rbuff_read_port_b(struct shm_ap_rbuff * rb,
&abstime);
else
ret = pthread_cond_wait(rb->add, rb->lock);
-
+#ifndef __APPLE__
if (ret == EOWNERDEAD) {
LOG_DBG("Recovering dead mutex.");
pthread_mutex_consistent(rb->lock);
}
-
+#endif
if (ret == ETIMEDOUT)
break;
}
@@ -461,12 +484,12 @@ ssize_t shm_ap_rbuff_read_port_b(struct shm_ap_rbuff * rb,
&abstime);
else
ret = pthread_cond_wait(rb->del, rb->lock);
-
+#ifndef __APPLE__
if (ret == EOWNERDEAD) {
LOG_DBG("Recovering dead mutex.");
pthread_mutex_consistent(rb->lock);
}
-
+#endif
if (ret == ETIMEDOUT)
break;
}
diff --git a/src/lib/shm_rdrbuff.c b/src/lib/shm_rdrbuff.c
index d42dbea7..87fe2d97 100644
--- a/src/lib/shm_rdrbuff.c
+++ b/src/lib/shm_rdrbuff.c
@@ -228,7 +228,9 @@ struct shm_rdrbuff * shm_rdrbuff_create()
pthread_mutexattr_init(&mattr);
pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
+#ifndef __APPLE__
pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST);
+#endif
pthread_mutex_init(rdrb->lock, &mattr);
pthread_condattr_init(&cattr);
@@ -325,11 +327,14 @@ void * shm_rdrbuff_sanitize(void * o)
if (rdrb == NULL)
return (void *) -1;
-
+#ifdef __APPLE__
+ pthread_mutex_lock(rdrb->lock);
+#else
if (pthread_mutex_lock(rdrb->lock) == EOWNERDEAD) {
LOG_WARN("Recovering dead mutex.");
pthread_mutex_consistent(rdrb->lock);
}
+#endif
pthread_cleanup_push((void (*)(void *)) pthread_mutex_unlock,
(void *) rdrb->lock);
@@ -338,12 +343,14 @@ void * shm_rdrbuff_sanitize(void * o)
int ret = 0;
struct timespec now;
struct timespec dl;
-
+#ifdef __APPLE__
+ pthread_cond_wait(rdrb->full, rdrb->lock);
+#else
if (pthread_cond_wait(rdrb->full, rdrb->lock) == EOWNERDEAD) {
LOG_WARN("Recovering dead mutex.");
pthread_mutex_consistent(rdrb->lock);
}
-
+#endif
*rdrb->choked = 1;
garbage_collect(rdrb);
@@ -367,12 +374,12 @@ void * shm_rdrbuff_sanitize(void * o)
&dl);
if (!ret)
continue;
-
+#ifndef __APPLE__
if (ret == EOWNERDEAD) {
LOG_WARN("Recovering dead mutex.");
pthread_mutex_consistent(rdrb->lock);
}
-
+#endif
if (ret == ETIMEDOUT) {
LOG_DBGF("SDU timed out (dst: %d).", api);
clean_sdus(rdrb, api);
@@ -462,10 +469,14 @@ ssize_t shm_rdrbuff_write(struct shm_rdrbuff * rdrb,
return -1;
}
#endif
+#ifdef __APPLE__
+ pthread_mutex_lock(rdrb->lock);
+#else
if (pthread_mutex_lock(rdrb->lock) == EOWNERDEAD) {
LOG_DBGF("Recovering dead mutex.");
pthread_mutex_consistent(rdrb->lock);
}
+#endif
#ifdef SHM_RDRBUFF_MULTI_BLOCK
while (sz > 0) {
sz -= SHM_RDRB_BLOCK_SIZE;
@@ -547,11 +558,14 @@ ssize_t shm_rdrbuff_write_b(struct shm_rdrbuff * rdrb,
return -1;
}
#endif
+#ifdef __APPLE__
+ pthread_mutex_lock(rdrb->lock);
+#else
if (pthread_mutex_lock(rdrb->lock) == EOWNERDEAD) {
LOG_DBGF("Recovering dead mutex.");
pthread_mutex_consistent(rdrb->lock);
}
-
+#endif
pthread_cleanup_push((void(*)(void *))pthread_mutex_unlock,
(void *) rdrb->lock);
@@ -616,12 +630,14 @@ int shm_rdrbuff_read(uint8_t ** dst,
if (idx > SHM_BUFFER_SIZE)
return -1;
-
+#ifdef __APPLE__
+ pthread_mutex_lock(rdrb->lock);
+#else
if (pthread_mutex_lock(rdrb->lock) == EOWNERDEAD) {
LOG_DBGF("Recovering dead mutex.");
pthread_mutex_consistent(rdrb->lock);
}
-
+#endif
if (shm_rdrb_empty(rdrb)) {
pthread_mutex_unlock(rdrb->lock);
return -1;
@@ -640,12 +656,14 @@ int shm_rdrbuff_remove(struct shm_rdrbuff * rdrb, ssize_t idx)
{
if (idx > SHM_BUFFER_SIZE)
return -1;
-
+#ifdef __APPLE__
+ pthread_mutex_lock(rdrb->lock);
+#else
if (pthread_mutex_lock(rdrb->lock) == EOWNERDEAD) {
LOG_DBGF("Recovering dead mutex.");
pthread_mutex_consistent(rdrb->lock);
}
-
+#endif
if (shm_rdrb_empty(rdrb)) {
pthread_mutex_unlock(rdrb->lock);
return -1;
@@ -682,11 +700,14 @@ uint8_t * shm_du_buff_head_alloc(struct shm_rdrbuff * rdrb,
if (idx < 0 || idx > SHM_BUFFER_SIZE)
return NULL;
+#ifdef __APPLE__
+ pthread_mutex_lock(rdrb->lock);
+#else
if (pthread_mutex_lock(rdrb->lock) == EOWNERDEAD) {
LOG_DBGF("Recovering dead mutex.");
pthread_mutex_consistent(rdrb->lock);
}
-
+#endif
sdb = idx_to_du_buff_ptr(rdrb, idx);
if ((long) (sdb->du_head - size) < 0) {
@@ -717,11 +738,14 @@ uint8_t * shm_du_buff_tail_alloc(struct shm_rdrbuff * rdrb,
if (idx < 0 || idx > SHM_BUFFER_SIZE)
return NULL;
+#ifdef __APPLE__
+ pthread_mutex_lock(rdrb->lock);
+#else
if (pthread_mutex_lock(rdrb->lock) == EOWNERDEAD) {
LOG_DBGF("Recovering dead mutex.");
pthread_mutex_consistent(rdrb->lock);
}
-
+#endif
sdb = idx_to_du_buff_ptr(rdrb, idx);
if (sdb->du_tail + size >= sdb->size) {
@@ -751,10 +775,14 @@ int shm_du_buff_head_release(struct shm_rdrbuff * rdrb,
if (idx < 0 || idx > SHM_BUFFER_SIZE)
return -1;
+#ifdef __APPLE__
+ pthread_mutex_lock(rdrb->lock);
+#else
if (pthread_mutex_lock(rdrb->lock) == EOWNERDEAD) {
LOG_DBGF("Recovering dead mutex.");
pthread_mutex_consistent(rdrb->lock);
}
+#endif
sdb = idx_to_du_buff_ptr(rdrb, idx);
@@ -783,11 +811,14 @@ int shm_du_buff_tail_release(struct shm_rdrbuff * rdrb,
if (idx < 0 || idx > SHM_BUFFER_SIZE)
return -1;
+#ifdef __APPLE__
+ pthread_mutex_lock(rdrb->lock);
+#else
if (pthread_mutex_lock(rdrb->lock) == EOWNERDEAD) {
LOG_DBGF("Recovering dead mutex.");
pthread_mutex_consistent(rdrb->lock);
}
-
+#endif
sdb = idx_to_du_buff_ptr(rdrb, idx);
if (size > sdb->du_tail - sdb->du_head) {
diff --git a/src/lib/time_utils.c b/src/lib/time_utils.c
index d0b312da..b34e4763 100644
--- a/src/lib/time_utils.c
+++ b/src/lib/time_utils.c
@@ -140,3 +140,15 @@ int ts_to_tv(const struct timespec * src,
return 0;
}
+
+#ifdef __APPLE__
+int clock_gettime(int clock, struct timespec * t)
+{
+ struct timeval tv;
+ int ret = gettimeofday(&tv, NULL);
+ t->tv_sec = tv.tv_sec;
+ t->tv_nsec = tv.tv_usec * 1000;
+ (void) clock;
+ return ret;
+}
+#endif