From 76bafc25c21297860c161b819ececca0d907872c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=A4rkl?= Date: Fri, 14 Jun 2019 10:42:16 +0200 Subject: [PATCH] Stub Discovery Thread --- lib/include/chiaki/congestioncontrol.h | 4 +-- lib/include/chiaki/discovery.h | 2 +- lib/src/congestioncontrol.c | 35 ++++++-------------------- lib/src/discovery.c | 22 +++++++++++++--- 4 files changed, 28 insertions(+), 35 deletions(-) diff --git a/lib/include/chiaki/congestioncontrol.h b/lib/include/chiaki/congestioncontrol.h index a7db7b4..ae41509 100644 --- a/lib/include/chiaki/congestioncontrol.h +++ b/lib/include/chiaki/congestioncontrol.h @@ -29,9 +29,7 @@ typedef struct chiaki_congestion_control_t { ChiakiTakion *takion; ChiakiThread thread; - ChiakiCond stop_cond; - ChiakiMutex stop_cond_mutex; - bool stop_cond_predicate; + ChiakiPredCond stop_cond; } ChiakiCongestionControl; CHIAKI_EXPORT ChiakiErrorCode chiaki_congestion_control_start(ChiakiCongestionControl *control, ChiakiTakion *takion); diff --git a/lib/include/chiaki/discovery.h b/lib/include/chiaki/discovery.h index 745c5df..63c8a69 100644 --- a/lib/include/chiaki/discovery.h +++ b/lib/include/chiaki/discovery.h @@ -60,7 +60,7 @@ typedef struct chiaki_discovery_thread_t ChiakiThread thread; } ChiakiDiscoveryThread; -CHIAKI_EXPORT ChiakiErrorCode chiaki_discovery_thread_start(ChiakiDiscoveryThread *thread); +CHIAKI_EXPORT ChiakiErrorCode chiaki_discovery_thread_start(ChiakiDiscoveryThread *thread, ChiakiDiscovery *discovery); CHIAKI_EXPORT ChiakiErrorCode chiaki_discovery_thread_stop(ChiakiDiscoveryThread *thread); #ifdef __cplusplus diff --git a/lib/src/congestioncontrol.c b/lib/src/congestioncontrol.c index 50a6a7c..3ddcc24 100644 --- a/lib/src/congestioncontrol.c +++ b/lib/src/congestioncontrol.c @@ -25,25 +25,24 @@ static void *congestion_control_thread_func(void *user) { ChiakiCongestionControl *control = user; - ChiakiErrorCode err = chiaki_mutex_lock(&control->stop_cond_mutex); + ChiakiErrorCode err = chiaki_pred_cond_lock(&control->stop_cond); if(err != CHIAKI_ERR_SUCCESS) return NULL; while(true) { - err = chiaki_cond_timedwait(&control->stop_cond, &control->stop_cond_mutex, CONGESTION_CONTROL_INTERVAL); + err = chiaki_pred_cond_timedwait(&control->stop_cond, CONGESTION_CONTROL_INTERVAL); if(err != CHIAKI_ERR_SUCCESS && err != CHIAKI_ERR_TIMEOUT) break; - if(control->stop_cond_predicate) + if(err != CHIAKI_ERR_TIMEOUT) break; - // TODO: detect non-stop and non-timeout (spurious) wakeup and wait the rest of the timeout CHIAKI_LOGD(control->takion->log, "Sending Congestion Control Packet\n"); ChiakiTakionCongestionPacket packet = { 0 }; // TODO: fill with real values chiaki_takion_send_congestion(control->takion, &packet); } - chiaki_mutex_unlock(&control->stop_cond_mutex); + chiaki_pred_cond_unlock(&control->stop_cond); return NULL; } @@ -51,22 +50,14 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_congestion_control_start(ChiakiCongestionCo { control->takion = takion; - control->stop_cond_predicate = false; - ChiakiErrorCode err = chiaki_cond_init(&control->stop_cond); + ChiakiErrorCode err = chiaki_pred_cond_init(&control->stop_cond); if(err != CHIAKI_ERR_SUCCESS) return err; - err = chiaki_mutex_init(&control->stop_cond_mutex); - if(err != CHIAKI_ERR_SUCCESS) - { - chiaki_cond_fini(&control->stop_cond); - return err; - } err = chiaki_thread_create(&control->thread, congestion_control_thread_func, control); if(err != CHIAKI_ERR_SUCCESS) { - chiaki_mutex_fini(&control->stop_cond_mutex); - chiaki_cond_fini(&control->stop_cond); + chiaki_pred_cond_fini(&control->stop_cond); return err; } @@ -75,14 +66,7 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_congestion_control_start(ChiakiCongestionCo CHIAKI_EXPORT ChiakiErrorCode chiaki_congestion_control_stop(ChiakiCongestionControl *control) { - ChiakiErrorCode err = chiaki_mutex_lock(&control->stop_cond_mutex); - if(err != CHIAKI_ERR_SUCCESS) - return err; - control->stop_cond_predicate = true; - err = chiaki_cond_signal(&control->stop_cond); - if(err != CHIAKI_ERR_SUCCESS) - return err; - err = chiaki_mutex_unlock(&control->stop_cond_mutex); + ChiakiErrorCode err = chiaki_pred_cond_signal(&control->stop_cond); if(err != CHIAKI_ERR_SUCCESS) return err; @@ -90,8 +74,5 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_congestion_control_stop(ChiakiCongestionCon if(err != CHIAKI_ERR_SUCCESS) return err; - chiaki_mutex_fini(&control->stop_cond_mutex); - chiaki_cond_fini(&control->stop_cond); - - return CHIAKI_ERR_SUCCESS; + return chiaki_pred_cond_fini(&control->stop_cond); } diff --git a/lib/src/discovery.c b/lib/src/discovery.c index f08250a..dde6846 100644 --- a/lib/src/discovery.c +++ b/lib/src/discovery.c @@ -98,15 +98,29 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_discovery_send(ChiakiDiscovery *discovery, return CHIAKI_ERR_SUCCESS; } +static void *discovery_thread_func(void *user); -CHIAKI_EXPORT ChiakiErrorCode chiaki_discovery_thread_start(ChiakiDiscoveryThread *thread) +CHIAKI_EXPORT ChiakiErrorCode chiaki_discovery_thread_start(ChiakiDiscoveryThread *thread, ChiakiDiscovery *discovery) { - // TODO - return CHIAKI_ERR_SUCCESS; + thread->discovery = discovery; + + // TODO: stop pipe + + return chiaki_thread_create(&thread->thread, discovery_thread_func, thread); } CHIAKI_EXPORT ChiakiErrorCode chiaki_discovery_thread_stop(ChiakiDiscoveryThread *thread) { // TODO - return CHIAKI_ERR_SUCCESS; + + return chiaki_thread_join(&thread->thread, NULL); +} + +static void *discovery_thread_func(void *user) +{ + ChiakiDiscoveryThread *thread = user; + + // TODO + + return NULL; } \ No newline at end of file