From 8c875f2f291b069dd40ec2e075c92f5c853e492d Mon Sep 17 00:00:00 2001 From: Tobias Langer Date: Tue, 4 Oct 2016 13:10:12 +0200 Subject: [PATCH] embb_time_t time stamps The implementation now uses embb_time_t time stamps instead of uin64_t time stamps. Thereby time checks now rely on embb internals, instead of more clumsy comparissons. Furthermore this allows to use the embb::base::Duration on EMBB level and thus setting _relative_ deadlines, instead of _absolute_ deadlines on task creation. --- base_cpp/include/embb/base/time.h | 9 +++++++++ base_cpp/src/time.cc | 6 ++++++ mtapi_c/include/embb/mtapi/c/mtapi.h | 7 +++++-- mtapi_c/src/embb_mtapi_attr.c | 2 +- mtapi_c/src/embb_mtapi_attr.h | 5 ++++- mtapi_c/src/embb_mtapi_priority_queue_t.c | 37 +++++++++++++++++++++---------------- mtapi_c/src/embb_mtapi_task_t.c | 2 +- mtapi_c/src/mtapi_task_attributes_t.c | 2 +- mtapi_cpp/include/embb/mtapi/execution_policy.h | 25 ++++++++++++++++++------- mtapi_cpp/include/embb/mtapi/task_attributes.h | 5 +++-- mtapi_cpp/src/execution_policy.cc | 25 +++++++++++-------------- 11 files changed, 80 insertions(+), 45 deletions(-) diff --git a/base_cpp/include/embb/base/time.h b/base_cpp/include/embb/base/time.h index f942588..6bdf76f 100644 --- a/base_cpp/include/embb/base/time.h +++ b/base_cpp/include/embb/base/time.h @@ -63,6 +63,15 @@ class Time { embb_time_in(&rep_, &(duration.rep_)); } +#ifdef EMBB_HARD_REALTIME + /** + * Return the internal representation of the current moment in time of the + * MTAPI C interface. This is necessary for communication with the MTAPI + * layer. + */ + embb_time_t GetRepresentation(); +#endif /*EMBB_HARD_REALTIME*/ + private: /** * Representation of the absolute time point. diff --git a/base_cpp/src/time.cc b/base_cpp/src/time.cc index 1f827c5..e02ef13 100644 --- a/base_cpp/src/time.cc +++ b/base_cpp/src/time.cc @@ -36,6 +36,12 @@ Time::Time() : rep_() { embb_time_now(&rep_); } +#ifdef EMBB_HARD_REALTIME +embb_time_t Time::GetRepresentation() { + return rep_; +} +#endif /*EMBB_HARD_REALTIME*/ + } // namespace base } // namespace embb diff --git a/mtapi_c/include/embb/mtapi/c/mtapi.h b/mtapi_c/include/embb/mtapi/c/mtapi.h index f381950..3100f4b 100644 --- a/mtapi_c/include/embb/mtapi/c/mtapi.h +++ b/mtapi_c/include/embb/mtapi/c/mtapi.h @@ -245,6 +245,9 @@ #include #include #include +#ifdef EMBB_HARD_REALTIME +#include +#endif /*EMBB_HARD_REALTIME*/ #ifdef __cplusplus extern "C" { @@ -753,7 +756,7 @@ enum mtapi_task_attributes_enum { #define MTAPI_TASK_AFFINITY_SIZE sizeof(mtapi_affinity_t) #ifdef EMBB_HARD_REALTIME /** size of the \a MTAPI_TASK_DEADLINE attribute */ -#define MTAPI_TASK_DEADLINE_SIZE sizeof(mtapi_uint64_t) +#define MTAPI_TASK_DEADLINE_SIZE sizeof(embb_time_t) #endif /*EMBB_HARD_REALTIME*/ @@ -851,7 +854,7 @@ struct mtapi_task_attributes_struct { mtapi_uint_t num_instances; /**< stores MTAPI_TASK_INSTANCES */ mtapi_uint_t priority; /**< stores MTAPI_TASK_PRIORITY */ #ifdef EMBB_HARD_REALTIME - mtapi_uint64_t deadline; /**< stores MTAPI_TASK_DEADLINE */ + embb_time_t deadline; /**< stores MTAPI_TASK_DEADLINE */ #endif /*EMBB_HARD_REALTIME*/ mtapi_affinity_t affinity; /**< stores MTAPI_TASK_AFFINITY */ void * user_data; /**< stores MTAPI_TASK_USER_DATA */ diff --git a/mtapi_c/src/embb_mtapi_attr.c b/mtapi_c/src/embb_mtapi_attr.c index 50e4da2..32f9f84 100644 --- a/mtapi_c/src/embb_mtapi_attr.c +++ b/mtapi_c/src/embb_mtapi_attr.c @@ -61,7 +61,7 @@ mtapi_status_t embb_mtapi_attr_get_##TYPE(const TYPE* source, \ embb_mtapi_attr_implementation(mtapi_uint_t); #ifdef EMBB_HARD_REALTIME -embb_mtapi_attr_implementation(mtapi_uint64_t); +embb_mtapi_attr_implementation(embb_time_t); #endif /*EMBB_HARD_REALTIME*/ embb_mtapi_attr_implementation(mtapi_affinity_t); embb_mtapi_attr_implementation(mtapi_boolean_t); diff --git a/mtapi_c/src/embb_mtapi_attr.h b/mtapi_c/src/embb_mtapi_attr.h index 09e953b..a748cfb 100644 --- a/mtapi_c/src/embb_mtapi_attr.h +++ b/mtapi_c/src/embb_mtapi_attr.h @@ -28,6 +28,9 @@ #define MTAPI_C_SRC_EMBB_MTAPI_ATTR_H_ #include +#ifdef EMBB_HARD_REALTIME +#include +#endif /*EMBB_HARD_REALTIME*/ #ifdef __cplusplus extern "C" { @@ -42,7 +45,7 @@ mtapi_status_t embb_mtapi_attr_get_##TYPE(const TYPE* source, \ embb_mtapi_attr(mtapi_uint_t) #ifdef EMBB_HARD_REALTIME -embb_mtapi_attr(mtapi_uint64_t); +embb_mtapi_attr(embb_time_t); #endif /*EMBB_HARD_REALTIME*/ embb_mtapi_attr(mtapi_affinity_t) embb_mtapi_attr(mtapi_boolean_t) diff --git a/mtapi_c/src/embb_mtapi_priority_queue_t.c b/mtapi_c/src/embb_mtapi_priority_queue_t.c index 62ab89d..2dc2aeb 100644 --- a/mtapi_c/src/embb_mtapi_priority_queue_t.c +++ b/mtapi_c/src/embb_mtapi_priority_queue_t.c @@ -33,6 +33,8 @@ #include #include +#include + #include #include #include @@ -42,9 +44,9 @@ /* ---- LOCAL HELPERS ------------------------------------------------------ */ -static mtapi_uint64_t get_deadline(mtapi_task_hndl_t node) { +static embb_time_t get_deadline(mtapi_task_hndl_t node) { mtapi_status_t status; - mtapi_uint64_t deadline; + embb_time_t deadline; mtapi_task_get_attribute( node, MTAPI_TASK_DEADLINE, @@ -53,7 +55,8 @@ static mtapi_uint64_t get_deadline(mtapi_task_hndl_t node) { &status); if(status != MTAPI_SUCCESS) { // TODO error handling - return -1; + embb_time_t ret; + return ret; } return deadline; } @@ -103,11 +106,11 @@ embb_mtapi_task_t * embb_mtapi_priority_queue_pop(embb_mtapi_priority_queue_t* t embb_mtapi_task_t * task = MTAPI_NULL; embb_mtapi_task_t * tmp_task = MTAPI_NULL; mtapi_uint_t ii; - mtapi_uint64_t deadline = 0; - mtapi_uint64_t left_deadline = 0; - mtapi_uint64_t right_deadline = 0; + embb_time_t deadline; + embb_time_t left_deadline; + embb_time_t right_deadline; mtapi_uint64_t swap = 0; - mtapi_uint64_t swap_deadline = 0; + embb_time_t swap_deadline; assert(MTAPI_NULL != that); @@ -126,31 +129,33 @@ embb_mtapi_task_t * embb_mtapi_priority_queue_pop(embb_mtapi_priority_queue_t* t /* recreate heap property */ while(that->tasks_available > 0) { - deadline = get_deadline(that->task_buffer[ii]->handle); + deadline = get_deadline(that->task_buffer[ii]->handle); if(that->task_buffer[ii + 1] != MTAPI_NULL) { left_deadline = get_deadline(that->task_buffer[ii + 1]->handle); } else { - left_deadline = ULLONG_MAX; + left_deadline.seconds = ULLONG_MAX; + left_deadline.nanoseconds = ULONG_MAX; } if(that->task_buffer[ii + 2] != MTAPI_NULL) { right_deadline = get_deadline(that->task_buffer[ii + 2]->handle); } else { - right_deadline = ULLONG_MAX; + right_deadline.seconds = ULLONG_MAX; + right_deadline.nanoseconds = ULONG_MAX; } /* min Heap, swap with the smaller of both children. */ - if(left_deadline <= right_deadline) { + if(embb_time_compare(&left_deadline, &right_deadline) <= 0) { swap = ii + 1; swap_deadline = left_deadline; - } else if(right_deadline < left_deadline) { + } else if(embb_time_compare(&left_deadline, &right_deadline) > 0) { swap = ii + 2; swap_deadline = right_deadline; } /* abort if heap property is restored. */ - if(deadline <= swap_deadline) { + if(embb_time_compare(&deadline, &swap_deadline) <= 0) { break; } @@ -173,8 +178,8 @@ mtapi_boolean_t embb_mtapi_priority_queue_push( embb_mtapi_task_t * tmp_task = MTAPI_NULL; mtapi_uint_t ii; mtapi_uint_t kk; - mtapi_uint64_t deadline = 0; - mtapi_uint64_t parent_deadline = 0; + embb_time_t deadline; + embb_time_t parent_deadline; mtapi_boolean_t result = MTAPI_FALSE; assert(MTAPI_NULL != that); @@ -195,7 +200,7 @@ mtapi_boolean_t embb_mtapi_priority_queue_push( parent_deadline = get_deadline(that->task_buffer[ii]->handle); /* abort if heap property is restored. */ - if(deadline >= parent_deadline) { + if(embb_time_compare(&deadline, &parent_deadline) >= 0) { break; } diff --git a/mtapi_c/src/embb_mtapi_task_t.c b/mtapi_c/src/embb_mtapi_task_t.c index d88e6d4..f8a21d8 100644 --- a/mtapi_c/src/embb_mtapi_task_t.c +++ b/mtapi_c/src/embb_mtapi_task_t.c @@ -442,7 +442,7 @@ void mtapi_task_get_attribute( #ifdef EMBB_HARD_REALTIME case MTAPI_TASK_DEADLINE: - local_status = embb_mtapi_attr_get_mtapi_uint64_t( + local_status = embb_mtapi_attr_get_embb_time_t( &local_task->attributes.deadline, attribute, attribute_size); break; diff --git a/mtapi_c/src/mtapi_task_attributes_t.c b/mtapi_c/src/mtapi_task_attributes_t.c index 1ca1f7c..962eee3 100644 --- a/mtapi_c/src/mtapi_task_attributes_t.c +++ b/mtapi_c/src/mtapi_task_attributes_t.c @@ -104,7 +104,7 @@ void mtapi_taskattr_set( #ifdef EMBB_HARD_REALTIME case MTAPI_TASK_DEADLINE: - local_status = embb_mtapi_attr_set_mtapi_uint64_t( + local_status = embb_mtapi_attr_set_embb_time_t( &attributes->deadline, attribute, attribute_size); break; diff --git a/mtapi_cpp/include/embb/mtapi/execution_policy.h b/mtapi_cpp/include/embb/mtapi/execution_policy.h index 78b61c4..c4a4e55 100644 --- a/mtapi_cpp/include/embb/mtapi/execution_policy.h +++ b/mtapi_cpp/include/embb/mtapi/execution_policy.h @@ -28,6 +28,10 @@ #define EMBB_MTAPI_EXECUTION_POLICY_H_ #include +#ifdef EMBB_HARD_REALTIME +#include +#include +#endif /*EMBB_HARD_REALTIME*/ #include namespace embb { @@ -79,10 +83,18 @@ class ExecutionPolicy{ * Constructs an execution policy with the specified priority. * Sets the affinity to all worker threads. */ + template explicit ExecutionPolicy( - mtapi_uint64_t deadline /**< [in] Relative dealine for the - execution policy. */ - ); + embb::base::Duration relative_deadline /**< [in] Relative dealine + for the execution policy.*/ + ) : priority_(DefaultPriority), deadline_(relative_deadline) { + InitializeWithDeadline(); + } + + /** + * Initialize the ExecutionPolicy object. Necessary to prevent include loops. + */ + void InitializeWithDeadline(); #endif /*EMBB_HARD_REALTIME*/ /** @@ -143,7 +155,7 @@ class ExecutionPolicy{ * * \return the deadline */ - mtapi_uint_t GetDeadline() const; + embb::base::Time GetDeadline() const; #endif /*EMBB_HARD_REALTIME*/ friend class Task; @@ -158,9 +170,8 @@ class ExecutionPolicy{ #ifdef EMBB_HARD_REALTIME /** * Default deadline. - * Currently set to UULONG_MAX = MAX deadline. */ - static const mtapi_uint64_t DefaultDeadline; + static const embb::base::Time DefaultDeadline; #endif /*EMBB_HARD_REALTIME*/ /** @@ -178,7 +189,7 @@ class ExecutionPolicy{ /** * Task Priority. */ - mtapi_uint64_t deadline_; + embb::base::Time deadline_; #endif /*EMBB_HARD_REALTIME*/ }; diff --git a/mtapi_cpp/include/embb/mtapi/task_attributes.h b/mtapi_cpp/include/embb/mtapi/task_attributes.h index 283e5c6..8208e2c 100644 --- a/mtapi_cpp/include/embb/mtapi/task_attributes.h +++ b/mtapi_cpp/include/embb/mtapi/task_attributes.h @@ -150,11 +150,12 @@ class TaskAttributes { * \notthreadsafe */ TaskAttributes & SetDeadline( - mtapi_uint64_t deadline /**< The deadline to set. */ + embb::base::Time deadline /**< The deadline to set. */ ) { + embb_time_t c_deadline = deadline.GetRepresentation(); mtapi_status_t status; mtapi_taskattr_set(&attributes_, MTAPI_TASK_DEADLINE, - &deadline, sizeof(deadline), &status); + &c_deadline, sizeof(c_deadline), &status); internal::CheckStatus(status); return *this; } diff --git a/mtapi_cpp/src/execution_policy.cc b/mtapi_cpp/src/execution_policy.cc index 025cfb0..a85f06e 100644 --- a/mtapi_cpp/src/execution_policy.cc +++ b/mtapi_cpp/src/execution_policy.cc @@ -86,17 +86,16 @@ ExecutionPolicy::ExecutionPolicy(mtapi_uint_t priority) } #ifdef EMBB_HARD_REALTIME -ExecutionPolicy::ExecutionPolicy(mtapi_uint64_t deadline) -: priority_(DefaultPriority), deadline_(deadline) { -#if MTAPI_CPP_AUTOMATIC_INITIALIZE - Node::GetInstance(); // MTAPI has to be initialized +void ExecutionPolicy::InitializeWithDeadline() { +#ifdef MTAPI_CPP_AUTOMATIC_INITIALIZE + Node::GetInstance(); // MTAPI has to be initialized #endif - mtapi_status_t status; - mtapi_affinity_init(&affinity_, MTAPI_TRUE, &status); - if (MTAPI_SUCCESS != status) { - EMBB_THROW(embb::base::ErrorException, - "Could not default construct Affinity."); - } + mtapi_status_t status; + mtapi_affinity_init(&affinity_, MTAPI_TRUE, &status); + if(MTAPI_SUCCESS != status) { + EMBB_THROW(embb::base::ErrorException, + "Could not default construct Affinity."); + } } #endif /*EMBB_HARD_REALTIME*/ @@ -150,15 +149,13 @@ mtapi_uint_t ExecutionPolicy::GetPriority() const { } #ifdef EMBB_HARD_REALTIME -mtapi_uint_t ExecutionPolicy::GetDeadline() const { +embb::base::Time ExecutionPolicy::GetDeadline() const { return deadline_; } +const embb::base::Time ExecutionPolicy::DefaultDeadline; #endif /*EMBB_HARD_REALTIME*/ const mtapi_uint_t ExecutionPolicy::DefaultPriority = 0; -#ifdef EMBB_HARD_REALTIME -const mtapi_uint64_t ExecutionPolicy::DefaultDeadline = ULLONG_MAX; -#endif /*EMBB_HARD_REALTIME*/ } // namespace mtapi } // namespace embb -- libgit2 0.26.0