From 04eb03136466a18d81511e7ccadf51c08faa8edb Mon Sep 17 00:00:00 2001 From: dimitri staessens Date: Tue, 30 Aug 2016 14:29:08 +0200 Subject: lib, ipcp: Compile on Apple junk Disables robust mutexes and clock attributes for condition variables for compatibility with OSX (SUSv2). Implements clock_gettime and adds some defines for OSX compatibility in time_utils. --- cmake/FindProtobufC.cmake | 2 +- include/ouroboros/time_utils.h | 22 +++++++++++++++ src/ipcpd/CMakeLists.txt | 4 ++- src/ipcpd/ipcp.c | 2 ++ src/ipcpd/normal/cdap_request.c | 2 ++ src/lib/CMakeLists.txt | 10 ++++--- src/lib/shm_ap_rbuff.c | 59 ++++++++++++++++++++++++++++------------- src/lib/shm_rdrbuff.c | 57 ++++++++++++++++++++++++++++++--------- src/lib/time_utils.c | 12 +++++++++ 9 files changed, 134 insertions(+), 36 deletions(-) diff --git a/cmake/FindProtobufC.cmake b/cmake/FindProtobufC.cmake index 8c1f23db..6b81d751 100644 --- a/cmake/FindProtobufC.cmake +++ b/cmake/FindProtobufC.cmake @@ -50,7 +50,7 @@ endif() # Find library find_library(PROTOBUF_C_LIBRARY - NAMES libprotobuf-c.so libprotobuf-c + NAMES libprotobuf-c.so libprotobuf-c libprotobuf-c.dylib ) mark_as_advanced(PROTOBUF_C_LIBRARY) diff --git a/include/ouroboros/time_utils.h b/include/ouroboros/time_utils.h index e097a01f..0b65a2b4 100644 --- a/include/ouroboros/time_utils.h +++ b/include/ouroboros/time_utils.h @@ -78,4 +78,26 @@ int tv_to_ts(const struct timeval * src, int ts_to_tv(const struct timespec * src, struct timeval * dst); +#ifdef __APPLE__ /* morons */ + +/* taken from time.h */ +#define CLOCK_REALTIME 0 +#define CLOCK_MONOTONIC 1 +#define CLOCK_PROCESS_CPUTIME_ID 2 +#define CLOCK_THREAD_CPUTIME_ID 3 +#define CLOCK_MONOTONIC_RAW 4 +#define CLOCK_REALTIME_COARSE 5 +#define CLOCK_MONOTONIC_COARSE 6 +#define CLOCK_BOOTTIME 7 +#define CLOCK_REALTIME_ALARM 8 +#define CLOCK_BOOTTIME_ALARM 9 +#define CLOCK_SGI_CYCLE 10 /* Hardware specific */ +#define CLOCK_TAI 11 + +#define CLOCKS_MASK (CLOCK_REALTIME | CLOCK_MONOTONIC) +#define CLOCKS_MONO CLOCK_MONOTONIC + +int clock_gettime(int clock, struct timespec * t); +#endif + #endif /* OUROBOROS_TIME_UTILS_H */ diff --git a/src/ipcpd/CMakeLists.txt b/src/ipcpd/CMakeLists.txt index bb482ed9..43af7a25 100644 --- a/src/ipcpd/CMakeLists.txt +++ b/src/ipcpd/CMakeLists.txt @@ -7,4 +7,6 @@ set(IPCP_SOURCES add_subdirectory(local) add_subdirectory(normal) add_subdirectory(shim-udp) -add_subdirectory(shim-eth-llc) +if(NOT APPLE) + add_subdirectory(shim-eth-llc) +endif() diff --git a/src/ipcpd/ipcp.c b/src/ipcpd/ipcp.c index 87bf9e51..12111a51 100644 --- a/src/ipcpd/ipcp.c +++ b/src/ipcpd/ipcp.c @@ -48,7 +48,9 @@ struct ipcp * ipcp_instance_create() pthread_rwlock_init(&i->state_lock, NULL); pthread_mutex_init(&i->state_mtx, NULL); pthread_condattr_init(&cattr); +#ifndef __APPLE__ pthread_condattr_setclock(&cattr, PTHREAD_COND_CLOCK); +#endif pthread_cond_init(&i->state_cond, &cattr); return i; diff --git a/src/ipcpd/normal/cdap_request.c b/src/ipcpd/normal/cdap_request.c index cdbbb833..5839360b 100644 --- a/src/ipcpd/normal/cdap_request.c +++ b/src/ipcpd/normal/cdap_request.c @@ -48,7 +48,9 @@ struct cdap_request * cdap_request_create(enum cdap_opcode code, creq->result = -1; pthread_condattr_init(&cattr); +#ifndef __APPLE__ pthread_condattr_setclock(&cattr, PTHREAD_COND_CLOCK); +#endif pthread_cond_init(&creq->cond, &cattr); pthread_mutex_init(&creq->lock, NULL); 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 -- cgit v1.2.3