Commit 5c73ee43 by Marcus Winter

mtapi_c: fixed problem with non monotonic time readings and timeouts

parent 04ead31a
...@@ -289,10 +289,12 @@ void mtapi_action_delete( ...@@ -289,10 +289,12 @@ void mtapi_action_delete(
embb_mtapi_thread_context_t * context = NULL; embb_mtapi_thread_context_t * context = NULL;
embb_duration_t wait_duration; embb_duration_t wait_duration;
embb_time_t start_time;
embb_time_t end_time; embb_time_t end_time;
if (MTAPI_INFINITE < timeout) { if (MTAPI_INFINITE < timeout) {
embb_duration_set_milliseconds( embb_duration_set_milliseconds(
&wait_duration, (unsigned long long)timeout); &wait_duration, (unsigned long long)timeout);
embb_time_now(&start_time);
embb_time_in(&end_time, &wait_duration); embb_time_in(&end_time, &wait_duration);
} }
...@@ -309,6 +311,12 @@ void mtapi_action_delete( ...@@ -309,6 +311,12 @@ void mtapi_action_delete(
if (MTAPI_INFINITE < timeout) { if (MTAPI_INFINITE < timeout) {
embb_time_t current_time; embb_time_t current_time;
embb_time_now(&current_time); embb_time_now(&current_time);
if (embb_time_compare(&current_time, &start_time) < 0) {
/* time has moved backwards, maybe a wraparound or jitter
move end_time backward to avoid endeless loop */
start_time = current_time;
embb_time_in(&end_time, &wait_duration);
}
if (embb_time_compare(&current_time, &end_time) > 0) { if (embb_time_compare(&current_time, &end_time) > 0) {
/* timeout! */ /* timeout! */
local_status = MTAPI_TIMEOUT; local_status = MTAPI_TIMEOUT;
...@@ -362,10 +370,12 @@ void mtapi_action_disable( ...@@ -362,10 +370,12 @@ void mtapi_action_disable(
embb_mtapi_thread_context_t * context = NULL; embb_mtapi_thread_context_t * context = NULL;
embb_duration_t wait_duration; embb_duration_t wait_duration;
embb_time_t start_time;
embb_time_t end_time; embb_time_t end_time;
if (MTAPI_INFINITE < timeout) { if (MTAPI_INFINITE < timeout) {
embb_duration_set_milliseconds( embb_duration_set_milliseconds(
&wait_duration, (unsigned long long)timeout); &wait_duration, (unsigned long long)timeout);
embb_time_now(&start_time);
embb_time_in(&end_time, &wait_duration); embb_time_in(&end_time, &wait_duration);
} }
...@@ -382,6 +392,12 @@ void mtapi_action_disable( ...@@ -382,6 +392,12 @@ void mtapi_action_disable(
if (MTAPI_INFINITE < timeout) { if (MTAPI_INFINITE < timeout) {
embb_time_t current_time; embb_time_t current_time;
embb_time_now(&current_time); embb_time_now(&current_time);
if (embb_time_compare(&current_time, &start_time) < 0) {
/* time has moved backwards, maybe a wraparound or jitter
move end_time backward to avoid endeless loop */
start_time = current_time;
embb_time_in(&end_time, &wait_duration);
}
if (embb_time_compare(&current_time, &end_time) > 0) { if (embb_time_compare(&current_time, &end_time) > 0) {
/* timeout! */ /* timeout! */
local_status = MTAPI_TIMEOUT; local_status = MTAPI_TIMEOUT;
......
...@@ -213,10 +213,12 @@ void mtapi_group_wait_all( ...@@ -213,10 +213,12 @@ void mtapi_group_wait_all(
node->group_pool, group); node->group_pool, group);
embb_duration_t wait_duration; embb_duration_t wait_duration;
embb_time_t start_time;
embb_time_t end_time; embb_time_t end_time;
if (MTAPI_INFINITE < timeout) { if (MTAPI_INFINITE < timeout) {
embb_duration_set_milliseconds( embb_duration_set_milliseconds(
&wait_duration, (unsigned long long)timeout); &wait_duration, (unsigned long long)timeout);
embb_time_now(&start_time);
embb_time_in(&end_time, &wait_duration); embb_time_in(&end_time, &wait_duration);
} }
...@@ -232,6 +234,12 @@ void mtapi_group_wait_all( ...@@ -232,6 +234,12 @@ void mtapi_group_wait_all(
if (MTAPI_INFINITE < timeout) { if (MTAPI_INFINITE < timeout) {
embb_time_t current_time; embb_time_t current_time;
embb_time_now(&current_time); embb_time_now(&current_time);
if (embb_time_compare(&current_time, &start_time) < 0) {
/* time has moved backwards, maybe a wraparound or jitter
move end_time backward to avoid endeless loop */
start_time = current_time;
embb_time_in(&end_time, &wait_duration);
}
if (embb_time_compare(&current_time, &end_time) > 0) { if (embb_time_compare(&current_time, &end_time) > 0) {
/* timeout! */ /* timeout! */
local_status = MTAPI_TIMEOUT; local_status = MTAPI_TIMEOUT;
...@@ -300,10 +308,12 @@ void mtapi_group_wait_any( ...@@ -300,10 +308,12 @@ void mtapi_group_wait_any(
embb_mtapi_thread_context_t * context = NULL; embb_mtapi_thread_context_t * context = NULL;
embb_duration_t wait_duration; embb_duration_t wait_duration;
embb_time_t start_time;
embb_time_t end_time; embb_time_t end_time;
if (MTAPI_INFINITE < timeout) { if (MTAPI_INFINITE < timeout) {
embb_duration_set_milliseconds( embb_duration_set_milliseconds(
&wait_duration, (unsigned long long)timeout); &wait_duration, (unsigned long long)timeout);
embb_time_now(&start_time);
embb_time_in(&end_time, &wait_duration); embb_time_in(&end_time, &wait_duration);
} }
...@@ -318,6 +328,12 @@ void mtapi_group_wait_any( ...@@ -318,6 +328,12 @@ void mtapi_group_wait_any(
if (MTAPI_INFINITE < timeout) { if (MTAPI_INFINITE < timeout) {
embb_time_t current_time; embb_time_t current_time;
embb_time_now(&current_time); embb_time_now(&current_time);
if (embb_time_compare(&current_time, &start_time) < 0) {
/* time has moved backwards, maybe a wraparound or jitter
move end_time backward to avoid endeless loop */
start_time = current_time;
embb_time_in(&end_time, &wait_duration);
}
if (embb_time_compare(&current_time, &end_time) > 0) { if (embb_time_compare(&current_time, &end_time) > 0) {
/* timeout! */ /* timeout! */
local_status = MTAPI_TIMEOUT; local_status = MTAPI_TIMEOUT;
......
...@@ -360,10 +360,12 @@ void mtapi_queue_delete( ...@@ -360,10 +360,12 @@ void mtapi_queue_delete(
embb_mtapi_thread_context_t * context = NULL; embb_mtapi_thread_context_t * context = NULL;
embb_duration_t wait_duration; embb_duration_t wait_duration;
embb_time_t start_time;
embb_time_t end_time; embb_time_t end_time;
if (MTAPI_INFINITE < timeout) { if (MTAPI_INFINITE < timeout) {
embb_duration_set_milliseconds( embb_duration_set_milliseconds(
&wait_duration, (unsigned long long)timeout); &wait_duration, (unsigned long long)timeout);
embb_time_now(&start_time);
embb_time_in(&end_time, &wait_duration); embb_time_in(&end_time, &wait_duration);
} }
...@@ -381,6 +383,12 @@ void mtapi_queue_delete( ...@@ -381,6 +383,12 @@ void mtapi_queue_delete(
if (MTAPI_INFINITE < timeout) { if (MTAPI_INFINITE < timeout) {
embb_time_t current_time; embb_time_t current_time;
embb_time_now(&current_time); embb_time_now(&current_time);
if (embb_time_compare(&current_time, &start_time) < 0) {
/* time has moved backwards, maybe a wraparound or jitter
move end_time backward to avoid endeless loop */
start_time = current_time;
embb_time_in(&end_time, &wait_duration);
}
if (embb_time_compare(&current_time, &end_time) > 0) { if (embb_time_compare(&current_time, &end_time) > 0) {
/* timeout! */ /* timeout! */
local_status = MTAPI_TIMEOUT; local_status = MTAPI_TIMEOUT;
...@@ -435,10 +443,12 @@ void mtapi_queue_disable( ...@@ -435,10 +443,12 @@ void mtapi_queue_disable(
embb_mtapi_scheduler_get_current_thread_context(node->scheduler); embb_mtapi_scheduler_get_current_thread_context(node->scheduler);
embb_duration_t wait_duration; embb_duration_t wait_duration;
embb_time_t start_time;
embb_time_t end_time; embb_time_t end_time;
if (MTAPI_INFINITE < timeout) { if (MTAPI_INFINITE < timeout) {
embb_duration_set_milliseconds( embb_duration_set_milliseconds(
&wait_duration, (unsigned long long)timeout); &wait_duration, (unsigned long long)timeout);
embb_time_now(&start_time);
embb_time_in(&end_time, &wait_duration); embb_time_in(&end_time, &wait_duration);
} }
...@@ -448,6 +458,12 @@ void mtapi_queue_disable( ...@@ -448,6 +458,12 @@ void mtapi_queue_disable(
if (MTAPI_INFINITE < timeout) { if (MTAPI_INFINITE < timeout) {
embb_time_t current_time; embb_time_t current_time;
embb_time_now(&current_time); embb_time_now(&current_time);
if (embb_time_compare(&current_time, &start_time) < 0) {
/* time has moved backwards, maybe a wraparound or jitter
move end_time backward to avoid endeless loop */
start_time = current_time;
embb_time_in(&end_time, &wait_duration);
}
if (embb_time_compare(&current_time, &end_time) > 0) { if (embb_time_compare(&current_time, &end_time) > 0) {
/* timeout! */ /* timeout! */
local_status = MTAPI_TIMEOUT; local_status = MTAPI_TIMEOUT;
......
...@@ -407,6 +407,7 @@ mtapi_boolean_t embb_mtapi_scheduler_wait_for_task( ...@@ -407,6 +407,7 @@ mtapi_boolean_t embb_mtapi_scheduler_wait_for_task(
embb_mtapi_node_t* node = embb_mtapi_node_get_instance(); embb_mtapi_node_t* node = embb_mtapi_node_get_instance();
embb_mtapi_thread_context_t * context = NULL; embb_mtapi_thread_context_t * context = NULL;
embb_duration_t wait_duration; embb_duration_t wait_duration;
embb_time_t start_time;
embb_time_t end_time; embb_time_t end_time;
assert(MTAPI_NULL != node); assert(MTAPI_NULL != node);
...@@ -414,6 +415,7 @@ mtapi_boolean_t embb_mtapi_scheduler_wait_for_task( ...@@ -414,6 +415,7 @@ mtapi_boolean_t embb_mtapi_scheduler_wait_for_task(
if (MTAPI_INFINITE < timeout) { if (MTAPI_INFINITE < timeout) {
embb_duration_set_milliseconds(&wait_duration, (unsigned long long)timeout); embb_duration_set_milliseconds(&wait_duration, (unsigned long long)timeout);
embb_time_now(&start_time);
embb_time_in(&end_time, &wait_duration); embb_time_in(&end_time, &wait_duration);
} }
...@@ -431,6 +433,12 @@ mtapi_boolean_t embb_mtapi_scheduler_wait_for_task( ...@@ -431,6 +433,12 @@ mtapi_boolean_t embb_mtapi_scheduler_wait_for_task(
if (MTAPI_INFINITE < timeout) { if (MTAPI_INFINITE < timeout) {
embb_time_t current_time; embb_time_t current_time;
embb_time_now(&current_time); embb_time_now(&current_time);
if (embb_time_compare(&current_time, &start_time) < 0) {
/* time has moved backwards, maybe a wraparound or jitter
move end_time backward to avoid endeless loop */
start_time = current_time;
embb_time_in(&end_time, &wait_duration);
}
if (embb_time_compare(&current_time, &end_time) > 0) { if (embb_time_compare(&current_time, &end_time) > 0) {
/* timeout! */ /* timeout! */
return MTAPI_FALSE; return MTAPI_FALSE;
......
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 sign in to comment