From f58e653ac76b7b0b2be6bcdd7e6c84cdc1272513 Mon Sep 17 00:00:00 2001 From: Marcus Winter Date: Mon, 7 Nov 2016 14:03:20 +0100 Subject: [PATCH] base_c: atomic initialization check in debug mode, atomic initialization with value --- base_c/include/embb/base/c/atomic.h | 12 ++++++++++-- base_c/include/embb/base/c/internal/atomic/atomic_variables.h | 30 ++++++++++++++++++++++++++++++ base_c/include/embb/base/c/internal/atomic/init.h | 5 ++++- base_c/src/counter.c | 3 +-- base_c/src/mutex.c | 5 +---- base_c/test/thread_index_test.cc | 3 +-- mtapi_c/src/embb_mtapi_action_plugin_t.c | 3 +-- mtapi_c/src/embb_mtapi_action_t.c | 6 ++---- mtapi_c/src/embb_mtapi_group_t.c | 12 ++++-------- mtapi_c/src/embb_mtapi_node_t.c | 3 +-- mtapi_c/src/embb_mtapi_queue_t.c | 12 ++++-------- mtapi_c/src/embb_mtapi_scheduler_t.c | 3 +-- mtapi_c/src/embb_mtapi_task_t.c | 8 +++----- mtapi_c/src/embb_mtapi_thread_context_t.c | 7 ++----- mtapi_c/test/embb_mtapi_test_error.cc | 2 +- mtapi_c/test/embb_mtapi_test_plugin.cc | 6 ++---- mtapi_plugins_c/mtapi_network_c/src/embb_mtapi_network.c | 3 +-- 17 files changed, 69 insertions(+), 54 deletions(-) diff --git a/base_c/include/embb/base/c/atomic.h b/base_c/include/embb/base/c/atomic.h index c0868d9..c32e563 100644 --- a/base_c/include/embb/base/c/atomic.h +++ b/base_c/include/embb/base/c/atomic.h @@ -320,8 +320,6 @@ void embb_mutex_destroy( #define EMBB_ATOMIC_MUTEX_LOCK(mutex) embb_mutex_lock(&(mutex)) #define EMBB_ATOMIC_MUTEX_UNLOCK(mutex) embb_mutex_unlock(&(mutex)) #define EMBB_ATOMIC_MUTEX_DESTROY(mutex) embb_mutex_destroy(&(mutex)) -#define EMBB_ATOMIC_INIT_CHECK(variable) assert(variable->marker == 0x12345678) -#define EMBB_ATOMIC_INIT_MARKER(variable) variable->marker = 0x12345678 #else @@ -329,6 +327,16 @@ void embb_mutex_destroy( #define EMBB_ATOMIC_MUTEX_LOCK(...) #define EMBB_ATOMIC_MUTEX_UNLOCK(...) #define EMBB_ATOMIC_MUTEX_DESTROY(...) + +#endif + +#ifdef EMBB_DEBUG + +#define EMBB_ATOMIC_INIT_CHECK(variable) assert(variable->marker == 0x12345678) +#define EMBB_ATOMIC_INIT_MARKER(variable) variable->marker = 0x12345678 + +#else + #define EMBB_ATOMIC_INIT_CHECK(variable) (void)(variable) #define EMBB_ATOMIC_INIT_MARKER(variable) (void)(variable) diff --git a/base_c/include/embb/base/c/internal/atomic/atomic_variables.h b/base_c/include/embb/base/c/internal/atomic/atomic_variables.h index 2e85f37..e0fa02e 100644 --- a/base_c/include/embb/base/c/internal/atomic/atomic_variables.h +++ b/base_c/include/embb/base/c/internal/atomic/atomic_variables.h @@ -35,6 +35,8 @@ #include #endif +#ifdef EMBB_DEBUG + #ifdef EMBB_THREADING_ANALYSIS_MODE #define EMBB_ATOMIC_INTERNAL_DEFINE_VARIABLE( \ @@ -55,10 +57,38 @@ typedef struct \ { \ volatile EMBB_ATOMIC_PARAMETER_TYPE_NATIVE internal_variable; \ + uint32_t marker; \ } EMBB_CAT2(embb_atomic_, EMBB_ATOMIC_PARAMETER_ATOMIC_TYPE_SUFFIX); #endif +#else + +#ifdef EMBB_THREADING_ANALYSIS_MODE + +#define EMBB_ATOMIC_INTERNAL_DEFINE_VARIABLE( \ + EMBB_ATOMIC_PARAMETER_TYPE_NATIVE, \ + EMBB_ATOMIC_PARAMETER_ATOMIC_TYPE_SUFFIX) \ + typedef struct \ +{ \ + volatile EMBB_ATOMIC_PARAMETER_TYPE_NATIVE internal_variable; \ + embb_mutex_t internal_mutex; \ +} EMBB_CAT2(embb_atomic_, EMBB_ATOMIC_PARAMETER_ATOMIC_TYPE_SUFFIX); + +#else + +#define EMBB_ATOMIC_INTERNAL_DEFINE_VARIABLE( \ + EMBB_ATOMIC_PARAMETER_TYPE_NATIVE, \ + EMBB_ATOMIC_PARAMETER_ATOMIC_TYPE_SUFFIX) \ + typedef struct \ +{ \ + volatile EMBB_ATOMIC_PARAMETER_TYPE_NATIVE internal_variable; \ +} EMBB_CAT2(embb_atomic_, EMBB_ATOMIC_PARAMETER_ATOMIC_TYPE_SUFFIX); + +#endif + +#endif + EMBB_ATOMIC_INTERNAL_DEFINE_VARIABLE(char, char) EMBB_ATOMIC_INTERNAL_DEFINE_VARIABLE(short, short) EMBB_ATOMIC_INTERNAL_DEFINE_VARIABLE(unsigned short, unsigned_short) diff --git a/base_c/include/embb/base/c/internal/atomic/init.h b/base_c/include/embb/base/c/internal/atomic/init.h index 68f6ce4..6199493 100644 --- a/base_c/include/embb/base/c/internal/atomic/init.h +++ b/base_c/include/embb/base/c/internal/atomic/init.h @@ -33,6 +33,7 @@ #include #include #include +#include #include /* @@ -41,9 +42,11 @@ */ #define EMBB_ATOMIC_INTERNAL_DEFINE_INIT_METHOD(EMBB_ATOMIC_PARAMETER_TYPE_NATIVE, EMBB_ATOMIC_PARAMETER_ATOMIC_TYPE_SUFFIX, EMBB_ATOMIC_PARAMETER_TYPE_SIZE) \ EMBB_PLATFORM_INLINE void EMBB_CAT2(embb_atomic_init_, EMBB_ATOMIC_PARAMETER_ATOMIC_TYPE_SUFFIX)(\ - EMBB_CAT2(embb_atomic_, EMBB_ATOMIC_PARAMETER_ATOMIC_TYPE_SUFFIX)* variable) { \ + EMBB_CAT2(embb_atomic_, EMBB_ATOMIC_PARAMETER_ATOMIC_TYPE_SUFFIX)* variable, \ + EMBB_ATOMIC_PARAMETER_TYPE_NATIVE value) { \ EMBB_ATOMIC_MUTEX_INIT(variable->internal_mutex); \ EMBB_ATOMIC_INIT_MARKER(variable); \ + EMBB_CAT2(embb_atomic_store_, EMBB_ATOMIC_PARAMETER_ATOMIC_TYPE_SUFFIX)(variable, value); \ } #undef EMBB_ATOMIC_METHOD_TO_GENERATE diff --git a/base_c/src/counter.c b/base_c/src/counter.c index 2b8f0b7..47e985c 100644 --- a/base_c/src/counter.c +++ b/base_c/src/counter.c @@ -33,8 +33,7 @@ int embb_counter_init(embb_counter_t* counter) { if (counter == NULL) { return EMBB_ERROR; } - embb_atomic_init_unsigned_int(&(counter->value)); - embb_atomic_store_unsigned_int(&(counter->value), 0); + embb_atomic_init_unsigned_int(&(counter->value), 0); return EMBB_SUCCESS; } diff --git a/base_c/src/mutex.c b/base_c/src/mutex.c index a4203ca..158af1d 100644 --- a/base_c/src/mutex.c +++ b/base_c/src/mutex.c @@ -147,10 +147,7 @@ int embb_spin_init(embb_spinlock_t* spinlock) { if (NULL == spinlock) { return EMBB_ERROR; } - // For now, store the initial value. In the future will use atomic init - // function (as soon as available). - embb_atomic_init_int(&spinlock->atomic_spin_variable_); - embb_atomic_store_int(&spinlock->atomic_spin_variable_, 0); + embb_atomic_init_int(&spinlock->atomic_spin_variable_, 0); return EMBB_SUCCESS; } diff --git a/base_c/test/thread_index_test.cc b/base_c/test/thread_index_test.cc index adfa3cc..8ac3e40 100644 --- a/base_c/test/thread_index_test.cc +++ b/base_c/test/thread_index_test.cc @@ -40,8 +40,7 @@ embb_atomic_int flag; ThreadIndexTest::ThreadIndexTest() : number_threads_(partest::TestSuite::GetDefaultNumThreads()) { - embb_atomic_init_int(&flag); - embb_atomic_store_int(&flag, 1); + embb_atomic_init_int(&flag, 1); CreateUnit("Test 0 indices").Add(&ThreadIndexTest::Test0, this); CreateUnit("Test 1 index").Add(&ThreadIndexTest::Test1, this); diff --git a/mtapi_c/src/embb_mtapi_action_plugin_t.c b/mtapi_c/src/embb_mtapi_action_plugin_t.c index 85e0db7..0649548 100644 --- a/mtapi_c/src/embb_mtapi_action_plugin_t.c +++ b/mtapi_c/src/embb_mtapi_action_plugin_t.c @@ -62,8 +62,7 @@ mtapi_action_hndl_t mtapi_ext_plugin_action_create( new_action->node_local_data_size = node_local_data_size; new_action->enabled = MTAPI_TRUE; new_action->is_plugin_action = MTAPI_TRUE; - embb_atomic_init_int(&new_action->num_tasks); - embb_atomic_store_int(&new_action->num_tasks, 0); + embb_atomic_init_int(&new_action->num_tasks, 0); new_action->plugin_task_start_function = task_start_function; new_action->plugin_task_cancel_function = task_cancel_function; diff --git a/mtapi_c/src/embb_mtapi_action_t.c b/mtapi_c/src/embb_mtapi_action_t.c index d84b85f..a2426b7 100644 --- a/mtapi_c/src/embb_mtapi_action_t.c +++ b/mtapi_c/src/embb_mtapi_action_t.c @@ -59,8 +59,7 @@ void embb_mtapi_action_initialize(embb_mtapi_action_t* that) { that->node_local_data = NULL; that->node_local_data_size = 0; that->plugin_data = MTAPI_NULL; - embb_atomic_init_int(&that->num_tasks); - embb_atomic_store_int(&that->num_tasks, 0); + embb_atomic_init_int(&that->num_tasks, 0); } void embb_mtapi_action_finalize(embb_mtapi_action_t* that) { @@ -153,8 +152,7 @@ mtapi_action_hndl_t mtapi_action_create( new_action->node_local_data_size = node_local_data_size; new_action->enabled = MTAPI_TRUE; new_action->is_plugin_action = MTAPI_FALSE; - embb_atomic_init_int(&new_action->num_tasks); - embb_atomic_store_int(&new_action->num_tasks, 0); + embb_atomic_init_int(&new_action->num_tasks, 0); new_action->action_function = action_function; diff --git a/mtapi_c/src/embb_mtapi_group_t.c b/mtapi_c/src/embb_mtapi_group_t.c index 2f28045..9fa9efc 100644 --- a/mtapi_c/src/embb_mtapi_group_t.c +++ b/mtapi_c/src/embb_mtapi_group_t.c @@ -55,10 +55,8 @@ void embb_mtapi_group_initialize(embb_mtapi_group_t * that) { assert(MTAPI_NULL != that); that->group_id = MTAPI_GROUP_ID_NONE; - embb_atomic_init_int(&that->deleted); - embb_atomic_store_int(&that->deleted, MTAPI_FALSE); - embb_atomic_init_int(&that->num_tasks); - embb_atomic_store_int(&that->num_tasks, 0); + embb_atomic_init_int(&that->deleted, MTAPI_FALSE); + embb_atomic_init_int(&that->num_tasks, 0); embb_mtapi_task_queue_initialize(&that->queue); } @@ -69,10 +67,8 @@ void embb_mtapi_group_initialize_with_node( assert(MTAPI_NULL != node); that->group_id = MTAPI_GROUP_ID_NONE; - embb_atomic_init_int(&that->deleted); - embb_atomic_store_int(&that->deleted, MTAPI_FALSE); - embb_atomic_init_int(&that->num_tasks); - embb_atomic_store_int(&that->num_tasks, 0); + embb_atomic_init_int(&that->deleted, MTAPI_FALSE); + embb_atomic_init_int(&that->num_tasks, 0); embb_mtapi_task_queue_initialize_with_capacity( &that->queue, node->attributes.queue_limit); } diff --git a/mtapi_c/src/embb_mtapi_node_t.c b/mtapi_c/src/embb_mtapi_node_t.c index e839694..32126ce 100644 --- a/mtapi_c/src/embb_mtapi_node_t.c +++ b/mtapi_c/src/embb_mtapi_node_t.c @@ -105,8 +105,7 @@ void mtapi_initialize( } if (MTAPI_SUCCESS == local_status) { - embb_atomic_init_int(&node->is_scheduler_running); - embb_atomic_store_int(&node->is_scheduler_running, MTAPI_FALSE); + embb_atomic_init_int(&node->is_scheduler_running, MTAPI_FALSE); /* initialize storage */ embb_mtapi_job_initialize_list(node); diff --git a/mtapi_c/src/embb_mtapi_queue_t.c b/mtapi_c/src/embb_mtapi_queue_t.c index 2f373b6..36b5e83 100644 --- a/mtapi_c/src/embb_mtapi_queue_t.c +++ b/mtapi_c/src/embb_mtapi_queue_t.c @@ -56,10 +56,8 @@ void embb_mtapi_queue_initialize(embb_mtapi_queue_t* that) { mtapi_queueattr_init(&that->attributes, MTAPI_NULL); that->queue_id = MTAPI_QUEUE_ID_NONE; - embb_atomic_init_char(&that->enabled); - embb_atomic_store_char(&that->enabled, MTAPI_FALSE); - embb_atomic_init_int(&that->num_tasks); - embb_atomic_store_int(&that->num_tasks, 0); + embb_atomic_init_char(&that->enabled, MTAPI_FALSE); + embb_atomic_init_int(&that->num_tasks, 0); that->job_handle.id = 0; that->job_handle.tag = 0; } @@ -73,10 +71,8 @@ void embb_mtapi_queue_initialize_with_attributes_and_job( that->attributes = *attributes; that->queue_id = MTAPI_QUEUE_ID_NONE; - embb_atomic_init_char(&that->enabled); - embb_atomic_store_char(&that->enabled, MTAPI_TRUE); - embb_atomic_init_int(&that->num_tasks); - embb_atomic_store_int(&that->num_tasks, 0); + embb_atomic_init_char(&that->enabled, MTAPI_TRUE); + embb_atomic_init_int(&that->num_tasks, 0); that->job_handle = job; } diff --git a/mtapi_c/src/embb_mtapi_scheduler_t.c b/mtapi_c/src/embb_mtapi_scheduler_t.c index e16e519..de34ba7 100644 --- a/mtapi_c/src/embb_mtapi_scheduler_t.c +++ b/mtapi_c/src/embb_mtapi_scheduler_t.c @@ -473,8 +473,7 @@ mtapi_boolean_t embb_mtapi_scheduler_initialize_with_mode( assert(MTAPI_NULL != that); assert(MTAPI_NULL != node); - embb_atomic_init_int(&that->affine_task_counter); - embb_atomic_store_int(&that->affine_task_counter, 0); + embb_atomic_init_int(&that->affine_task_counter, 0); /* Paranoia sanitizing of scheduler mode */ if (mode >= NUM_SCHEDULER_MODES) { diff --git a/mtapi_c/src/embb_mtapi_task_t.c b/mtapi_c/src/embb_mtapi_task_t.c index 8e50d39..5530e2e 100644 --- a/mtapi_c/src/embb_mtapi_task_t.c +++ b/mtapi_c/src/embb_mtapi_task_t.c @@ -79,16 +79,14 @@ void embb_mtapi_task_initialize(embb_mtapi_task_t* that) { that->action.id = EMBB_MTAPI_IDPOOL_INVALID_ID; that->job.id = EMBB_MTAPI_IDPOOL_INVALID_ID; - embb_atomic_init_int(&that->state); - embb_atomic_store_int(&that->state, MTAPI_TASK_ERROR); + embb_atomic_init_int(&that->state, MTAPI_TASK_ERROR); that->task_id = MTAPI_TASK_ID_NONE; that->group.id = EMBB_MTAPI_IDPOOL_INVALID_ID; that->queue.id = EMBB_MTAPI_IDPOOL_INVALID_ID; that->error_code = MTAPI_SUCCESS; - embb_atomic_init_unsigned_int(&that->current_instance); - embb_atomic_store_unsigned_int(&that->current_instance, 0); + embb_atomic_init_unsigned_int(&that->current_instance, 0); embb_spin_init(&that->state_lock); - embb_atomic_init_unsigned_int(&that->instances_todo); + embb_atomic_init_unsigned_int(&that->instances_todo, 0); } void embb_mtapi_task_finalize(embb_mtapi_task_t* that) { diff --git a/mtapi_c/src/embb_mtapi_thread_context_t.c b/mtapi_c/src/embb_mtapi_thread_context_t.c index b96ea6b..d550c4b 100644 --- a/mtapi_c/src/embb_mtapi_thread_context_t.c +++ b/mtapi_c/src/embb_mtapi_thread_context_t.c @@ -59,11 +59,8 @@ mtapi_boolean_t embb_mtapi_thread_context_initialize( that->is_main_thread = (worker_index == 0) ? node->attributes.reuse_main_thread : MTAPI_FALSE; - embb_atomic_init_int(&that->run); - embb_atomic_init_int(&that->is_sleeping); - - embb_atomic_store_int(&that->run, 0); - embb_atomic_store_int(&that->is_sleeping, 0); + embb_atomic_init_int(&that->run, 0); + embb_atomic_init_int(&that->is_sleeping, 0); that->queue = (embb_mtapi_task_queue_t**)embb_mtapi_alloc_allocate( sizeof(embb_mtapi_task_queue_t)*that->priorities); diff --git a/mtapi_c/test/embb_mtapi_test_error.cc b/mtapi_c/test/embb_mtapi_test_error.cc index 2481ee5..57cb1e0 100644 --- a/mtapi_c/test/embb_mtapi_test_error.cc +++ b/mtapi_c/test/embb_mtapi_test_error.cc @@ -733,7 +733,7 @@ void TestContext() { } void ErrorTest::TestBasic() { - embb_atomic_init_int(&wait); + embb_atomic_init_int(&wait, 0); TestNodeNotInit(); TestParameter(); diff --git a/mtapi_c/test/embb_mtapi_test_plugin.cc b/mtapi_c/test/embb_mtapi_test_plugin.cc index 7c1ccee..c5bc34f 100644 --- a/mtapi_c/test/embb_mtapi_test_plugin.cc +++ b/mtapi_c/test/embb_mtapi_test_plugin.cc @@ -84,10 +84,8 @@ void plugin_initialize( plugin_task.id = 0; plugin_task.tag = 0; - embb_atomic_init_int(&plugin_running); - embb_atomic_store_int(&plugin_running, 1); - embb_atomic_init_int(&plugin_task_available); - embb_atomic_store_int(&plugin_task_available, 0); + embb_atomic_init_int(&plugin_running, 1); + embb_atomic_init_int(&plugin_task_available, 0); err = embb_thread_create(&plugin_thread, NULL, plugin_thread_function, NULL); if (EMBB_SUCCESS == err) { diff --git a/mtapi_plugins_c/mtapi_network_c/src/embb_mtapi_network.c b/mtapi_plugins_c/mtapi_network_c/src/embb_mtapi_network.c index 3c5f3b7..9bac593 100644 --- a/mtapi_plugins_c/mtapi_network_c/src/embb_mtapi_network.c +++ b/mtapi_plugins_c/mtapi_network_c/src/embb_mtapi_network.c @@ -658,8 +658,7 @@ void mtapi_network_plugin_initialize( err = embb_mtapi_network_initialize(); if (0 == err) return; - embb_atomic_init_int(&plugin->run); - embb_atomic_store_int(&plugin->run, 0); + embb_atomic_init_int(&plugin->run, 0); err = embb_mtapi_network_buffer_initialize( &plugin->recv_buffer, (int)buffer_size); -- libgit2 0.26.0