From 5432e665fd6eac776c5985d98058e5c4645ec0e4 Mon Sep 17 00:00:00 2001 From: Sander Vrijders Date: Thu, 11 Aug 2016 17:39:39 +0200 Subject: ipcpd: Add condition variable to IPCP state This adds a condition variable to the IPCP state, so that upon state changes any listeners to state changes can be notified. It also replaces the read/write lock with a mutex in order to be able to do so. --- src/ipcpd/normal/fmgr.c | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) (limited to 'src/ipcpd/normal/fmgr.c') diff --git a/src/ipcpd/normal/fmgr.c b/src/ipcpd/normal/fmgr.c index f9de16c4..70afff37 100644 --- a/src/ipcpd/normal/fmgr.c +++ b/src/ipcpd/normal/fmgr.c @@ -80,21 +80,22 @@ static void * fmgr_listen(void * o) int fd; char * ae_name; - /* FIXME: Avoid busy wait and react to pthread_cond_t */ - pthread_rwlock_rdlock(&_ipcp->state_lock); - while (!(_ipcp->state == IPCP_ENROLLED || - _ipcp->state == IPCP_SHUTDOWN)) { - pthread_rwlock_unlock(&_ipcp->state_lock); - sched_yield(); - pthread_rwlock_rdlock(&_ipcp->state_lock); - } + while (true) { + pthread_mutex_lock(&_ipcp->state_lock); + while (!(_ipcp->state == IPCP_ENROLLED || + _ipcp->state == IPCP_SHUTDOWN)) + pthread_cond_wait(&_ipcp->state_cond, + &_ipcp->state_lock); + + if (_ipcp->state == IPCP_SHUTDOWN) { + pthread_mutex_unlock(&_ipcp->state_lock); + return 0; + } + pthread_mutex_unlock(&_ipcp->state_lock); - while (_ipcp->state != IPCP_SHUTDOWN) { - pthread_rwlock_unlock(&_ipcp->state_lock); fd = flow_accept(&ae_name); if (fd < 0) { LOG_ERR("Failed to accept flow."); - pthread_rwlock_rdlock(&_ipcp->state_lock); continue; } @@ -103,14 +104,12 @@ static void * fmgr_listen(void * o) if (flow_alloc_resp(fd, -1)) LOG_ERR("Failed to reply to flow allocation."); flow_dealloc(fd); - pthread_rwlock_rdlock(&_ipcp->state_lock); continue; } if (flow_alloc_resp(fd, 0)) { LOG_ERR("Failed to reply to flow allocation."); flow_dealloc(fd); - pthread_rwlock_rdlock(&_ipcp->state_lock); continue; } @@ -121,7 +120,6 @@ static void * fmgr_listen(void * o) if (ribmgr_add_flow(fd)) { LOG_ERR("Failed to hand fd to RIB."); flow_dealloc(fd); - pthread_rwlock_rdlock(&_ipcp->state_lock); continue; } } @@ -130,7 +128,6 @@ static void * fmgr_listen(void * o) if (frct_dt_flow(fd)) { LOG_ERR("Failed to hand fd to FRCT."); flow_dealloc(fd); - pthread_rwlock_rdlock(&_ipcp->state_lock); continue; } } @@ -138,11 +135,8 @@ static void * fmgr_listen(void * o) if (add_n_1_fd(fd, ae_name)) { LOG_ERR("Failed to add file descriptor to list."); flow_dealloc(fd); - pthread_rwlock_rdlock(&_ipcp->state_lock); continue; } - - pthread_rwlock_rdlock(&_ipcp->state_lock); } return (void *) 0; -- cgit v1.2.3