From 2ecaee071cbc22ebeb1862177601fb1e856d8a77 Mon Sep 17 00:00:00 2001 From: Niranjhana N Date: Wed, 7 Mar 2018 19:11:53 +0530 Subject: [PATCH] tests: posix: Fixing pthread attr object initialization. In some cases pthread attrib initialization may fail as garbare value may indicate that this attribute is already initialized. Destroy the attr object and recreate it. Signed-off-by: Niranjhana N --- tests/posix/pthread/src/pthread.c | 9 ++++++--- tests/posix/pthread_equal/src/pthread_equal.c | 11 +++++++++-- tests/posix/pthread_join/src/pthread.c | 8 +++++++- tests/posix/semaphore/src/sem.c | 10 ++++++++-- 4 files changed, 30 insertions(+), 8 deletions(-) diff --git a/tests/posix/pthread/src/pthread.c b/tests/posix/pthread/src/pthread.c index b9fc44d6c69..060f4300bbb 100644 --- a/tests/posix/pthread/src/pthread.c +++ b/tests/posix/pthread/src/pthread.c @@ -197,9 +197,12 @@ void test_pthread(void) for (i = 0; i < N_THR; i++) { ret = pthread_attr_init(&attr[i]); - - /*TESTPOINT: Check if thread attr init is done*/ - zassert_false(ret, "Thread attribute initialization failed\n"); + if (ret != 0) { + zassert_false(pthread_attr_destroy(&attr[i]), + "Unable to destroy pthread object attrib\n"); + zassert_false(pthread_attr_init(&attr[i]), + "Unable to create pthread object attrib\n"); + } pthread_attr_setstack(&attr[i], &stacks[i][0], STACKSZ); pthread_attr_setschedpolicy(&attr[i], schedpolicy); diff --git a/tests/posix/pthread_equal/src/pthread_equal.c b/tests/posix/pthread_equal/src/pthread_equal.c index a1e82592016..2fc57e635b0 100644 --- a/tests/posix/pthread_equal/src/pthread_equal.c +++ b/tests/posix/pthread_equal/src/pthread_equal.c @@ -22,12 +22,19 @@ void *thread_top(void *p1) void test_pthread_equal(void) { - int ret = 1; + int ret = -1; pthread_attr_t attr; struct sched_param schedparam; pthread_t newthread; - pthread_attr_init(&attr); + ret = pthread_attr_init(&attr); + if (ret != 0) { + zassert_false(pthread_attr_destroy(&attr), + "Unable to destroy pthread object attrib\n"); + zassert_false(pthread_attr_init(&attr), + "Unable to create pthread object attrib\n"); + } + schedparam.priority = 2; pthread_attr_setschedparam(&attr, &schedparam); pthread_attr_setstack(&attr, &stacks[0][0], STACKSZ); diff --git a/tests/posix/pthread_join/src/pthread.c b/tests/posix/pthread_join/src/pthread.c index f5da62be9fa..0de209d2d25 100644 --- a/tests/posix/pthread_join/src/pthread.c +++ b/tests/posix/pthread_join/src/pthread.c @@ -56,7 +56,13 @@ void test_pthread_join(void) printk("POSIX pthread join API\n"); /* Creating threads with lowest application priority */ for (i = 0; i < N_THR; i++) { - pthread_attr_init(&attr[i]); + ret = pthread_attr_init(&attr[i]); + if (ret != 0) { + zassert_false(pthread_attr_destroy(&attr[i]), + "Unable to destroy pthread object attrib\n"); + zassert_false(pthread_attr_init(&attr[i]), + "Unable to create pthread object attrib\n"); + } /* Setting pthread as JOINABLE */ pthread_attr_getdetachstate(&attr[i], &detachstate); diff --git a/tests/posix/semaphore/src/sem.c b/tests/posix/semaphore/src/sem.c index 43969ff19a1..0caa1048fcf 100644 --- a/tests/posix/semaphore/src/sem.c +++ b/tests/posix/semaphore/src/sem.c @@ -29,11 +29,17 @@ static void test_sema(void) pthread_attr_t attr; struct sched_param schedparam; int schedpolicy = SCHED_FIFO; - int val; + int val, ret; schedparam.priority = 1; + ret = pthread_attr_init(&attr); + if (ret != 0) { + zassert_false(pthread_attr_destroy(&attr), + "Unable to destroy pthread object attrib\n"); + zassert_false(pthread_attr_init(&attr), + "Unable to create pthread object attrib\n"); + } - pthread_attr_init(&attr); pthread_attr_setstack(&attr, &stack, STACK_SIZE); pthread_attr_setschedpolicy(&attr, schedpolicy); pthread_attr_setschedparam(&attr, &schedparam);