Refactor ChiakiPredCond to ChiakiBoolPredCond

This commit is contained in:
Florian Märkl 2019-06-25 12:40:31 +02:00
commit c23b3b9bf9
No known key found for this signature in database
GPG key ID: 125BC8A5A6A1E857
6 changed files with 80 additions and 59 deletions

View file

@ -29,7 +29,7 @@ typedef struct chiaki_congestion_control_t
{ {
ChiakiTakion *takion; ChiakiTakion *takion;
ChiakiThread thread; ChiakiThread thread;
ChiakiPredCond stop_cond; ChiakiBoolPredCond stop_cond;
} ChiakiCongestionControl; } ChiakiCongestionControl;
CHIAKI_EXPORT ChiakiErrorCode chiaki_congestion_control_start(ChiakiCongestionControl *control, ChiakiTakion *takion); CHIAKI_EXPORT ChiakiErrorCode chiaki_congestion_control_start(ChiakiCongestionControl *control, ChiakiTakion *takion);

View file

@ -39,7 +39,7 @@ typedef struct chiaki_stream_connection_t
uint8_t *ecdh_secret; uint8_t *ecdh_secret;
ChiakiGKCrypt *gkcrypt_local; ChiakiGKCrypt *gkcrypt_local;
ChiakiGKCrypt *gkcrypt_remote; ChiakiGKCrypt *gkcrypt_remote;
ChiakiPredCond stop_cond; ChiakiBoolPredCond stop_cond;
} ChiakiStreamConnection; } ChiakiStreamConnection;
CHIAKI_EXPORT ChiakiErrorCode chiaki_stream_connection_run(struct chiaki_session_t *session); CHIAKI_EXPORT ChiakiErrorCode chiaki_stream_connection_run(struct chiaki_session_t *session);

View file

@ -56,30 +56,33 @@ typedef struct chiaki_cond_t
pthread_cond_t cond; pthread_cond_t cond;
} ChiakiCond; } ChiakiCond;
typedef bool (*ChiakiCheckPred)(void *);
CHIAKI_EXPORT ChiakiErrorCode chiaki_cond_init(ChiakiCond *cond); CHIAKI_EXPORT ChiakiErrorCode chiaki_cond_init(ChiakiCond *cond);
CHIAKI_EXPORT ChiakiErrorCode chiaki_cond_fini(ChiakiCond *cond); CHIAKI_EXPORT ChiakiErrorCode chiaki_cond_fini(ChiakiCond *cond);
CHIAKI_EXPORT ChiakiErrorCode chiaki_cond_wait(ChiakiCond *cond, ChiakiMutex *mutex); CHIAKI_EXPORT ChiakiErrorCode chiaki_cond_wait(ChiakiCond *cond, ChiakiMutex *mutex);
CHIAKI_EXPORT ChiakiErrorCode chiaki_cond_timedwait(ChiakiCond *cond, ChiakiMutex *mutex, uint64_t timeout_ms); CHIAKI_EXPORT ChiakiErrorCode chiaki_cond_timedwait(ChiakiCond *cond, ChiakiMutex *mutex, uint64_t timeout_ms);
CHIAKI_EXPORT ChiakiErrorCode chiaki_cond_wait_pred(ChiakiCond *cond, ChiakiMutex *mutex, ChiakiCheckPred check_pred, void *check_pred_user);
CHIAKI_EXPORT ChiakiErrorCode chiaki_cond_timedwait_pred(ChiakiCond *cond, ChiakiMutex *mutex, uint64_t timeout_ms, ChiakiCheckPred check_pred, void *check_pred_user);
CHIAKI_EXPORT ChiakiErrorCode chiaki_cond_signal(ChiakiCond *cond); CHIAKI_EXPORT ChiakiErrorCode chiaki_cond_signal(ChiakiCond *cond);
CHIAKI_EXPORT ChiakiErrorCode chiaki_cond_broadcast(ChiakiCond *cond); CHIAKI_EXPORT ChiakiErrorCode chiaki_cond_broadcast(ChiakiCond *cond);
typedef struct chiaki_pred_cond_t typedef struct chiaki_bool_pred_cond_t
{ {
ChiakiCond cond; ChiakiCond cond;
ChiakiMutex mutex; ChiakiMutex mutex;
bool pred; bool pred;
} ChiakiPredCond; } ChiakiBoolPredCond;
CHIAKI_EXPORT ChiakiErrorCode chiaki_pred_cond_init(ChiakiPredCond *cond); CHIAKI_EXPORT ChiakiErrorCode chiaki_bool_pred_cond_init(ChiakiBoolPredCond *cond);
CHIAKI_EXPORT ChiakiErrorCode chiaki_pred_cond_fini(ChiakiPredCond *cond); CHIAKI_EXPORT ChiakiErrorCode chiaki_bool_pred_cond_fini(ChiakiBoolPredCond *cond);
CHIAKI_EXPORT ChiakiErrorCode chiaki_pred_cond_lock(ChiakiPredCond *cond); CHIAKI_EXPORT ChiakiErrorCode chiaki_bool_pred_cond_lock(ChiakiBoolPredCond *cond);
CHIAKI_EXPORT ChiakiErrorCode chiaki_pred_cond_unlock(ChiakiPredCond *cond); CHIAKI_EXPORT ChiakiErrorCode chiaki_bool_pred_cond_unlock(ChiakiBoolPredCond *cond);
CHIAKI_EXPORT ChiakiErrorCode chiaki_pred_cond_wait(ChiakiPredCond *cond); CHIAKI_EXPORT ChiakiErrorCode chiaki_bool_pred_cond_wait(ChiakiBoolPredCond *cond);
CHIAKI_EXPORT ChiakiErrorCode chiaki_pred_cond_timedwait(ChiakiPredCond *cond, uint64_t timeout_ms); CHIAKI_EXPORT ChiakiErrorCode chiaki_bool_pred_cond_timedwait(ChiakiBoolPredCond *cond, uint64_t timeout_ms);
CHIAKI_EXPORT void chiaki_pred_cond_reset(ChiakiPredCond *cond); CHIAKI_EXPORT ChiakiErrorCode chiaki_bool_pred_cond_signal(ChiakiBoolPredCond *cond);
CHIAKI_EXPORT ChiakiErrorCode chiaki_pred_cond_signal(ChiakiPredCond *cond); CHIAKI_EXPORT ChiakiErrorCode chiaki_bool_pred_cond_broadcast(ChiakiBoolPredCond *cond);
CHIAKI_EXPORT ChiakiErrorCode chiaki_pred_cond_broadcast(ChiakiPredCond *cond);
#ifdef __cplusplus #ifdef __cplusplus
} }

View file

@ -25,13 +25,13 @@ static void *congestion_control_thread_func(void *user)
{ {
ChiakiCongestionControl *control = user; ChiakiCongestionControl *control = user;
ChiakiErrorCode err = chiaki_pred_cond_lock(&control->stop_cond); ChiakiErrorCode err = chiaki_bool_pred_cond_lock(&control->stop_cond);
if(err != CHIAKI_ERR_SUCCESS) if(err != CHIAKI_ERR_SUCCESS)
return NULL; return NULL;
while(true) while(true)
{ {
err = chiaki_pred_cond_timedwait(&control->stop_cond, CONGESTION_CONTROL_INTERVAL_MS); err = chiaki_bool_pred_cond_timedwait(&control->stop_cond, CONGESTION_CONTROL_INTERVAL_MS);
if(err != CHIAKI_ERR_TIMEOUT) if(err != CHIAKI_ERR_TIMEOUT)
break; break;
@ -40,7 +40,7 @@ static void *congestion_control_thread_func(void *user)
chiaki_takion_send_congestion(control->takion, &packet); chiaki_takion_send_congestion(control->takion, &packet);
} }
chiaki_pred_cond_unlock(&control->stop_cond); chiaki_bool_pred_cond_unlock(&control->stop_cond);
return NULL; return NULL;
} }
@ -48,14 +48,14 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_congestion_control_start(ChiakiCongestionCo
{ {
control->takion = takion; control->takion = takion;
ChiakiErrorCode err = chiaki_pred_cond_init(&control->stop_cond); ChiakiErrorCode err = chiaki_bool_pred_cond_init(&control->stop_cond);
if(err != CHIAKI_ERR_SUCCESS) if(err != CHIAKI_ERR_SUCCESS)
return err; return err;
err = chiaki_thread_create(&control->thread, congestion_control_thread_func, control); err = chiaki_thread_create(&control->thread, congestion_control_thread_func, control);
if(err != CHIAKI_ERR_SUCCESS) if(err != CHIAKI_ERR_SUCCESS)
{ {
chiaki_pred_cond_fini(&control->stop_cond); chiaki_bool_pred_cond_fini(&control->stop_cond);
return err; return err;
} }
@ -64,7 +64,7 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_congestion_control_start(ChiakiCongestionCo
CHIAKI_EXPORT ChiakiErrorCode chiaki_congestion_control_stop(ChiakiCongestionControl *control) CHIAKI_EXPORT ChiakiErrorCode chiaki_congestion_control_stop(ChiakiCongestionControl *control)
{ {
ChiakiErrorCode err = chiaki_pred_cond_signal(&control->stop_cond); ChiakiErrorCode err = chiaki_bool_pred_cond_signal(&control->stop_cond);
if(err != CHIAKI_ERR_SUCCESS) if(err != CHIAKI_ERR_SUCCESS)
return err; return err;
@ -72,5 +72,5 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_congestion_control_stop(ChiakiCongestionCon
if(err != CHIAKI_ERR_SUCCESS) if(err != CHIAKI_ERR_SUCCESS)
return err; return err;
return chiaki_pred_cond_fini(&control->stop_cond); return chiaki_bool_pred_cond_fini(&control->stop_cond);
} }

View file

@ -74,7 +74,7 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_stream_connection_run(ChiakiSession *sessio
stream_connection->ecdh_secret = NULL; stream_connection->ecdh_secret = NULL;
ChiakiErrorCode err = chiaki_pred_cond_init(&stream_connection->stop_cond); ChiakiErrorCode err = chiaki_bool_pred_cond_init(&stream_connection->stop_cond);
if(err != CHIAKI_ERR_SUCCESS) if(err != CHIAKI_ERR_SUCCESS)
goto error; goto error;
@ -177,12 +177,12 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_stream_connection_run(ChiakiSession *sessio
CHIAKI_LOGI(&session->log, "StreamConnection successfully received streaminfo\n"); CHIAKI_LOGI(&session->log, "StreamConnection successfully received streaminfo\n");
err = chiaki_pred_cond_lock(&stream_connection->stop_cond); err = chiaki_bool_pred_cond_lock(&stream_connection->stop_cond);
assert(err == CHIAKI_ERR_SUCCESS); assert(err == CHIAKI_ERR_SUCCESS);
while(true) while(true)
{ {
err = chiaki_pred_cond_timedwait(&stream_connection->stop_cond, HEARTBEAT_INTERVAL_MS); err = chiaki_bool_pred_cond_timedwait(&stream_connection->stop_cond, HEARTBEAT_INTERVAL_MS);
if(err != CHIAKI_ERR_TIMEOUT) if(err != CHIAKI_ERR_TIMEOUT)
break; break;
@ -193,7 +193,7 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_stream_connection_run(ChiakiSession *sessio
CHIAKI_LOGI(stream_connection->log, "StreamConnection sent heartbeat\n"); CHIAKI_LOGI(stream_connection->log, "StreamConnection sent heartbeat\n");
} }
err = chiaki_pred_cond_unlock(&stream_connection->stop_cond); err = chiaki_bool_pred_cond_unlock(&stream_connection->stop_cond);
assert(err == CHIAKI_ERR_SUCCESS); assert(err == CHIAKI_ERR_SUCCESS);
CHIAKI_LOGI(&session->log, "StreamConnection is disconnecting\n"); CHIAKI_LOGI(&session->log, "StreamConnection is disconnecting\n");
@ -212,7 +212,7 @@ error_takion:
error_mirai: error_mirai:
chiaki_mirai_fini(&stream_connection->mirai); chiaki_mirai_fini(&stream_connection->mirai);
error_stop_cond: error_stop_cond:
chiaki_pred_cond_fini(&stream_connection->stop_cond); chiaki_bool_pred_cond_fini(&stream_connection->stop_cond);
error: error:
free(stream_connection->ecdh_secret); free(stream_connection->ecdh_secret);
return err; return err;

View file

@ -155,6 +155,32 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_cond_timedwait(ChiakiCond *cond, ChiakiMute
return chiaki_cond_timedwait_abs(cond, mutex, &timeout); return chiaki_cond_timedwait_abs(cond, mutex, &timeout);
} }
CHIAKI_EXPORT ChiakiErrorCode chiaki_cond_wait_pred(ChiakiCond *cond, ChiakiMutex *mutex, ChiakiCheckPred check_pred, void *check_pred_user)
{
while(!check_pred(check_pred_user))
{
ChiakiErrorCode err = chiaki_cond_wait(cond, mutex);
if(err != CHIAKI_ERR_SUCCESS)
return err;
}
return CHIAKI_ERR_SUCCESS;
}
CHIAKI_EXPORT ChiakiErrorCode chiaki_cond_timedwait_pred(ChiakiCond *cond, ChiakiMutex *mutex, uint64_t timeout_ms, ChiakiCheckPred check_pred, void *check_pred_user)
{
struct timespec timeout;
set_timeout(&timeout, timeout_ms);
while(!check_pred(check_pred_user))
{
ChiakiErrorCode err = chiaki_cond_timedwait_abs(cond, mutex, &timeout);
if(err != CHIAKI_ERR_SUCCESS)
return err;
}
return CHIAKI_ERR_SUCCESS;
}
CHIAKI_EXPORT ChiakiErrorCode chiaki_cond_signal(ChiakiCond *cond) CHIAKI_EXPORT ChiakiErrorCode chiaki_cond_signal(ChiakiCond *cond)
{ {
int r = pthread_cond_signal(&cond->cond); int r = pthread_cond_signal(&cond->cond);
@ -174,7 +200,7 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_cond_broadcast(ChiakiCond *cond)
CHIAKI_EXPORT ChiakiErrorCode chiaki_pred_cond_init(ChiakiPredCond *cond) CHIAKI_EXPORT ChiakiErrorCode chiaki_bool_pred_cond_init(ChiakiBoolPredCond *cond)
{ {
cond->pred = false; cond->pred = false;
@ -192,7 +218,7 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_pred_cond_init(ChiakiPredCond *cond)
return CHIAKI_ERR_SUCCESS; return CHIAKI_ERR_SUCCESS;
} }
CHIAKI_EXPORT ChiakiErrorCode chiaki_pred_cond_fini(ChiakiPredCond *cond) CHIAKI_EXPORT ChiakiErrorCode chiaki_bool_pred_cond_fini(ChiakiBoolPredCond *cond)
{ {
ChiakiErrorCode err = chiaki_cond_fini(&cond->cond); ChiakiErrorCode err = chiaki_cond_fini(&cond->cond);
if(err != CHIAKI_ERR_SUCCESS) if(err != CHIAKI_ERR_SUCCESS)
@ -205,67 +231,59 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_pred_cond_fini(ChiakiPredCond *cond)
return CHIAKI_ERR_SUCCESS; return CHIAKI_ERR_SUCCESS;
} }
CHIAKI_EXPORT ChiakiErrorCode chiaki_pred_cond_lock(ChiakiPredCond *cond)
CHIAKI_EXPORT ChiakiErrorCode chiaki_bool_pred_cond_lock(ChiakiBoolPredCond *cond)
{ {
return chiaki_mutex_lock(&cond->mutex); return chiaki_mutex_lock(&cond->mutex);
} }
CHIAKI_EXPORT ChiakiErrorCode chiaki_pred_cond_unlock(ChiakiPredCond *cond) CHIAKI_EXPORT ChiakiErrorCode chiaki_bool_pred_cond_unlock(ChiakiBoolPredCond *cond)
{ {
return chiaki_mutex_unlock(&cond->mutex); return chiaki_mutex_unlock(&cond->mutex);
} }
CHIAKI_EXPORT ChiakiErrorCode chiaki_pred_cond_wait(ChiakiPredCond *cond) bool bool_pred_cond_check_pred(void *user)
{ {
while(!cond->pred) ChiakiBoolPredCond *bool_pred_cond = user;
{ return bool_pred_cond->pred;
ChiakiErrorCode err = chiaki_cond_wait(&cond->cond, &cond->mutex);
if(err != CHIAKI_ERR_SUCCESS)
return err;
}
return CHIAKI_ERR_SUCCESS;
} }
CHIAKI_EXPORT ChiakiErrorCode chiaki_pred_cond_timedwait(ChiakiPredCond *cond, uint64_t timeout_ms) CHIAKI_EXPORT ChiakiErrorCode chiaki_bool_pred_cond_wait(ChiakiBoolPredCond *cond)
{ {
struct timespec timeout; return chiaki_cond_wait_pred(&cond->cond, &cond->mutex, bool_pred_cond_check_pred, cond);
set_timeout(&timeout, timeout_ms);
while(!cond->pred)
{
ChiakiErrorCode err = chiaki_cond_timedwait_abs(&cond->cond, &cond->mutex, &timeout);
if(err != CHIAKI_ERR_SUCCESS)
return err;
}
return CHIAKI_ERR_SUCCESS;
} }
CHIAKI_EXPORT void chiaki_pred_cond_reset(ChiakiPredCond *cond) CHIAKI_EXPORT ChiakiErrorCode chiaki_bool_pred_cond_timedwait(ChiakiBoolPredCond *cond, uint64_t timeout_ms)
{ {
cond->pred = false; return chiaki_cond_timedwait_pred(&cond->cond, &cond->mutex, timeout_ms, bool_pred_cond_check_pred, cond);
} }
CHIAKI_EXPORT ChiakiErrorCode chiaki_pred_cond_signal(ChiakiPredCond *cond) CHIAKI_EXPORT ChiakiErrorCode chiaki_bool_pred_cond_signal(ChiakiBoolPredCond *cond)
{ {
ChiakiErrorCode err = chiaki_mutex_lock(&cond->mutex); ChiakiErrorCode err = chiaki_bool_pred_cond_lock(cond);
if(err != CHIAKI_ERR_SUCCESS) if(err != CHIAKI_ERR_SUCCESS)
return err; return err;
cond->pred = true; cond->pred = true;
err = chiaki_cond_signal(&cond->cond);
ChiakiErrorCode unlock_err = chiaki_mutex_unlock(&cond->mutex); err = chiaki_bool_pred_cond_unlock(cond);
return err == CHIAKI_ERR_SUCCESS ? unlock_err : err; if(err != CHIAKI_ERR_SUCCESS)
return err;
return chiaki_cond_signal(&cond->cond);
} }
CHIAKI_EXPORT ChiakiErrorCode chiaki_pred_cond_broadcast(ChiakiPredCond *cond) CHIAKI_EXPORT ChiakiErrorCode chiaki_bool_pred_cond_broadcast(ChiakiBoolPredCond *cond)
{ {
ChiakiErrorCode err = chiaki_mutex_lock(&cond->mutex); ChiakiErrorCode err = chiaki_bool_pred_cond_lock(cond);
if(err != CHIAKI_ERR_SUCCESS) if(err != CHIAKI_ERR_SUCCESS)
return err; return err;
cond->pred = true; cond->pred = true;
err = chiaki_cond_broadcast(&cond->cond);
ChiakiErrorCode unlock_err = chiaki_mutex_unlock(&cond->mutex); err = chiaki_bool_pred_cond_unlock(cond);
return err == CHIAKI_ERR_SUCCESS ? unlock_err : err; if(err != CHIAKI_ERR_SUCCESS)
return err;
return chiaki_cond_broadcast(&cond->cond);
} }