Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- diff --git a/lib/posix/options/pthread.c b/lib/posix/options/pthread.c
- index bda8624f192..a3a959d5c80 100644
- --- a/lib/posix/options/pthread.c
- +++ b/lib/posix/options/pthread.c
- @@ -638,18 +638,11 @@ int pthread_create(pthread_t *th, const pthread_attr_t *_attr, void *(*threadrou
- t->attr = *(struct posix_thread_attr *)_attr;
- }
- - struct posix_thread *self = NULL;
- + if (t->attr.inheritsched == PTHREAD_INHERIT_SCHED) {
- + int pol;
- - K_SPINLOCK(&pthread_pool_lock) {
- - self = to_posix_thread(pthread_self());
- - if (self == NULL) {
- - K_SPINLOCK_BREAK;
- - }
- - if (self->attr.inheritsched == PTHREAD_INHERIT_SCHED) {
- - t->attr.priority = self->attr.priority;
- - t->attr.schedpolicy = self->attr.schedpolicy;
- - t->attr.inheritsched = self->attr.inheritsched;
- - }
- + t->attr.priority = zephyr_to_posix_priority(k_thread_priority_get(k_current_get()), &pol);
- + t->attr.schedpolicy = pol;
- }
- /* spawn the thread */
- diff --git a/tests/posix/common/src/pthread_attr.c b/tests/posix/common/src/pthread_attr.c
- index 5f2e363581a..a8ac914d7ef 100644
- --- a/tests/posix/common/src/pthread_attr.c
- +++ b/tests/posix/common/src/pthread_attr.c
- @@ -33,7 +33,8 @@ static void *thread_entry(void *arg)
- return NULL;
- }
- -static void create_thread_common(const pthread_attr_t *attrp, bool expect_success, bool joinable)
- +static void create_thread_common_entry(const pthread_attr_t *attrp, bool expect_success, bool joinable, void* (*entry)(void *arg), void *arg)
- +
- {
- pthread_t th;
- @@ -42,9 +43,9 @@ static void create_thread_common(const pthread_attr_t *attrp, bool expect_succes
- }
- if (expect_success) {
- - zassert_ok(pthread_create(&th, attrp, thread_entry, UINT_TO_POINTER(joinable)));
- + zassert_ok(pthread_create(&th, attrp, entry, arg));
- } else {
- - zassert_not_ok(pthread_create(&th, attrp, thread_entry, UINT_TO_POINTER(joinable)));
- + zassert_not_ok(pthread_create(&th, attrp, entry, arg));
- return;
- }
- @@ -66,6 +67,11 @@ static void create_thread_common(const pthread_attr_t *attrp, bool expect_succes
- zassert_true(detached_thread_has_finished, "detached thread did not seem to finish");
- }
- +static void create_thread_common(const pthread_attr_t *attrp, bool expect_success, bool joinable)
- +{
- + create_thread_common_entry(attrp, expect_success, joinable, thread_entry, UINT_TO_POINTER(joinable));
- +}
- +
- static inline void can_create_thread(const pthread_attr_t *attrp)
- {
- create_thread_common(attrp, true, true);
- @@ -501,97 +507,73 @@ ZTEST(pthread_attr, test_pthread_attr_getinheritsched)
- zassert_equal(inheritsched, PTHREAD_INHERIT_SCHED);
- }
- -static int setinheritsched_inheritsched;
- -
- -static void *test_pthread_attr_set_inheritsched_child_fn(void *arg)
- +static void *inheritsched_entry(void *arg)
- {
- - ARG_UNUSED(arg);
- -
- - struct sched_param param = {
- - .sched_priority = 1,
- - };
- - int policy = SCHED_INVALID;
- -
- - int getChildPolicy = SCHED_INVALID;
- - struct sched_param getChildParam = {
- - .sched_priority = 3,
- - };
- + int prio;
- + bool inheritsched = (bool)POINTER_TO_UINT(arg);
- - pthread_t self = pthread_self();
- + prio = k_thread_priority_get(k_current_get());
- + zassert_ok(pthread_getschedparam(pthread_self(), &policy, ¶m));
- - zassert_ok(pthread_getschedparam(self, &getChildPolicy, &getChildParam));
- - zassert_ok(pthread_attr_getschedpolicy(&attr, &policy));
- - zassert_ok(pthread_attr_getschedparam(&attr, ¶m));
- + /*
- + * There may be numerical overlap between posix priorities in different scheduler policies so
- + * only check the Zephyr priority here. The posix policy and posix priority are derived from
- + * the Zephyr priority in any case.
- + */
- - if (setinheritsched_inheritsched == PTHREAD_INHERIT_SCHED) {
- - zassert_equal(getChildPolicy, policy);
- - zassert_equal(getChildParam.sched_priority, param.sched_priority);
- + if (inheritsched == PTHREAD_INHERIT_SCHED) {
- + zassert_not_equal(prio, K_LOWEST_APPLICATION_THREAD_PRIO);
- + } else {
- + zassert_equal(prio, K_LOWEST_APPLICATION_THREAD_PRIO);
- }
- return NULL;
- }
- -static void *test_pthread_attr_set_inheritsched_parent_fn(void *arg)
- +static void test_pthread_attr_setinheritsched_common(bool inheritsched)
- {
- - ARG_UNUSED(arg);
- -
- - pthread_t child;
- -
- - zassert_ok(pthread_create(&child, NULL,
- - test_pthread_attr_set_inheritsched_child_fn, NULL));
- - zassert_ok(pthread_join(child, NULL));
- -
- - return NULL;
- -}
- + int prio;
- + int policy;
- + struct sched_param param;
- -static void test_pthread_attr_set_inheritsched_common(int inheritsched)
- -{
- - int getinheritsched = BIOS_FOOD;
- - struct sched_param param = {
- - .sched_priority = 2,
- - };
- - int policy = SCHED_RR;
- + extern int zephyr_to_posix_priority(int priority, int *policy);
- - setinheritsched_inheritsched = inheritsched;
- + prio = k_thread_priority_get(k_current_get());
- + zassert_not_equal(prio, K_LOWEST_APPLICATION_THREAD_PRIO);
- - /* Set inheritsched attribute */
- - zassert_ok(pthread_attr_setinheritsched(&attr, inheritsched));
- - zassert_ok(pthread_attr_getinheritsched(&attr, &getinheritsched));
- - zassert_equal(getinheritsched, inheritsched);
- + /*
- + * values affected by inheritsched are policy / priority / contentionscope
- + *
- + * we only support PTHREAD_SCOPE_SYSTEM, so no need to set contentionscope
- + */
- + prio = K_LOWEST_APPLICATION_THREAD_PRIO;
- + param.sched_priority = zephyr_to_posix_priority(prio, &policy);
- - /* Change priority of thread */
- - zassert_ok(pthread_attr_setschedparam(&attr, ¶m));
- zassert_ok(pthread_attr_setschedpolicy(&attr, policy));
- + zassert_ok(pthread_attr_setschedparam(&attr, ¶m));
- - pthread_t parent;
- -
- - zassert_ok(pthread_create(&parent, &attr,
- - test_pthread_attr_set_inheritsched_parent_fn, NULL));
- - zassert_ok(pthread_join(parent, NULL));
- + zassert_ok(pthread_attr_setinheritsched(&attr, inheritsched));
- + create_thread_common_entry(&attr, true, true, inheritsched_entry, UINT_TO_POINTER(inheritsched));
- }
- ZTEST(pthread_attr, test_pthread_attr_setinheritsched)
- {
- - int inheritsched = BIOS_FOOD;
- -
- /* degenerate cases */
- {
- if (false) {
- /* undefined behaviour */
- zassert_equal(pthread_attr_setinheritsched(NULL, PTHREAD_EXPLICIT_SCHED),
- EINVAL);
- - zassert_equal(pthread_attr_setinheritsched(NULL, inheritsched), EINVAL);
- + zassert_equal(pthread_attr_setinheritsched(NULL, PTHREAD_INHERIT_SCHED), EINVAL);
- zassert_equal(pthread_attr_setinheritsched((pthread_attr_t *)&uninit_attr,
- - inheritsched), EINVAL);
- + PTHREAD_INHERIT_SCHED), EINVAL);
- }
- zassert_equal(pthread_attr_setinheritsched(&attr, 3), EINVAL);
- }
- - zassert_ok(pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED));
- - zassert_ok(pthread_attr_getinheritsched(&attr, &inheritsched));
- - zassert_equal(inheritsched, PTHREAD_EXPLICIT_SCHED);
- -
- - test_pthread_attr_set_inheritsched_common(PTHREAD_INHERIT_SCHED);
- + /* valid cases */
- + test_pthread_attr_setinheritsched_common(PTHREAD_INHERIT_SCHED);
- + test_pthread_attr_setinheritsched_common(PTHREAD_EXPLICIT_SCHED);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement