Commit 60cb6593 by Marcus Winter

Merge branch 'embb531_codesonar_fixes' into development

# Conflicts:
#	base_c/src/thread.c
#	mtapi_c/src/embb_mtapi_node_t.c
parents f459e5ef 5f59e812
...@@ -51,10 +51,14 @@ int embb_duration_set_nanoseconds(embb_duration_t* duration, ...@@ -51,10 +51,14 @@ int embb_duration_set_nanoseconds(embb_duration_t* duration,
} }
if (nanoseconds > 0) { if (nanoseconds > 0) {
if (embb_duration_min()->nanoseconds > nanoseconds) { if (embb_duration_min()->nanoseconds > nanoseconds) {
duration->seconds = 0;
duration->nanoseconds = 0;
return EMBB_UNDERFLOW; return EMBB_UNDERFLOW;
} }
const embb_duration_t* max = embb_duration_max(); const embb_duration_t* max = embb_duration_max();
if (max->seconds * 1000000000 + max->nanoseconds < nanoseconds) { if (max->seconds * 1000000000 + max->nanoseconds < nanoseconds) {
duration->seconds = max->seconds;
duration->nanoseconds = max->nanoseconds;
return EMBB_OVERFLOW; return EMBB_OVERFLOW;
} }
} }
...@@ -70,10 +74,14 @@ int embb_duration_set_microseconds(embb_duration_t* duration, ...@@ -70,10 +74,14 @@ int embb_duration_set_microseconds(embb_duration_t* duration,
} }
if (microseconds > 0) { if (microseconds > 0) {
if (embb_duration_min()->nanoseconds > microseconds*1000) { if (embb_duration_min()->nanoseconds > microseconds*1000) {
duration->seconds = 0;
duration->nanoseconds = 0;
return EMBB_UNDERFLOW; return EMBB_UNDERFLOW;
} }
const embb_duration_t* max = embb_duration_max(); const embb_duration_t* max = embb_duration_max();
if (max->seconds * 1000000 + max->nanoseconds / 1000 < microseconds) { if (max->seconds * 1000000 + max->nanoseconds / 1000 < microseconds) {
duration->seconds = max->seconds;
duration->nanoseconds = max->nanoseconds;
return EMBB_OVERFLOW; return EMBB_OVERFLOW;
} }
} }
...@@ -89,10 +97,14 @@ int embb_duration_set_milliseconds(embb_duration_t* duration, ...@@ -89,10 +97,14 @@ int embb_duration_set_milliseconds(embb_duration_t* duration,
} }
if (milliseconds > 0) { if (milliseconds > 0) {
if (embb_duration_min()->nanoseconds > milliseconds*1000000) { if (embb_duration_min()->nanoseconds > milliseconds*1000000) {
duration->seconds = 0;
duration->nanoseconds = 0;
return EMBB_UNDERFLOW; return EMBB_UNDERFLOW;
} }
const embb_duration_t* max = embb_duration_max(); const embb_duration_t* max = embb_duration_max();
if (max->seconds * 1000 + max->nanoseconds / 1000000 < milliseconds) { if (max->seconds * 1000 + max->nanoseconds / 1000000 < milliseconds) {
duration->seconds = max->seconds;
duration->nanoseconds = max->nanoseconds;
return EMBB_OVERFLOW; return EMBB_OVERFLOW;
} }
} }
...@@ -108,10 +120,14 @@ int embb_duration_set_seconds(embb_duration_t* duration, ...@@ -108,10 +120,14 @@ int embb_duration_set_seconds(embb_duration_t* duration,
} }
if (seconds > 0) { if (seconds > 0) {
if (embb_duration_min()->nanoseconds > seconds*1000000000) { if (embb_duration_min()->nanoseconds > seconds*1000000000) {
duration->seconds = 0;
duration->nanoseconds = 0;
return EMBB_UNDERFLOW; return EMBB_UNDERFLOW;
} }
const embb_duration_t* max = embb_duration_max(); const embb_duration_t* max = embb_duration_max();
if (max->seconds + max->nanoseconds / 1000000000 < seconds) { if (max->seconds + max->nanoseconds / 1000000000 < seconds) {
duration->seconds = max->seconds;
duration->nanoseconds = max->nanoseconds;
return EMBB_OVERFLOW; return EMBB_OVERFLOW;
} }
} }
...@@ -126,6 +142,8 @@ int embb_duration_add(embb_duration_t* lhs, const embb_duration_t* rhs) { ...@@ -126,6 +142,8 @@ int embb_duration_add(embb_duration_t* lhs, const embb_duration_t* rhs) {
} }
int carry = (int)((lhs->nanoseconds + rhs->nanoseconds) / 1000000000); int carry = (int)((lhs->nanoseconds + rhs->nanoseconds) / 1000000000);
if (lhs->seconds + rhs->seconds + carry > EMBB_DURATION_MAX_SECONDS) { if (lhs->seconds + rhs->seconds + carry > EMBB_DURATION_MAX_SECONDS) {
lhs->seconds = 0;
lhs->nanoseconds = 0;
return EMBB_OVERFLOW; return EMBB_OVERFLOW;
} }
lhs->nanoseconds = (lhs->nanoseconds + rhs->nanoseconds) % 1000000000; lhs->nanoseconds = (lhs->nanoseconds + rhs->nanoseconds) % 1000000000;
......
...@@ -90,7 +90,6 @@ void embb_log_write_internal( ...@@ -90,7 +90,6 @@ void embb_log_write_internal(
case EMBB_LOG_LEVEL_NONE: case EMBB_LOG_LEVEL_NONE:
default: default:
log_level_str = " ";
break; break;
} }
#if defined(EMBB_PLATFORM_COMPILER_MSVC) #if defined(EMBB_PLATFORM_COMPILER_MSVC)
......
...@@ -85,7 +85,10 @@ int embb_thread_create(embb_thread_t* thread, const embb_core_set_t* core_set, ...@@ -85,7 +85,10 @@ int embb_thread_create(embb_thread_t* thread, const embb_core_set_t* core_set,
} }
thread->embb_internal_arg = (embb_internal_thread_arg_t*) thread->embb_internal_arg = (embb_internal_thread_arg_t*)
embb_alloc(sizeof(embb_internal_thread_arg_t)); embb_alloc(sizeof(embb_internal_thread_arg_t));
if (thread->embb_internal_arg == NULL) return EMBB_NOMEM; if (thread->embb_internal_arg == NULL) {
thread->embb_internal_handle = NULL;
return EMBB_NOMEM;
}
thread->embb_internal_arg->func = func; thread->embb_internal_arg->func = func;
thread->embb_internal_arg->arg = arg; thread->embb_internal_arg->arg = arg;
...@@ -97,6 +100,8 @@ int embb_thread_create(embb_thread_t* thread, const embb_core_set_t* core_set, ...@@ -97,6 +100,8 @@ int embb_thread_create(embb_thread_t* thread, const embb_core_set_t* core_set,
0, /* no creation arguments */ 0, /* no creation arguments */
0); /* no system thread ID */ 0); /* no system thread ID */
if (thread->embb_internal_handle == NULL) { if (thread->embb_internal_handle == NULL) {
embb_free(thread->embb_internal_arg);
thread->embb_internal_arg = NULL;
return EMBB_ERROR; return EMBB_ERROR;
} }
...@@ -234,7 +239,11 @@ int embb_thread_create(embb_thread_t* thread, const embb_core_set_t* core_set, ...@@ -234,7 +239,11 @@ int embb_thread_create(embb_thread_t* thread, const embb_core_set_t* core_set,
} }
} }
status = pthread_attr_setaffinity_np(&attr, sizeof(cpuset), &cpuset); status = pthread_attr_setaffinity_np(&attr, sizeof(cpuset), &cpuset);
if (status != 0) return EMBB_ERROR; if (status != 0) {
thread->embb_internal_arg = NULL;
thread->embb_internal_handle = NULL;
return EMBB_ERROR;
}
#else #else
embb_log_write("base_c", EMBB_LOG_LEVEL_WARNING, "Could not set thread " embb_log_write("base_c", EMBB_LOG_LEVEL_WARNING, "Could not set thread "
"affinity, since no implementation available!\n"); "affinity, since no implementation available!\n");
...@@ -244,6 +253,11 @@ int embb_thread_create(embb_thread_t* thread, const embb_core_set_t* core_set, ...@@ -244,6 +253,11 @@ int embb_thread_create(embb_thread_t* thread, const embb_core_set_t* core_set,
/* Dynamic allocation of thread arguments. Freed on call of join. */ /* Dynamic allocation of thread arguments. Freed on call of join. */
thread->embb_internal_arg = (embb_internal_thread_arg_t*) thread->embb_internal_arg = (embb_internal_thread_arg_t*)
embb_alloc(sizeof(embb_internal_thread_arg_t)); embb_alloc(sizeof(embb_internal_thread_arg_t));
if (thread->embb_internal_arg == NULL) {
thread->embb_internal_handle = NULL;
pthread_attr_destroy(&attr);
return EMBB_NOMEM;
}
thread->embb_internal_arg->func = func; thread->embb_internal_arg->func = func;
thread->embb_internal_arg->arg = arg; thread->embb_internal_arg->arg = arg;
...@@ -265,11 +279,14 @@ int embb_thread_join(embb_thread_t* thread, int *result_code) { ...@@ -265,11 +279,14 @@ int embb_thread_join(embb_thread_t* thread, int *result_code) {
return EMBB_ERROR; return EMBB_ERROR;
} }
int status = 0; int status = 0;
if (thread == NULL) return EMBB_ERROR;
status = pthread_join(thread->embb_internal_handle, NULL); status = pthread_join(thread->embb_internal_handle, NULL);
if (result_code != NULL) { if (thread->embb_internal_arg != NULL) {
*result_code = thread->embb_internal_arg->result; if (result_code != NULL) {
*result_code = thread->embb_internal_arg->result;
}
embb_free(thread->embb_internal_arg);
} }
embb_free(thread->embb_internal_arg);
if (status != 0) { if (status != 0) {
return EMBB_ERROR; return EMBB_ERROR;
} }
......
...@@ -76,5 +76,7 @@ void* embb_tss_get(const embb_tss_t* tss) { ...@@ -76,5 +76,7 @@ void* embb_tss_get(const embb_tss_t* tss) {
void embb_tss_delete(embb_tss_t* tss) { void embb_tss_delete(embb_tss_t* tss) {
assert(tss != NULL); assert(tss != NULL);
embb_free_aligned(tss->values); if (tss->values != NULL) {
embb_free_aligned(tss->values);
}
} }
...@@ -152,6 +152,9 @@ void AllocTest::TestMixedAllocs() { ...@@ -152,6 +152,9 @@ void AllocTest::TestMixedAllocs() {
void* plain = NULL; void* plain = NULL;
plain = embb_alloc(2); plain = embb_alloc(2);
PT_EXPECT_NE(plain, static_cast<void*>(NULL)); PT_EXPECT_NE(plain, static_cast<void*>(NULL));
if (NULL == plain) {
return;
}
allocated = embb_get_bytes_allocated(); allocated = embb_get_bytes_allocated();
#ifdef EMBB_DEBUG #ifdef EMBB_DEBUG
expected += 2 + 2*sizeof(size_t); expected += 2 + 2*sizeof(size_t);
...@@ -162,6 +165,10 @@ void AllocTest::TestMixedAllocs() { ...@@ -162,6 +165,10 @@ void AllocTest::TestMixedAllocs() {
void* aligned = NULL; void* aligned = NULL;
aligned = embb_alloc_aligned(2*sizeof(void*), 2); aligned = embb_alloc_aligned(2*sizeof(void*), 2);
PT_EXPECT_NE(aligned, static_cast<void*>(NULL)); PT_EXPECT_NE(aligned, static_cast<void*>(NULL));
if (NULL == aligned) {
embb_free(plain);
return;
}
allocated = embb_get_bytes_allocated(); allocated = embb_get_bytes_allocated();
#ifdef EMBB_DEBUG #ifdef EMBB_DEBUG
expected += (1 + 1) * 2 * sizeof(void*) + 3 * sizeof(size_t) - 1; expected += (1 + 1) * 2 * sizeof(void*) + 3 * sizeof(size_t) - 1;
...@@ -172,6 +179,11 @@ void AllocTest::TestMixedAllocs() { ...@@ -172,6 +179,11 @@ void AllocTest::TestMixedAllocs() {
void* cache_aligned = NULL; void* cache_aligned = NULL;
cache_aligned = embb_alloc_cache_aligned(2); cache_aligned = embb_alloc_cache_aligned(2);
PT_EXPECT_NE(cache_aligned, static_cast<void*>(NULL)); PT_EXPECT_NE(cache_aligned, static_cast<void*>(NULL));
if (NULL == cache_aligned) {
embb_free(plain);
embb_free_aligned(aligned);
return;
}
allocated = embb_get_bytes_allocated(); allocated = embb_get_bytes_allocated();
#ifdef EMBB_DEBUG #ifdef EMBB_DEBUG
expected += (1 + 1) * EMBB_PLATFORM_CACHE_LINE_SIZE + 3 * sizeof(size_t) - 1; expected += (1 + 1) * EMBB_PLATFORM_CACHE_LINE_SIZE + 3 * sizeof(size_t) - 1;
......
...@@ -50,7 +50,11 @@ void ThreadSpecificStorageTest::Test() { ...@@ -50,7 +50,11 @@ void ThreadSpecificStorageTest::Test() {
size_t rank = partest::TestSuite::GetCurrentThreadID(); size_t rank = partest::TestSuite::GetCurrentThreadID();
void* value = embb_tss_get(&tss_); void* value = embb_tss_get(&tss_);
if (value == NULL) { if (value == NULL) {
int status = embb_tss_set(&tss_, new size_t(rank)); size_t * prank = new size_t(rank);
int status = embb_tss_set(&tss_, prank);
if (EMBB_SUCCESS != status) {
delete prank;
}
PT_EXPECT_EQ(status, EMBB_SUCCESS); PT_EXPECT_EQ(status, EMBB_SUCCESS);
} else { } else {
size_t stored_rank = *static_cast<size_t*>(value); size_t stored_rank = *static_cast<size_t*>(value);
......
...@@ -173,10 +173,6 @@ AtomicTest::TestStressSwap::TestStressSwap( ...@@ -173,10 +173,6 @@ AtomicTest::TestStressSwap::TestStressSwap(
size_t number_threads, size_t number_iterations) size_t number_threads, size_t number_iterations)
: TestUnit("Swap Stress test for Atomics"), swap1_counter(1) : TestUnit("Swap Stress test for Atomics"), swap1_counter(1)
, swap2_counter(2) { , swap2_counter(2) {
bitsets[0] = std::bitset<ATOMIC_TESTS_ITERATIONS * 2 + 1>();
bitsets[1] = std::bitset<ATOMIC_TESTS_ITERATIONS * 2 + 1>();
bitsets[2] = std::bitset<ATOMIC_TESTS_ITERATIONS * 2 + 1>();
PT_ASSERT(number_threads == 1); PT_ASSERT(number_threads == 1);
Pre(&TestStressSwap::Init, this); Pre(&TestStressSwap::Init, this);
......
...@@ -56,6 +56,7 @@ void LogTest::Test() { ...@@ -56,6 +56,7 @@ void LogTest::Test() {
logged_message = null; logged_message = null;
Log::Trace("chn", test_msg); Log::Trace("chn", test_msg);
#ifdef EMBB_DEBUG #ifdef EMBB_DEBUG
PT_ASSERT_NE(logged_message, null);
PT_EXPECT(0 == strcmp(logged_message, "[chn] - [TRACE] hello")); PT_EXPECT(0 == strcmp(logged_message, "[chn] - [TRACE] hello"));
#else #else
PT_EXPECT_EQ(null, logged_message); PT_EXPECT_EQ(null, logged_message);
...@@ -63,15 +64,18 @@ void LogTest::Test() { ...@@ -63,15 +64,18 @@ void LogTest::Test() {
logged_message = null; logged_message = null;
Log::Info("chn", test_msg); Log::Info("chn", test_msg);
#ifdef EMBB_DEBUG #ifdef EMBB_DEBUG
PT_ASSERT_NE(logged_message, null);
PT_EXPECT(0 == strcmp(logged_message, "[chn] - [INFO ] hello")); PT_EXPECT(0 == strcmp(logged_message, "[chn] - [INFO ] hello"));
#else #else
PT_EXPECT_EQ(null, logged_message); PT_EXPECT_EQ(null, logged_message);
#endif #endif
logged_message = null; logged_message = null;
Log::Warning("chn", test_msg); Log::Warning("chn", test_msg);
PT_ASSERT_NE(logged_message, null);
PT_EXPECT(0 == strcmp(logged_message, "[chn] - [WARN ] hello")); PT_EXPECT(0 == strcmp(logged_message, "[chn] - [WARN ] hello"));
logged_message = null; logged_message = null;
Log::Error("chn", test_msg); Log::Error("chn", test_msg);
PT_ASSERT_NE(logged_message, null);
PT_EXPECT(0 == strcmp(logged_message, "[chn] - [ERROR] hello")); PT_EXPECT(0 == strcmp(logged_message, "[chn] - [ERROR] hello"));
Log::SetLogLevel(EMBB_LOG_LEVEL_INFO); Log::SetLogLevel(EMBB_LOG_LEVEL_INFO);
...@@ -81,15 +85,18 @@ void LogTest::Test() { ...@@ -81,15 +85,18 @@ void LogTest::Test() {
logged_message = null; logged_message = null;
Log::Info("chn", test_msg); Log::Info("chn", test_msg);
#ifdef EMBB_DEBUG #ifdef EMBB_DEBUG
PT_ASSERT_NE(logged_message, null);
PT_EXPECT(0 == strcmp(logged_message, "[chn] - [INFO ] hello")); PT_EXPECT(0 == strcmp(logged_message, "[chn] - [INFO ] hello"));
#else #else
PT_EXPECT_EQ(null, logged_message); PT_EXPECT_EQ(null, logged_message);
#endif #endif
logged_message = null; logged_message = null;
Log::Warning("chn", test_msg); Log::Warning("chn", test_msg);
PT_ASSERT_NE(logged_message, null);
PT_EXPECT(0 == strcmp(logged_message, "[chn] - [WARN ] hello")); PT_EXPECT(0 == strcmp(logged_message, "[chn] - [WARN ] hello"));
logged_message = null; logged_message = null;
Log::Error("chn", test_msg); Log::Error("chn", test_msg);
PT_ASSERT_NE(logged_message, null);
PT_EXPECT(0 == strcmp(logged_message, "[chn] - [ERROR] hello")); PT_EXPECT(0 == strcmp(logged_message, "[chn] - [ERROR] hello"));
Log::SetLogLevel(EMBB_LOG_LEVEL_WARNING); Log::SetLogLevel(EMBB_LOG_LEVEL_WARNING);
...@@ -101,9 +108,11 @@ void LogTest::Test() { ...@@ -101,9 +108,11 @@ void LogTest::Test() {
PT_EXPECT_EQ(null, logged_message); PT_EXPECT_EQ(null, logged_message);
logged_message = null; logged_message = null;
Log::Warning("chn", test_msg); Log::Warning("chn", test_msg);
PT_ASSERT_NE(logged_message, null);
PT_EXPECT(0 == strcmp(logged_message, "[chn] - [WARN ] hello")); PT_EXPECT(0 == strcmp(logged_message, "[chn] - [WARN ] hello"));
logged_message = null; logged_message = null;
Log::Error("chn", test_msg); Log::Error("chn", test_msg);
PT_ASSERT_NE(logged_message, null);
PT_EXPECT(0 == strcmp(logged_message, "[chn] - [ERROR] hello")); PT_EXPECT(0 == strcmp(logged_message, "[chn] - [ERROR] hello"));
Log::SetLogLevel(EMBB_LOG_LEVEL_ERROR); Log::SetLogLevel(EMBB_LOG_LEVEL_ERROR);
...@@ -118,6 +127,7 @@ void LogTest::Test() { ...@@ -118,6 +127,7 @@ void LogTest::Test() {
PT_EXPECT_EQ(null, logged_message); PT_EXPECT_EQ(null, logged_message);
logged_message = null; logged_message = null;
Log::Error("chn", test_msg); Log::Error("chn", test_msg);
PT_ASSERT_NE(logged_message, null);
PT_EXPECT(0 == strcmp(logged_message, "[chn] - [ERROR] hello")); PT_EXPECT(0 == strcmp(logged_message, "[chn] - [ERROR] hello"));
Log::SetLogLevel(EMBB_LOG_LEVEL_NONE); Log::SetLogLevel(EMBB_LOG_LEVEL_NONE);
......
...@@ -123,8 +123,10 @@ bool LockFreeStack< Type, ValuePool >::TryPop(Type & element) { ...@@ -123,8 +123,10 @@ bool LockFreeStack< Type, ValuePool >::TryPop(Type & element) {
top_cached = top; top_cached = top;
// Stack empty, cannot pop // Stack empty, cannot pop
if (top_cached == NULL) if (top_cached == NULL) {
element = Type();
return false; return false;
}
// Guard top_cached // Guard top_cached
hazardPointer.Guard(0, top_cached); hazardPointer.Guard(0, top_cached);
......
...@@ -113,14 +113,17 @@ allocate_rec(int node, Type& element) { ...@@ -113,14 +113,17 @@ allocate_rec(int node, Type& element) {
int pool_index = NodeIndexToPoolIndex(node); int pool_index = NodeIndexToPoolIndex(node);
Type expected = pool_[pool_index]; Type expected = pool_[pool_index];
if (expected == Undefined) if (expected == Undefined) {
element = Type();
return -1; return -1;
}
if (pool_[pool_index].CompareAndSwap(expected, Undefined)) { if (pool_[pool_index].CompareAndSwap(expected, Undefined)) {
element = expected; element = expected;
return pool_index; return pool_index;
} }
element = Type();
return -1; return -1;
} }
...@@ -133,8 +136,10 @@ allocate_rec(int node, Type& element) { ...@@ -133,8 +136,10 @@ allocate_rec(int node, Type& element) {
do { do {
current = tree_[node]; current = tree_[node];
desired = current - 1; desired = current - 1;
if (desired < 0) if (desired < 0) {
element = Type();
return -1; return -1;
}
} while (!tree_[node].CompareAndSwap(current, desired)); } while (!tree_[node].CompareAndSwap(current, desired));
int leftResult = allocate_rec(GetLeftChildIndex(node), element); int leftResult = allocate_rec(GetLeftChildIndex(node), element);
......
...@@ -55,6 +55,7 @@ Allocate(Type & element) { ...@@ -55,6 +55,7 @@ Allocate(Type & element) {
return i; return i;
} }
} }
element = Type();
return -1; return -1;
} }
......
...@@ -144,6 +144,7 @@ void HazardPointerTest::HazardPointerTest1ThreadMethod() { ...@@ -144,6 +144,7 @@ void HazardPointerTest::HazardPointerTest1ThreadMethod() {
for (int i = 0; i != n_elements_per_thread_; ++i) { for (int i = 0; i != n_elements_per_thread_; ++i) {
embb::base::Atomic<int>* allocated_object = object_pool_->Allocate(0); embb::base::Atomic<int>* allocated_object = object_pool_->Allocate(0);
PT_ASSERT(NULL != allocated_object);
hazard_pointer_->Guard(0, allocated_object); hazard_pointer_->Guard(0, allocated_object);
...@@ -210,8 +211,9 @@ void HazardPointerTest2::DeletePointerCallback(int* to_delete) { ...@@ -210,8 +211,9 @@ void HazardPointerTest2::DeletePointerCallback(int* to_delete) {
} }
bool HazardPointerTest2::SetRelativeGuards() { bool HazardPointerTest2::SetRelativeGuards() {
unsigned int thread_index; unsigned int thread_index = 0;
embb_internal_thread_index(&thread_index); int result = embb_internal_thread_index(&thread_index);
PT_ASSERT(EMBB_SUCCESS == result);
unsigned int my_begin = guards_per_phread_count_*thread_index; unsigned int my_begin = guards_per_phread_count_*thread_index;
int guard_number = 0; int guard_number = 0;
...@@ -247,6 +249,7 @@ void HazardPointerTest2::HazardPointerTest2Master() { ...@@ -247,6 +249,7 @@ void HazardPointerTest2::HazardPointerTest2Master() {
// while the hazard pointer guard array is not full // while the hazard pointer guard array is not full
int** allocatedLocal = static_cast<int**>( int** allocatedLocal = static_cast<int**>(
embb::base::Allocation::Allocate(sizeof(int*)*guaranteed_capacity_pool_)); embb::base::Allocation::Allocate(sizeof(int*)*guaranteed_capacity_pool_));
PT_ASSERT(NULL != allocatedLocal);
bool full = false; bool full = false;
while (!full) { while (!full) {
...@@ -294,16 +297,19 @@ void HazardPointerTest2::HazardPointerTest2Pre() { ...@@ -294,16 +297,19 @@ void HazardPointerTest2::HazardPointerTest2Pre() {
// first the test pool has to be created // first the test pool has to be created
test_pool_ = embb::base::Allocation::New<IntObjectTestPool> test_pool_ = embb::base::Allocation::New<IntObjectTestPool>
(pool_size_using_hazard_pointer_); (pool_size_using_hazard_pointer_);
PT_ASSERT(NULL != test_pool_);
// after the pool has been created, we create the hp class // after the pool has been created, we create the hp class
hazard_pointer_ = embb::base::Allocation::New < hazard_pointer_ = embb::base::Allocation::New <
embb::containers::internal::HazardPointer<int*> > embb::containers::internal::HazardPointer<int*> >
(delete_pointer_callback_, static_cast<int*>(NULL), (delete_pointer_callback_, static_cast<int*>(NULL),
static_cast<int>(guards_per_phread_count_), n_threads); static_cast<int>(guards_per_phread_count_), n_threads);
PT_ASSERT(NULL != hazard_pointer_);
shared_guarded_ = static_cast<embb::base::Atomic<int*>*>( shared_guarded_ = static_cast<embb::base::Atomic<int*>*>(
embb::base::Allocation::Allocate(sizeof(embb::base::Atomic<int*>)* embb::base::Allocation::Allocate(sizeof(embb::base::Atomic<int*>)*
guaranteed_capacity_pool_)); guaranteed_capacity_pool_));
PT_ASSERT(NULL != shared_guarded_);
for (unsigned int i = 0; i != guaranteed_capacity_pool_; ++i) { for (unsigned int i = 0; i != guaranteed_capacity_pool_; ++i) {
// in-place new for each array cell // in-place new for each array cell
...@@ -450,8 +456,9 @@ void HazardPointerTest2::HazardPointerTest2Post() { ...@@ -450,8 +456,9 @@ void HazardPointerTest2::HazardPointerTest2Post() {
void HazardPointerTest2::HazardPointerTest2ThreadMethod() { void HazardPointerTest2::HazardPointerTest2ThreadMethod() {
for (;;) { for (;;) {
unsigned int thread_index; unsigned int thread_index = 0;
embb_internal_thread_index(&thread_index); int result = embb_internal_thread_index(&thread_index);
PT_ASSERT(EMBB_SUCCESS == result);
if (thread_index == current_master_) { if (thread_index == current_master_) {
HazardPointerTest2Master(); HazardPointerTest2Master();
......
...@@ -279,6 +279,7 @@ void mtapi_group_wait_any( ...@@ -279,6 +279,7 @@ void mtapi_group_wait_any(
MTAPI_IN mtapi_timeout_t timeout, MTAPI_IN mtapi_timeout_t timeout,
MTAPI_OUT mtapi_status_t* status) { MTAPI_OUT mtapi_status_t* status) {
mtapi_status_t local_status = MTAPI_ERR_UNKNOWN; mtapi_status_t local_status = MTAPI_ERR_UNKNOWN;
void* local_result = MTAPI_NULL;
embb_mtapi_log_trace("mtapi_group_wait_any() called\n"); embb_mtapi_log_trace("mtapi_group_wait_any() called\n");
...@@ -335,10 +336,7 @@ void mtapi_group_wait_any( ...@@ -335,10 +336,7 @@ void mtapi_group_wait_any(
} }
/* was there a timeout, or is there a result? */ /* was there a timeout, or is there a result? */
if (MTAPI_NULL != local_task) { if (MTAPI_NULL != local_task) {
/* store result */ local_result = local_task->result_buffer;
if (MTAPI_NULL != result) {
*result = local_task->result_buffer;
}
/* return error code set by the task */ /* return error code set by the task */
local_status = local_task->error_code; local_status = local_task->error_code;
...@@ -356,6 +354,11 @@ void mtapi_group_wait_any( ...@@ -356,6 +354,11 @@ void mtapi_group_wait_any(
local_status = MTAPI_ERR_NODE_NOTINIT; local_status = MTAPI_ERR_NODE_NOTINIT;
} }
/* store result */
if (MTAPI_NULL != result) {
*result = local_result;
}
mtapi_status_set(status, local_status); mtapi_status_set(status, local_status);
embb_mtapi_log_trace("mtapi_group_wait_any() returns\n"); embb_mtapi_log_trace("mtapi_group_wait_any() returns\n");
} }
......
...@@ -36,14 +36,19 @@ void embb_mtapi_id_pool_initialize( ...@@ -36,14 +36,19 @@ void embb_mtapi_id_pool_initialize(
mtapi_uint_t capacity) { mtapi_uint_t capacity) {
mtapi_uint_t ii; mtapi_uint_t ii;
that->capacity = capacity;
that->id_buffer = (mtapi_uint_t*) that->id_buffer = (mtapi_uint_t*)
embb_mtapi_alloc_allocate(sizeof(mtapi_uint_t)*(capacity + 1)); embb_mtapi_alloc_allocate(sizeof(mtapi_uint_t)*(capacity + 1));
that->id_buffer[0] = EMBB_MTAPI_IDPOOL_INVALID_ID; if (NULL != that->id_buffer) {
for (ii = 1; ii <= capacity; ii++) { that->capacity = capacity;
that->id_buffer[ii] = ii; that->id_buffer[0] = EMBB_MTAPI_IDPOOL_INVALID_ID;
for (ii = 1; ii <= capacity; ii++) {
that->id_buffer[ii] = ii;
}
that->ids_available = capacity;
} else {
that->capacity = 0;
that->ids_available = 0;
} }
that->ids_available = capacity;
that->put_id_position = 0; that->put_id_position = 0;
that->get_id_position = 1; that->get_id_position = 1;
embb_spin_init(&that->lock); embb_spin_init(&that->lock);
......
...@@ -41,6 +41,9 @@ ...@@ -41,6 +41,9 @@
mtapi_boolean_t embb_mtapi_job_initialize_list(embb_mtapi_node_t * node) { mtapi_boolean_t embb_mtapi_job_initialize_list(embb_mtapi_node_t * node) {
node->job_list = (embb_mtapi_job_t*)embb_mtapi_alloc_allocate( node->job_list = (embb_mtapi_job_t*)embb_mtapi_alloc_allocate(
sizeof(embb_mtapi_job_t)*(node->attributes.max_jobs + 1)); sizeof(embb_mtapi_job_t)*(node->attributes.max_jobs + 1));
if (NULL == node->job_list) {
return MTAPI_FALSE;
}
mtapi_uint_t ii; mtapi_uint_t ii;
for (ii = 0; ii <= node->attributes.max_jobs; ii++) { for (ii = 0; ii <= node->attributes.max_jobs; ii++) {
embb_mtapi_job_initialize( embb_mtapi_job_initialize(
...@@ -112,11 +115,15 @@ void embb_mtapi_job_initialize( ...@@ -112,11 +115,15 @@ void embb_mtapi_job_initialize(
that->domain_id = 0; that->domain_id = 0;
that->node_id = 0; that->node_id = 0;
that->num_actions = 0; that->num_actions = 0;
that->max_actions = max_actions;
that->actions = (mtapi_action_hndl_t*) that->actions = (mtapi_action_hndl_t*)
embb_mtapi_alloc_allocate(sizeof(mtapi_action_hndl_t)*max_actions); embb_mtapi_alloc_allocate(sizeof(mtapi_action_hndl_t)*max_actions);
for (ii = 0; ii < max_actions; ii++) { if (NULL != that->actions) {
that->actions[ii].id = EMBB_MTAPI_IDPOOL_INVALID_ID; that->max_actions = max_actions;
for (ii = 0; ii < max_actions; ii++) {
that->actions[ii].id = EMBB_MTAPI_IDPOOL_INVALID_ID;
}
} else {
that->max_actions = 0;
} }
} }
...@@ -159,7 +166,7 @@ void embb_mtapi_job_remove_action( ...@@ -159,7 +166,7 @@ void embb_mtapi_job_remove_action(
embb_mtapi_action_t * action) { embb_mtapi_action_t * action) {
assert(MTAPI_NULL != that); assert(MTAPI_NULL != that);
assert(MTAPI_NULL != action); assert(MTAPI_NULL != action);
mtapi_uint_t ii = 0; mtapi_uint_t ii;
for (ii = 0; ii + 1 < that->num_actions; ii++) { for (ii = 0; ii + 1 < that->num_actions; ii++) {
if (that->actions[ii].id == action->handle.id && if (that->actions[ii].id == action->handle.id &&
......
...@@ -117,35 +117,39 @@ void mtapi_initialize( ...@@ -117,35 +117,39 @@ void mtapi_initialize(
node->attributes.max_tasks); node->attributes.max_tasks);
node->queue_pool = embb_mtapi_queue_pool_new( node->queue_pool = embb_mtapi_queue_pool_new(
node->attributes.max_queues); node->attributes.max_queues);
if (MTAPI_NULL == node->job_list ||
MTAPI_NULL == node->action_pool ||
MTAPI_NULL == node->group_pool ||
MTAPI_NULL == node->task_pool ||
MTAPI_NULL == node->queue_pool) {
mtapi_finalize(NULL);
local_status = MTAPI_ERR_NODE_INITFAILED;
}
/* initialize scheduler for local node */ if (local_status == MTAPI_SUCCESS) {
node->scheduler = embb_mtapi_scheduler_new(); /* initialize scheduler for local node */
if (MTAPI_NULL != node->scheduler) { node->scheduler = embb_mtapi_scheduler_new();
/* fill information structure */ if (MTAPI_NULL != node->scheduler) {
/* fill information structure */
node->info.mtapi_version = 0x1000; // mtapi version 1.0 node->info.mtapi_version = 0x1000; // mtapi version 1.0
node->info.organization_id = MCA_ORG_ID_EMB; node->info.organization_id = MCA_ORG_ID_EMB;
node->info.implementation_version = node->info.implementation_version =
EMBB_BASE_VERSION_MAJOR * 0x1000 + EMBB_BASE_VERSION_MINOR; EMBB_BASE_VERSION_MAJOR * 0x1000 + EMBB_BASE_VERSION_MINOR;
node->info.number_of_domains = 1; node->info.number_of_domains = 1;
node->info.number_of_nodes = 1; node->info.number_of_nodes = 1;
node->info.hardware_concurrency = embb_core_count_available(); node->info.hardware_concurrency = embb_core_count_available();
node->info.used_memory = embb_mtapi_alloc_get_bytes_allocated(); node->info.used_memory = embb_mtapi_alloc_get_bytes_allocated();
if (MTAPI_NULL != mtapi_info) { if (MTAPI_NULL != mtapi_info) {
*mtapi_info = node->info; *mtapi_info = node->info;
} }
/* initialization succeeded, tell workers to start working */ /* initialization succeeded, tell workers to start working */
embb_atomic_store_int(&node->is_scheduler_running, MTAPI_TRUE); embb_atomic_store_int(&node->is_scheduler_running, MTAPI_TRUE);
} else {
if (MTAPI_SUCCESS != local_status) {
mtapi_finalize(MTAPI_NULL); mtapi_finalize(MTAPI_NULL);
local_status = MTAPI_ERR_NODE_INITFAILED; local_status = MTAPI_ERR_NODE_INITFAILED;
} }
} else {
mtapi_finalize(MTAPI_NULL);
local_status = MTAPI_ERR_NODE_INITFAILED;
} }
} else { } else {
embb_mtapi_alloc_deallocate(node); embb_mtapi_alloc_deallocate(node);
local_status = MTAPI_ERR_PARAMETER; local_status = MTAPI_ERR_PARAMETER;
...@@ -171,19 +175,29 @@ void mtapi_finalize(MTAPI_OUT mtapi_status_t* status) { ...@@ -171,19 +175,29 @@ void mtapi_finalize(MTAPI_OUT mtapi_status_t* status) {
} }
/* finalize storage in reverse order */ /* finalize storage in reverse order */
embb_mtapi_queue_pool_delete(node->queue_pool); if (MTAPI_NULL != node->queue_pool) {
node->queue_pool = MTAPI_NULL; embb_mtapi_queue_pool_delete(node->queue_pool);
node->queue_pool = MTAPI_NULL;
}
embb_mtapi_task_pool_delete(node->task_pool); if (MTAPI_NULL != node->task_pool) {
node->task_pool = MTAPI_NULL; embb_mtapi_task_pool_delete(node->task_pool);
node->task_pool = MTAPI_NULL;
}
embb_mtapi_group_pool_delete(node->group_pool); if (MTAPI_NULL != node->group_pool) {
node->group_pool = MTAPI_NULL; embb_mtapi_group_pool_delete(node->group_pool);
node->group_pool = MTAPI_NULL;
}
embb_mtapi_action_pool_delete(node->action_pool); if (MTAPI_NULL != node->action_pool) {
node->action_pool = MTAPI_NULL; embb_mtapi_action_pool_delete(node->action_pool);
node->action_pool = MTAPI_NULL;
}
embb_mtapi_job_finalize_list(node); if (MTAPI_NULL != node->job_list) {
embb_mtapi_job_finalize_list(node);
}
/* free system instance */ /* free system instance */
embb_mtapi_alloc_deallocate(node); embb_mtapi_alloc_deallocate(node);
......
...@@ -61,13 +61,18 @@ mtapi_boolean_t embb_mtapi_##TYPE##_pool_initialize( \ ...@@ -61,13 +61,18 @@ mtapi_boolean_t embb_mtapi_##TYPE##_pool_initialize( \
embb_mtapi_id_pool_initialize(&that->id_pool, capacity); \ embb_mtapi_id_pool_initialize(&that->id_pool, capacity); \
that->storage = (embb_mtapi_##TYPE##_t*)embb_mtapi_alloc_allocate( \ that->storage = (embb_mtapi_##TYPE##_t*)embb_mtapi_alloc_allocate( \
sizeof(embb_mtapi_##TYPE##_t)*(capacity + 1)); \ sizeof(embb_mtapi_##TYPE##_t)*(capacity + 1)); \
for (ii = 0; ii <= capacity; ii++) { \ if (NULL != that->storage) { \
that->storage[ii].handle.id = EMBB_MTAPI_IDPOOL_INVALID_ID; \ for (ii = 0; ii <= capacity; ii++) { \
that->storage[ii].handle.tag = 0; \ that->storage[ii].handle.id = EMBB_MTAPI_IDPOOL_INVALID_ID; \
that->storage[ii].handle.tag = 0; \
} \
/* use entry 0 as invalid */ \
embb_mtapi_##TYPE##_initialize(that->storage); \
return MTAPI_TRUE; \
} else { \
that->id_pool.ids_available = 0; \
return MTAPI_FALSE; \
} \ } \
/* use entry 0 as invalid */ \
embb_mtapi_##TYPE##_initialize(that->storage); \
return MTAPI_TRUE; \
} \ } \
\ \
void embb_mtapi_##TYPE##_pool_finalize(embb_mtapi_##TYPE##_pool_t * that) { \ void embb_mtapi_##TYPE##_pool_finalize(embb_mtapi_##TYPE##_pool_t * that) { \
......
...@@ -325,7 +325,7 @@ mtapi_queue_hndl_t mtapi_queue_get( ...@@ -325,7 +325,7 @@ mtapi_queue_hndl_t mtapi_queue_get(
if (embb_mtapi_node_is_initialized()) { if (embb_mtapi_node_is_initialized()) {
embb_mtapi_node_t* node = embb_mtapi_node_get_instance(); embb_mtapi_node_t* node = embb_mtapi_node_get_instance();
mtapi_uint_t ii = 0; mtapi_uint_t ii;
local_status = MTAPI_ERR_QUEUE_INVALID; local_status = MTAPI_ERR_QUEUE_INVALID;
for (ii = 0; ii < node->attributes.max_queues; ii++) { for (ii = 0; ii < node->attributes.max_queues; ii++) {
......
...@@ -78,8 +78,7 @@ embb_mtapi_task_t * embb_mtapi_scheduler_get_next_task_vhpf( ...@@ -78,8 +78,7 @@ embb_mtapi_task_t * embb_mtapi_scheduler_get_next_task_vhpf(
embb_mtapi_node_t * node, embb_mtapi_node_t * node,
embb_mtapi_thread_context_t * thread_context) { embb_mtapi_thread_context_t * thread_context) {
embb_mtapi_task_t * task = MTAPI_NULL; embb_mtapi_task_t * task = MTAPI_NULL;
mtapi_uint_t ii = 0; mtapi_uint_t ii;
mtapi_uint_t kk = 0;
assert(MTAPI_NULL != that); assert(MTAPI_NULL != that);
assert(MTAPI_NULL != node); assert(MTAPI_NULL != node);
...@@ -102,6 +101,7 @@ embb_mtapi_task_t * embb_mtapi_scheduler_get_next_task_vhpf( ...@@ -102,6 +101,7 @@ embb_mtapi_task_t * embb_mtapi_scheduler_get_next_task_vhpf(
*/ */
mtapi_uint_t context_index = mtapi_uint_t context_index =
(thread_context->worker_index + 1) % that->worker_count; (thread_context->worker_index + 1) % that->worker_count;
mtapi_uint_t kk;
for (kk = 0; for (kk = 0;
kk < that->worker_count - 1 && MTAPI_NULL == task; kk < that->worker_count - 1 && MTAPI_NULL == task;
kk++) { kk++) {
...@@ -121,8 +121,7 @@ embb_mtapi_task_t * embb_mtapi_scheduler_get_next_task_lf( ...@@ -121,8 +121,7 @@ embb_mtapi_task_t * embb_mtapi_scheduler_get_next_task_lf(
embb_mtapi_node_t * node, embb_mtapi_node_t * node,
embb_mtapi_thread_context_t * thread_context) { embb_mtapi_thread_context_t * thread_context) {
embb_mtapi_task_t * task = MTAPI_NULL; embb_mtapi_task_t * task = MTAPI_NULL;
mtapi_uint_t prio = 0; mtapi_uint_t prio;
mtapi_uint_t kk = 0;
assert(MTAPI_NULL != that); assert(MTAPI_NULL != that);
assert(MTAPI_NULL != node); assert(MTAPI_NULL != node);
...@@ -153,6 +152,7 @@ embb_mtapi_task_t * embb_mtapi_scheduler_get_next_task_lf( ...@@ -153,6 +152,7 @@ embb_mtapi_task_t * embb_mtapi_scheduler_get_next_task_lf(
prio++) { prio++) {
mtapi_uint_t context_index = mtapi_uint_t context_index =
(thread_context->worker_index + 1) % that->worker_count; (thread_context->worker_index + 1) % that->worker_count;
mtapi_uint_t kk;
for (kk = 0; for (kk = 0;
kk < that->worker_count - 1 && MTAPI_NULL == task; kk < that->worker_count - 1 && MTAPI_NULL == task;
kk++) { kk++) {
...@@ -195,7 +195,7 @@ embb_mtapi_task_t * embb_mtapi_scheduler_get_next_task( ...@@ -195,7 +195,7 @@ embb_mtapi_task_t * embb_mtapi_scheduler_get_next_task(
embb_mtapi_thread_context_t * embb_mtapi_scheduler_get_current_thread_context( embb_mtapi_thread_context_t * embb_mtapi_scheduler_get_current_thread_context(
embb_mtapi_scheduler_t * that) { embb_mtapi_scheduler_t * that) {
mtapi_uint_t ii = 0; mtapi_uint_t ii;
embb_mtapi_thread_context_t * context = NULL; embb_mtapi_thread_context_t * context = NULL;
assert(MTAPI_NULL != that); assert(MTAPI_NULL != that);
...@@ -434,7 +434,7 @@ mtapi_boolean_t embb_mtapi_scheduler_initialize_with_mode( ...@@ -434,7 +434,7 @@ mtapi_boolean_t embb_mtapi_scheduler_initialize_with_mode(
embb_mtapi_scheduler_t * that, embb_mtapi_scheduler_t * that,
embb_mtapi_scheduler_mode_t mode) { embb_mtapi_scheduler_mode_t mode) {
embb_mtapi_node_t* node = embb_mtapi_node_get_instance(); embb_mtapi_node_t* node = embb_mtapi_node_get_instance();
mtapi_uint_t ii = 0; mtapi_uint_t ii;
embb_mtapi_log_trace("embb_mtapi_scheduler_initialize() called\n"); embb_mtapi_log_trace("embb_mtapi_scheduler_initialize() called\n");
...@@ -456,6 +456,10 @@ mtapi_boolean_t embb_mtapi_scheduler_initialize_with_mode( ...@@ -456,6 +456,10 @@ mtapi_boolean_t embb_mtapi_scheduler_initialize_with_mode(
that->worker_contexts = (embb_mtapi_thread_context_t*) that->worker_contexts = (embb_mtapi_thread_context_t*)
embb_mtapi_alloc_allocate( embb_mtapi_alloc_allocate(
sizeof(embb_mtapi_thread_context_t)*that->worker_count); sizeof(embb_mtapi_thread_context_t)*that->worker_count);
if (NULL == that->worker_contexts) {
return MTAPI_FALSE;
}
mtapi_boolean_t isinit = MTAPI_TRUE;
for (ii = 0; ii < that->worker_count; ii++) { for (ii = 0; ii < that->worker_count; ii++) {
unsigned int core_num = 0; unsigned int core_num = 0;
mtapi_uint_t ll = 0; mtapi_uint_t ll = 0;
...@@ -467,9 +471,12 @@ mtapi_boolean_t embb_mtapi_scheduler_initialize_with_mode( ...@@ -467,9 +471,12 @@ mtapi_boolean_t embb_mtapi_scheduler_initialize_with_mode(
} }
core_num++; core_num++;
} }
embb_mtapi_thread_context_initialize_with_node_worker_and_core( isinit &= embb_mtapi_thread_context_initialize_with_node_worker_and_core(
&that->worker_contexts[ii], node, ii, core_num); &that->worker_contexts[ii], node, ii, core_num);
} }
if (!isinit) {
return MTAPI_FALSE;
}
for (ii = 0; ii < that->worker_count; ii++) { for (ii = 0; ii < that->worker_count; ii++) {
if (MTAPI_FALSE == embb_mtapi_thread_context_start( if (MTAPI_FALSE == embb_mtapi_thread_context_start(
&that->worker_contexts[ii], that)) { &that->worker_contexts[ii], that)) {
...@@ -481,22 +488,24 @@ mtapi_boolean_t embb_mtapi_scheduler_initialize_with_mode( ...@@ -481,22 +488,24 @@ mtapi_boolean_t embb_mtapi_scheduler_initialize_with_mode(
} }
void embb_mtapi_scheduler_finalize(embb_mtapi_scheduler_t * that) { void embb_mtapi_scheduler_finalize(embb_mtapi_scheduler_t * that) {
mtapi_uint_t ii = 0; mtapi_uint_t ii;
embb_mtapi_log_trace("embb_mtapi_scheduler_finalize() called\n"); embb_mtapi_log_trace("embb_mtapi_scheduler_finalize() called\n");
assert(MTAPI_NULL != that); assert(MTAPI_NULL != that);
/* finalize all workers */ if (MTAPI_NULL != that->worker_contexts) {
for (ii = 0; ii < that->worker_count; ii++) { /* finalize all workers */
embb_mtapi_thread_context_stop(&that->worker_contexts[ii]); for (ii = 0; ii < that->worker_count; ii++) {
} embb_mtapi_thread_context_stop(&that->worker_contexts[ii]);
for (ii = 0; ii < that->worker_count; ii++) { }
embb_mtapi_thread_context_finalize(&that->worker_contexts[ii]); for (ii = 0; ii < that->worker_count; ii++) {
} embb_mtapi_thread_context_finalize(&that->worker_contexts[ii]);
}
that->worker_count = 0; that->worker_count = 0;
embb_mtapi_alloc_deallocate(that->worker_contexts); embb_mtapi_alloc_deallocate(that->worker_contexts);
that->worker_contexts = MTAPI_NULL; that->worker_contexts = MTAPI_NULL;
}
} }
embb_mtapi_scheduler_t * embb_mtapi_scheduler_new() { embb_mtapi_scheduler_t * embb_mtapi_scheduler_new() {
...@@ -506,6 +515,7 @@ embb_mtapi_scheduler_t * embb_mtapi_scheduler_new() { ...@@ -506,6 +515,7 @@ embb_mtapi_scheduler_t * embb_mtapi_scheduler_new() {
if (MTAPI_NULL != that) { if (MTAPI_NULL != that) {
if (MTAPI_FALSE == embb_mtapi_scheduler_initialize(that)) { if (MTAPI_FALSE == embb_mtapi_scheduler_initialize(that)) {
/* on error delete and return MTAPI_NULL */ /* on error delete and return MTAPI_NULL */
embb_mtapi_scheduler_finalize(that);
embb_mtapi_scheduler_delete(that); embb_mtapi_scheduler_delete(that);
return MTAPI_NULL; return MTAPI_NULL;
} }
......
...@@ -38,12 +38,13 @@ ...@@ -38,12 +38,13 @@
/* ---- CLASS MEMBERS ------------------------------------------------------ */ /* ---- CLASS MEMBERS ------------------------------------------------------ */
void embb_mtapi_thread_context_initialize_with_node_worker_and_core( mtapi_boolean_t embb_mtapi_thread_context_initialize_with_node_worker_and_core(
embb_mtapi_thread_context_t* that, embb_mtapi_thread_context_t* that,
embb_mtapi_node_t* node, embb_mtapi_node_t* node,
mtapi_uint_t worker_index, mtapi_uint_t worker_index,
mtapi_uint_t core_num) { mtapi_uint_t core_num) {
mtapi_uint_t ii; mtapi_uint_t ii;
mtapi_boolean_t result = MTAPI_TRUE;
assert(MTAPI_NULL != that); assert(MTAPI_NULL != that);
assert(MTAPI_NULL != node); assert(MTAPI_NULL != node);
...@@ -52,25 +53,55 @@ void embb_mtapi_thread_context_initialize_with_node_worker_and_core( ...@@ -52,25 +53,55 @@ void embb_mtapi_thread_context_initialize_with_node_worker_and_core(
that->worker_index = worker_index; that->worker_index = worker_index;
that->core_num = core_num; that->core_num = core_num;
that->priorities = node->attributes.max_priorities; that->priorities = node->attributes.max_priorities;
that->is_initialized = MTAPI_FALSE;
embb_atomic_store_int(&that->run, 0); embb_atomic_store_int(&that->run, 0);
that->queue = (embb_mtapi_task_queue_t**)embb_mtapi_alloc_allocate( that->queue = (embb_mtapi_task_queue_t**)embb_mtapi_alloc_allocate(
sizeof(embb_mtapi_task_queue_t)*that->priorities); sizeof(embb_mtapi_task_queue_t)*that->priorities);
that->private_queue = (embb_mtapi_task_queue_t**)embb_mtapi_alloc_allocate( if (that->queue == NULL) {
sizeof(embb_mtapi_task_queue_t)*that->priorities); that->private_queue = NULL;
return MTAPI_FALSE;
}
for (ii = 0; ii < that->priorities; ii++) { for (ii = 0; ii < that->priorities; ii++) {
that->queue[ii] = (embb_mtapi_task_queue_t*) that->queue[ii] = (embb_mtapi_task_queue_t*)
embb_mtapi_alloc_allocate(sizeof(embb_mtapi_task_queue_t)); embb_mtapi_alloc_allocate(sizeof(embb_mtapi_task_queue_t));
embb_mtapi_task_queue_initialize_with_capacity( if (that->queue[ii] != NULL) {
that->queue[ii], node->attributes.queue_limit); embb_mtapi_task_queue_initialize_with_capacity(
that->queue[ii], node->attributes.queue_limit);
} else {
result = MTAPI_FALSE;
}
}
if (!result) {
return MTAPI_FALSE;
}
that->private_queue = (embb_mtapi_task_queue_t**)embb_mtapi_alloc_allocate(
sizeof(embb_mtapi_task_queue_t)*that->priorities);
if (that->private_queue == NULL) {
return MTAPI_FALSE;
}
for (ii = 0; ii < that->priorities; ii++) {
that->private_queue[ii] = (embb_mtapi_task_queue_t*) that->private_queue[ii] = (embb_mtapi_task_queue_t*)
embb_mtapi_alloc_allocate(sizeof(embb_mtapi_task_queue_t)); embb_mtapi_alloc_allocate(sizeof(embb_mtapi_task_queue_t));
embb_mtapi_task_queue_initialize_with_capacity( if (that->private_queue[ii] != NULL) {
that->private_queue[ii], node->attributes.queue_limit); embb_mtapi_task_queue_initialize_with_capacity(
that->private_queue[ii], node->attributes.queue_limit);
} else {
result = MTAPI_FALSE;
}
}
if (!result) {
return MTAPI_FALSE;
} }
embb_mutex_init(&that->work_available_mutex, EMBB_MUTEX_PLAIN); embb_mutex_init(&that->work_available_mutex, EMBB_MUTEX_PLAIN);
embb_condition_init(&that->work_available); embb_condition_init(&that->work_available);
embb_atomic_store_int(&that->is_sleeping, 0); embb_atomic_store_int(&that->is_sleeping, 0);
that->is_initialized = MTAPI_TRUE;
return MTAPI_TRUE;
} }
mtapi_boolean_t embb_mtapi_thread_context_start( mtapi_boolean_t embb_mtapi_thread_context_start(
...@@ -126,22 +157,37 @@ void embb_mtapi_thread_context_finalize(embb_mtapi_thread_context_t* that) { ...@@ -126,22 +157,37 @@ void embb_mtapi_thread_context_finalize(embb_mtapi_thread_context_t* that) {
embb_mtapi_log_trace("embb_mtapi_thread_context_finalize() called\n"); embb_mtapi_log_trace("embb_mtapi_thread_context_finalize() called\n");
embb_condition_destroy(&that->work_available); if (that->is_initialized) {
embb_mutex_destroy(&that->work_available_mutex); embb_condition_destroy(&that->work_available);
embb_mutex_destroy(&that->work_available_mutex);
}
for (ii = 0; ii < that->priorities; ii++) { if (that->queue != NULL) {
embb_mtapi_task_queue_finalize(that->queue[ii]); for (ii = 0; ii < that->priorities; ii++) {
embb_mtapi_alloc_deallocate(that->queue[ii]); if (that->queue[ii] != NULL) {
that->queue[ii] = MTAPI_NULL; embb_mtapi_task_queue_finalize(that->queue[ii]);
embb_mtapi_task_queue_finalize(that->private_queue[ii]); embb_mtapi_alloc_deallocate(that->queue[ii]);
embb_mtapi_alloc_deallocate(that->private_queue[ii]); that->queue[ii] = MTAPI_NULL;
that->private_queue[ii] = MTAPI_NULL; }
}
embb_mtapi_alloc_deallocate(that->queue);
that->queue = MTAPI_NULL;
} }
embb_mtapi_alloc_deallocate(that->queue);
that->queue = MTAPI_NULL; if (that->private_queue != NULL) {
embb_mtapi_alloc_deallocate(that->private_queue); for (ii = 0; ii < that->priorities; ii++) {
that->private_queue = MTAPI_NULL; if (that->private_queue[ii] != NULL) {
embb_mtapi_task_queue_finalize(that->private_queue[ii]);
embb_mtapi_alloc_deallocate(that->private_queue[ii]);
that->private_queue[ii] = MTAPI_NULL;
}
}
embb_mtapi_alloc_deallocate(that->private_queue);
that->private_queue = MTAPI_NULL;
}
that->priorities = 0; that->priorities = 0;
that->is_initialized = MTAPI_FALSE;
that->node = MTAPI_NULL; that->node = MTAPI_NULL;
} }
......
...@@ -67,6 +67,7 @@ struct embb_mtapi_thread_context_struct { ...@@ -67,6 +67,7 @@ struct embb_mtapi_thread_context_struct {
mtapi_uint_t core_num; mtapi_uint_t core_num;
embb_atomic_int run; embb_atomic_int run;
mtapi_status_t status; mtapi_status_t status;
mtapi_boolean_t is_initialized;
}; };
#include <embb_mtapi_thread_context_t_fwd.h> #include <embb_mtapi_thread_context_t_fwd.h>
...@@ -74,8 +75,9 @@ struct embb_mtapi_thread_context_struct { ...@@ -74,8 +75,9 @@ struct embb_mtapi_thread_context_struct {
/** /**
* Constructor using attributes from node and a given core number. * Constructor using attributes from node and a given core number.
* \memberof embb_mtapi_thread_context_struct * \memberof embb_mtapi_thread_context_struct
* \returns MTAPI_TRUE if successful, MTAPI_FALSE on error
*/ */
void embb_mtapi_thread_context_initialize_with_node_worker_and_core( mtapi_boolean_t embb_mtapi_thread_context_initialize_with_node_worker_and_core(
embb_mtapi_thread_context_t* that, embb_mtapi_thread_context_t* that,
embb_mtapi_node_t* node, embb_mtapi_node_t* node,
mtapi_uint_t worker_index, mtapi_uint_t worker_index,
......
...@@ -107,6 +107,7 @@ int embb_mtapi_network_buffer_pop_front_int8( ...@@ -107,6 +107,7 @@ int embb_mtapi_network_buffer_pop_front_int8(
embb_mtapi_network_buffer_t * that, embb_mtapi_network_buffer_t * that,
int8_t * value) { int8_t * value) {
if (that->position + 1 > that->size) { if (that->position + 1 > that->size) {
*value = 0;
return 0; return 0;
} }
memcpy(value, that->data + that->position, 1); memcpy(value, that->data + that->position, 1);
...@@ -118,6 +119,7 @@ int embb_mtapi_network_buffer_pop_front_int16( ...@@ -118,6 +119,7 @@ int embb_mtapi_network_buffer_pop_front_int16(
embb_mtapi_network_buffer_t * that, embb_mtapi_network_buffer_t * that,
int16_t * value) { int16_t * value) {
if (that->position + 2 > that->size) { if (that->position + 2 > that->size) {
*value = 0;
return 0; return 0;
} }
memcpy(value, that->data + that->position, 2); memcpy(value, that->data + that->position, 2);
...@@ -129,6 +131,7 @@ int embb_mtapi_network_buffer_pop_front_int32( ...@@ -129,6 +131,7 @@ int embb_mtapi_network_buffer_pop_front_int32(
embb_mtapi_network_buffer_t * that, embb_mtapi_network_buffer_t * that,
int32_t * value) { int32_t * value) {
if (that->position + 4 > that->size) { if (that->position + 4 > that->size) {
*value = 0;
return 0; return 0;
} }
memcpy(value, that->data + that->position, 4); memcpy(value, that->data + that->position, 4);
...@@ -141,6 +144,7 @@ int embb_mtapi_network_buffer_pop_front_rawdata( ...@@ -141,6 +144,7 @@ int embb_mtapi_network_buffer_pop_front_rawdata(
int32_t size, int32_t size,
void * rawdata) { void * rawdata) {
if (that->position + size > that->size) { if (that->position + size > that->size) {
memset(rawdata, 0, (size_t)size);
return 0; return 0;
} }
memcpy(rawdata, that->data + that->position, (size_t)size); memcpy(rawdata, that->data + that->position, (size_t)size);
......
...@@ -71,13 +71,6 @@ int embb_mtapi_network_socket_bind_and_listen( ...@@ -71,13 +71,6 @@ int embb_mtapi_network_socket_bind_and_listen(
uint16_t port, uint16_t port,
uint16_t max_connections) { uint16_t max_connections) {
struct sockaddr_in in_addr; struct sockaddr_in in_addr;
int reuseaddr_on = 1;
// addr reuse
if (SOCKET_ERROR == setsockopt(that->handle, SOL_SOCKET, SO_REUSEADDR,
(const char *)&reuseaddr_on, sizeof(reuseaddr_on))) {
return 0;
}
// bind & listen // bind & listen
memset(&in_addr, 0, sizeof(in_addr)); memset(&in_addr, 0, sizeof(in_addr));
......
...@@ -126,7 +126,7 @@ void Node::Initialize( ...@@ -126,7 +126,7 @@ void Node::Initialize(
mtapi_nodeattr_set(&attr, MTAPI_NODE_MAX_ACTIONS, mtapi_nodeattr_set(&attr, MTAPI_NODE_MAX_ACTIONS,
&tmp, sizeof(tmp), &status); &tmp, sizeof(tmp), &status);
assert(MTAPI_SUCCESS == status); assert(MTAPI_SUCCESS == status);
tmp = 4; // tmp = 4;
mtapi_nodeattr_set(&attr, MTAPI_NODE_MAX_JOBS, mtapi_nodeattr_set(&attr, MTAPI_NODE_MAX_JOBS,
&tmp, sizeof(tmp), &status); &tmp, sizeof(tmp), &status);
assert(MTAPI_SUCCESS == status); assert(MTAPI_SUCCESS == status);
......
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