Advertisement
Guest User

Untitled

a guest
Mar 29th, 2024
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 7.00 KB | None | 0 0
  1. diff --git a/lib/posix/options/pthread.c b/lib/posix/options/pthread.c
  2. index bda8624f192..a3a959d5c80 100644
  3. --- a/lib/posix/options/pthread.c
  4. +++ b/lib/posix/options/pthread.c
  5. @@ -638,18 +638,11 @@ int pthread_create(pthread_t *th, const pthread_attr_t *_attr, void *(*threadrou
  6.         t->attr = *(struct posix_thread_attr *)_attr;
  7.     }
  8.  
  9. -   struct posix_thread *self = NULL;
  10. +   if (t->attr.inheritsched == PTHREAD_INHERIT_SCHED) {
  11. +       int pol;
  12.  
  13. -   K_SPINLOCK(&pthread_pool_lock) {
  14. -       self = to_posix_thread(pthread_self());
  15. -       if (self == NULL) {
  16. -           K_SPINLOCK_BREAK;
  17. -       }
  18. -       if (self->attr.inheritsched == PTHREAD_INHERIT_SCHED) {
  19. -           t->attr.priority = self->attr.priority;
  20. -           t->attr.schedpolicy = self->attr.schedpolicy;
  21. -           t->attr.inheritsched = self->attr.inheritsched;
  22. -       }
  23. +       t->attr.priority = zephyr_to_posix_priority(k_thread_priority_get(k_current_get()), &pol);
  24. +       t->attr.schedpolicy = pol;
  25.     }
  26.  
  27.     /* spawn the thread */
  28. diff --git a/tests/posix/common/src/pthread_attr.c b/tests/posix/common/src/pthread_attr.c
  29. index 5f2e363581a..a8ac914d7ef 100644
  30. --- a/tests/posix/common/src/pthread_attr.c
  31. +++ b/tests/posix/common/src/pthread_attr.c
  32. @@ -33,7 +33,8 @@ static void *thread_entry(void *arg)
  33.     return NULL;
  34.  }
  35.  
  36. -static void create_thread_common(const pthread_attr_t *attrp, bool expect_success, bool joinable)
  37. +static void create_thread_common_entry(const pthread_attr_t *attrp, bool expect_success, bool joinable, void* (*entry)(void *arg), void *arg)
  38. +
  39.  {
  40.     pthread_t th;
  41.  
  42. @@ -42,9 +43,9 @@ static void create_thread_common(const pthread_attr_t *attrp, bool expect_succes
  43.     }
  44.  
  45.     if (expect_success) {
  46. -       zassert_ok(pthread_create(&th, attrp, thread_entry, UINT_TO_POINTER(joinable)));
  47. +       zassert_ok(pthread_create(&th, attrp, entry, arg));
  48.     } else {
  49. -       zassert_not_ok(pthread_create(&th, attrp, thread_entry, UINT_TO_POINTER(joinable)));
  50. +       zassert_not_ok(pthread_create(&th, attrp, entry, arg));
  51.         return;
  52.     }
  53.  
  54. @@ -66,6 +67,11 @@ static void create_thread_common(const pthread_attr_t *attrp, bool expect_succes
  55.     zassert_true(detached_thread_has_finished, "detached thread did not seem to finish");
  56.  }
  57.  
  58. +static void create_thread_common(const pthread_attr_t *attrp, bool expect_success, bool joinable)
  59. +{
  60. +   create_thread_common_entry(attrp, expect_success, joinable, thread_entry, UINT_TO_POINTER(joinable));
  61. +}
  62. +
  63.  static inline void can_create_thread(const pthread_attr_t *attrp)
  64.  {
  65.     create_thread_common(attrp, true, true);
  66. @@ -501,97 +507,73 @@ ZTEST(pthread_attr, test_pthread_attr_getinheritsched)
  67.     zassert_equal(inheritsched, PTHREAD_INHERIT_SCHED);
  68.  }
  69.  
  70. -static int setinheritsched_inheritsched;
  71. -
  72. -static void *test_pthread_attr_set_inheritsched_child_fn(void *arg)
  73. +static void *inheritsched_entry(void *arg)
  74.  {
  75. -   ARG_UNUSED(arg);
  76. -
  77. -   struct sched_param param = {
  78. -       .sched_priority = 1,
  79. -   };
  80. -   int policy = SCHED_INVALID;
  81. -
  82. -   int getChildPolicy = SCHED_INVALID;
  83. -   struct sched_param getChildParam = {
  84. -       .sched_priority = 3,
  85. -   };
  86. +   int prio;
  87. +   bool inheritsched = (bool)POINTER_TO_UINT(arg);
  88.  
  89. -   pthread_t self = pthread_self();
  90. +   prio = k_thread_priority_get(k_current_get());
  91. +   zassert_ok(pthread_getschedparam(pthread_self(), &policy, &param));
  92.  
  93. -   zassert_ok(pthread_getschedparam(self, &getChildPolicy, &getChildParam));
  94. -   zassert_ok(pthread_attr_getschedpolicy(&attr, &policy));
  95. -   zassert_ok(pthread_attr_getschedparam(&attr, &param));
  96. +   /*
  97. +    * There may be numerical overlap between posix priorities in different scheduler policies so
  98. +    * only check the Zephyr priority here. The posix policy and posix priority are derived from
  99. +    * the Zephyr priority in any case.
  100. +    */
  101.  
  102. -   if (setinheritsched_inheritsched == PTHREAD_INHERIT_SCHED) {
  103. -       zassert_equal(getChildPolicy, policy);
  104. -       zassert_equal(getChildParam.sched_priority, param.sched_priority);
  105. +   if (inheritsched == PTHREAD_INHERIT_SCHED) {
  106. +       zassert_not_equal(prio, K_LOWEST_APPLICATION_THREAD_PRIO);
  107. +   } else {
  108. +       zassert_equal(prio, K_LOWEST_APPLICATION_THREAD_PRIO);
  109.     }
  110.  
  111.     return NULL;
  112.  }
  113.  
  114. -static void *test_pthread_attr_set_inheritsched_parent_fn(void *arg)
  115. +static void test_pthread_attr_setinheritsched_common(bool inheritsched)
  116.  {
  117. -   ARG_UNUSED(arg);
  118. -
  119. -   pthread_t child;
  120. -
  121. -   zassert_ok(pthread_create(&child, NULL,
  122. -                     test_pthread_attr_set_inheritsched_child_fn, NULL));
  123. -   zassert_ok(pthread_join(child, NULL));
  124. -
  125. -   return NULL;
  126. -}
  127. +   int prio;
  128. +   int policy;
  129. +   struct sched_param param;
  130.  
  131. -static void test_pthread_attr_set_inheritsched_common(int inheritsched)
  132. -{
  133. -   int getinheritsched = BIOS_FOOD;
  134. -   struct sched_param param = {
  135. -       .sched_priority = 2,
  136. -   };
  137. -   int policy = SCHED_RR;
  138. +   extern int zephyr_to_posix_priority(int priority, int *policy);
  139.  
  140. -   setinheritsched_inheritsched = inheritsched;
  141. +   prio = k_thread_priority_get(k_current_get());
  142. +   zassert_not_equal(prio, K_LOWEST_APPLICATION_THREAD_PRIO);
  143.  
  144. -   /* Set inheritsched attribute */
  145. -   zassert_ok(pthread_attr_setinheritsched(&attr, inheritsched));
  146. -   zassert_ok(pthread_attr_getinheritsched(&attr, &getinheritsched));
  147. -   zassert_equal(getinheritsched, inheritsched);
  148. +   /*
  149. +    * values affected by inheritsched are policy / priority / contentionscope
  150. +    *
  151. +    * we only support PTHREAD_SCOPE_SYSTEM, so no need to set contentionscope
  152. +    */
  153. +   prio = K_LOWEST_APPLICATION_THREAD_PRIO;
  154. +   param.sched_priority = zephyr_to_posix_priority(prio, &policy);
  155.  
  156. -   /* Change priority of thread */
  157. -   zassert_ok(pthread_attr_setschedparam(&attr, &param));
  158.     zassert_ok(pthread_attr_setschedpolicy(&attr, policy));
  159. +   zassert_ok(pthread_attr_setschedparam(&attr, &param));
  160.  
  161. -   pthread_t parent;
  162. -
  163. -   zassert_ok(pthread_create(&parent, &attr,
  164. -                     test_pthread_attr_set_inheritsched_parent_fn, NULL));
  165. -   zassert_ok(pthread_join(parent, NULL));
  166. +   zassert_ok(pthread_attr_setinheritsched(&attr, inheritsched));
  167. +   create_thread_common_entry(&attr, true, true, inheritsched_entry, UINT_TO_POINTER(inheritsched));
  168.  }
  169.  
  170.  ZTEST(pthread_attr, test_pthread_attr_setinheritsched)
  171.  {
  172. -   int inheritsched = BIOS_FOOD;
  173. -
  174.     /* degenerate cases */
  175.     {
  176.         if (false) {
  177.             /* undefined behaviour */
  178.             zassert_equal(pthread_attr_setinheritsched(NULL, PTHREAD_EXPLICIT_SCHED),
  179.                       EINVAL);
  180. -           zassert_equal(pthread_attr_setinheritsched(NULL, inheritsched), EINVAL);
  181. +           zassert_equal(pthread_attr_setinheritsched(NULL, PTHREAD_INHERIT_SCHED), EINVAL);
  182.             zassert_equal(pthread_attr_setinheritsched((pthread_attr_t *)&uninit_attr,
  183. -                     inheritsched), EINVAL);
  184. +                     PTHREAD_INHERIT_SCHED), EINVAL);
  185.         }
  186.         zassert_equal(pthread_attr_setinheritsched(&attr, 3), EINVAL);
  187.     }
  188.  
  189. -   zassert_ok(pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED));
  190. -   zassert_ok(pthread_attr_getinheritsched(&attr, &inheritsched));
  191. -   zassert_equal(inheritsched, PTHREAD_EXPLICIT_SCHED);
  192. -
  193. -   test_pthread_attr_set_inheritsched_common(PTHREAD_INHERIT_SCHED);
  194. +   /* valid cases */
  195. +   test_pthread_attr_setinheritsched_common(PTHREAD_INHERIT_SCHED);
  196. +   test_pthread_attr_setinheritsched_common(PTHREAD_EXPLICIT_SCHED);
  197.  }
  198.  
  199.  
  200.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement