Commit 4ae53964 authored by Andreas Schmidt's avatar Andreas Schmidt
Browse files

Locking channel state information.

parent eaaf1062
......@@ -4,13 +4,13 @@
#include "../util/dbg.h"
#include "channelStateInformation.h"
// TODO: LOCKS
PrrtChannelStateInformation * PrrtChannelStateInformation_create()
{
PrrtChannelStateInformation *csi = calloc(1, sizeof(PrrtChannelStateInformation));
check_mem(csi);
check(pthread_mutex_init(&csi->lock, NULL) == 0, "Mutex init failed.");
csi->rttMean = 0;
csi->rttDev = 0;
return csi;
......@@ -22,16 +22,20 @@ PrrtChannelStateInformation * PrrtChannelStateInformation_create()
void PrrtChannelStateInformation_update_rtt(PrrtChannelStateInformation *csi, prrtTimedelta_t rtt)
{
pthread_mutex_lock(&csi->lock);
int32_t delta = rtt - csi->rttMean;
// TODO: ensure that there are no arithemtic problems via rounding etc.
csi->rttMean = (prrtTimedelta_t) (csi->rttMean + RRT_ALPHA * delta);
csi->rttDev = (prrtTimedelta_t) (csi->rttDev + RRT_ALPHA * (labs(delta) - csi->rttDev));
pthread_mutex_unlock(&csi->lock);
}
void PrrtChannelStateInformation_print(PrrtChannelStateInformation *csi)
{
pthread_mutex_lock(&csi->lock);
check(csi != NULL, "Input should not be NULL.");
printf("RTT [us]: %d +- %d\n", csi->rttMean, csi->rttDev);
pthread_mutex_unlock(&csi->lock);
return;
error:
......@@ -40,11 +44,17 @@ void PrrtChannelStateInformation_print(PrrtChannelStateInformation *csi)
prrtTimedelta_t PrrtChannelStateInformation_get_rtt(PrrtChannelStateInformation *csi)
{
return csi->rttMean;
pthread_mutex_lock(&csi->lock);
prrtTimedelta_t res = csi->rttMean;
pthread_mutex_unlock(&csi->lock);
return res;
}
bool PrrtChannelStateInformation_destroy(PrrtChannelStateInformation *csi)
{
check(pthread_mutex_destroy(&csi->lock) == EXIT_SUCCESS, "Destroy mutex failed.");
free(csi);
return true;
error:
return false;
}
......@@ -4,6 +4,7 @@
#include "packet.h"
typedef struct prrtChannelStateInformation {
pthread_mutex_t lock;
prrtTimedelta_t rttMean;
prrtTimedelta_t rttDev;
} PrrtChannelStateInformation;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment