Commit 33b73193 authored by Robert Schmidt's avatar Robert Schmidt

FlexRAN timers: enable timer add/remove within timer event

A lock help during executing a timer event prevented such event from
creating/removing new timers due to a dead lock. This commit changes the
logic of the timing loop and adding/removing code such that this dead
lock does not happen.
parent 7bdf807b
...@@ -58,6 +58,11 @@ struct timesync { ...@@ -58,6 +58,11 @@ struct timesync {
int timer_num; int timer_num;
flexran_agent_timer_element_t *timer[MAX_NUM_TIMERS]; flexran_agent_timer_element_t *timer[MAX_NUM_TIMERS];
int add_num;
flexran_agent_timer_element_t *add_list[MAX_NUM_TIMERS];
int remove_num;
int remove_list[MAX_NUM_TIMERS];
pthread_mutex_t mutex_timer; pthread_mutex_t mutex_timer;
int exit; int exit;
...@@ -76,8 +81,12 @@ err_code_t flexran_agent_timer_init(mid_t mod_id) { ...@@ -76,8 +81,12 @@ err_code_t flexran_agent_timer_init(mid_t mod_id) {
sync->current = 0; sync->current = 0;
sync->next = 0; sync->next = 0;
sync->timer_num = 0; sync->timer_num = 0;
for (int i = 0; i < MAX_NUM_TIMERS; ++i) sync->add_num = 0;
sync->remove_num = 0;
for (int i = 0; i < MAX_NUM_TIMERS; ++i) {
sync->timer[i] = NULL; sync->timer[i] = NULL;
sync->add_list[i] = NULL;
}
pthread_mutex_init(&sync->mutex_timer, NULL); pthread_mutex_init(&sync->mutex_timer, NULL);
sync->exit = 0; sync->exit = 0;
...@@ -170,12 +179,19 @@ void *flexran_agent_timer_thread(void *args) { ...@@ -170,12 +179,19 @@ void *flexran_agent_timer_thread(void *args) {
break; break;
pthread_mutex_lock(&sync->mutex_timer); pthread_mutex_lock(&sync->mutex_timer);
sync->current++; for (int i = 0; i < sync->add_num; ++i) {
sync->timer[sync->timer_num] = sync->add_list[i];
if (sync->current < sync->next) { sync->timer_num++;
}
sync->add_num = 0;
for (int i = 0; i < sync->remove_num; ++i)
flexran_agent_timer_remove_internal(sync, sync->remove_list[i]);
sync->remove_num = 0;
pthread_mutex_unlock(&sync->mutex_timer); pthread_mutex_unlock(&sync->mutex_timer);
sync->current++;
if (sync->current < sync->next)
continue; continue;
}
for (int i = 0; i < sync->timer_num; ++i) { for (int i = 0; i < sync->timer_num; ++i) {
flexran_agent_timer_element_t *t = sync->timer[i]; flexran_agent_timer_element_t *t = sync->timer[i];
...@@ -186,7 +202,6 @@ void *flexran_agent_timer_thread(void *args) { ...@@ -186,7 +202,6 @@ void *flexran_agent_timer_thread(void *args) {
if (sync->next == sync->current || t->next < sync->next) if (sync->next == sync->current || t->next < sync->next)
sync->next = t->next; sync->next = t->next;
} }
pthread_mutex_unlock(&sync->mutex_timer);
} }
LOG_W(FLEXRAN_AGENT, "terminated timer thread\n"); LOG_W(FLEXRAN_AGENT, "terminated timer thread\n");
return NULL; return NULL;
...@@ -230,10 +245,10 @@ err_code_t flexran_agent_create_timer(mid_t mod_id, ...@@ -230,10 +245,10 @@ err_code_t flexran_agent_create_timer(mid_t mod_id,
struct timesync *sync = &timesync[mod_id]; struct timesync *sync = &timesync[mod_id];
pthread_mutex_lock(&sync->mutex_timer); pthread_mutex_lock(&sync->mutex_timer);
if (sync->timer_num >= MAX_NUM_TIMERS) { if (sync->timer_num + sync->add_num >= MAX_NUM_TIMERS) {
pthread_mutex_unlock(&sync->mutex_timer); pthread_mutex_unlock(&sync->mutex_timer);
LOG_E(FLEXRAN_AGENT, "maximum number of timers (%d) reached while adding timer %d\n", LOG_E(FLEXRAN_AGENT, "maximum number of timers (%d) reached while adding timer %d\n",
sync->timer_num, xid); sync->timer_num + sync->add_num, xid);
free(t); free(t);
return TIMER_SETUP_FAILED; return TIMER_SETUP_FAILED;
} }
...@@ -241,8 +256,8 @@ err_code_t flexran_agent_create_timer(mid_t mod_id, ...@@ -241,8 +256,8 @@ err_code_t flexran_agent_create_timer(mid_t mod_id,
t->next = sync->current + sf; t->next = sync->current + sf;
if (sync->next <= sync->current || t->next < sync->next) if (sync->next <= sync->current || t->next < sync->next)
sync->next = t->next; sync->next = t->next;
sync->timer[sync->timer_num] = t; sync->add_list[sync->add_num] = t;
sync->timer_num++; sync->add_num++;
pthread_mutex_unlock(&sync->mutex_timer); pthread_mutex_unlock(&sync->mutex_timer);
LOG_I(FLEXRAN_AGENT, "added new timer xid %d for agent %d\n", xid, mod_id); LOG_I(FLEXRAN_AGENT, "added new timer xid %d for agent %d\n", xid, mod_id);
return 0; return 0;
...@@ -250,15 +265,15 @@ err_code_t flexran_agent_create_timer(mid_t mod_id, ...@@ -250,15 +265,15 @@ err_code_t flexran_agent_create_timer(mid_t mod_id,
err_code_t flexran_agent_destroy_timer(mid_t mod_id, xid_t xid) { err_code_t flexran_agent_destroy_timer(mid_t mod_id, xid_t xid) {
struct timesync *sync = &timesync[mod_id]; struct timesync *sync = &timesync[mod_id];
pthread_mutex_lock(&sync->mutex_timer);
for (int i = 0; i < sync->timer_num; ++i) { for (int i = 0; i < sync->timer_num; ++i) {
if (sync->timer[i]->xid == xid) { if (sync->timer[i]->xid == xid) {
flexran_agent_timer_remove_internal(sync, i); pthread_mutex_lock(&sync->mutex_timer);
sync->remove_list[sync->remove_num] = i;
sync->remove_num++;
pthread_mutex_unlock(&sync->mutex_timer); pthread_mutex_unlock(&sync->mutex_timer);
return 0; return 0;
} }
} }
pthread_mutex_unlock(&sync->mutex_timer);
LOG_E(FLEXRAN_AGENT, "could not find timer %d\n", xid); LOG_E(FLEXRAN_AGENT, "could not find timer %d\n", xid);
return TIMER_ELEMENT_NOT_FOUND; return TIMER_ELEMENT_NOT_FOUND;
} }
......
Markdown is supported
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