|
28 | 28 |
|
29 | 29 | K_THREAD_STACK_ARRAY_DEFINE(stack_e, N_THR_E, STACKS);
|
30 | 30 | K_THREAD_STACK_ARRAY_DEFINE(stack_t, N_THR_T, STACKS);
|
| 31 | +K_THREAD_STACK_ARRAY_DEFINE(stack_s, 1, STACKS); |
31 | 32 | K_THREAD_STACK_ARRAY_DEFINE(stack_1, 1, 32);
|
32 | 33 |
|
33 | 34 | void *thread_top_exec(void *p1);
|
@@ -647,6 +648,87 @@ ZTEST(posix_apis, test_pthread_descriptor_leak)
|
647 | 648 | }
|
648 | 649 | }
|
649 | 650 |
|
| 651 | +static void *create_thread_sched(void *p1) |
| 652 | +{ |
| 653 | + struct sched_param param = { .sched_priority = -1 }; |
| 654 | + int err = 0; |
| 655 | + int rc = 0; |
| 656 | + |
| 657 | + /* Get scheduling params of the current process. */ |
| 658 | + rc = sched_getparam(0, ¶m); |
| 659 | + err = errno; |
| 660 | + zassert_ok(rc, "unable to get scheduling parameters: rc=%d, errno=%d", |
| 661 | + rc, err); |
| 662 | + |
| 663 | + rc = sched_getscheduler(0); |
| 664 | + err = errno; |
| 665 | + zassert_not_equal(rc, -1, "unable to get scheduling policy: errno=%d", |
| 666 | + err); |
| 667 | + zassert_equal(rc, SCHED_RR, "unexpected scheduling policy: %d", rc); |
| 668 | + |
| 669 | + if (p1) { |
| 670 | + zassert_equal(param.sched_priority, |
| 671 | + ((struct sched_param *)p1)->sched_priority, |
| 672 | + "unexpected sched_priority=%d expects=%d", |
| 673 | + param.sched_priority, |
| 674 | + ((struct sched_param *)p1)->sched_priority); |
| 675 | + } |
| 676 | + |
| 677 | + return NULL; |
| 678 | +} |
| 679 | + |
| 680 | +ZTEST(posix_apis, test_sched_getparam) |
| 681 | +{ |
| 682 | + struct sched_param param = { .sched_priority = -1 }; |
| 683 | + int err = 0; |
| 684 | + int rc = 0; |
| 685 | + |
| 686 | + /* TODO: Assuming non-existent PID is -1 */ |
| 687 | + rc = sched_getparam(-1, ¶m); |
| 688 | + err = errno; |
| 689 | + zassert_true((rc == -1 && err == ESRCH), |
| 690 | + "failed parameter check: rc=%d, errno=%d", rc, err); |
| 691 | + |
| 692 | + rc = sched_getscheduler(-1); |
| 693 | + err = errno; |
| 694 | + zassert_true((rc == -1 && err == ESRCH), |
| 695 | + "failed parameter check: rc=%d, errno=%d", rc, err); |
| 696 | + |
| 697 | + /* .. and it is safe to call with NULL as praram. */ |
| 698 | + rc = sched_getparam(-1, NULL); |
| 699 | + err = errno; |
| 700 | + zassert_true((rc == -1 && err == ESRCH), |
| 701 | + "failed parameter check: rc=%d, errno=%d", rc, err); |
| 702 | + |
| 703 | + /* Try with the current PID as ztest execution thread - it fails. */ |
| 704 | + rc = sched_getparam(0, ¶m); |
| 705 | + err = errno; |
| 706 | + zassert_true((rc == -1 && err == ESRCH), |
| 707 | + "Unexpected result : rc=%d, errno=%d", rc, err); |
| 708 | + |
| 709 | + rc = sched_getscheduler(0); |
| 710 | + err = errno; |
| 711 | + zassert_true((rc == -1 && err == ESRCH), |
| 712 | + "Unexpected result : rc=%d, errno=%d", rc, err); |
| 713 | + |
| 714 | + /* Check with a test thread. */ |
| 715 | + pthread_t pthread1; |
| 716 | + pthread_attr_t attr1 = (pthread_attr_t){0}; |
| 717 | + |
| 718 | + param.sched_priority = sched_get_priority_min(SCHED_RR); |
| 719 | + err = errno; |
| 720 | + zassert_not_equal(-1, param.sched_priority, |
| 721 | + "sched_get_priority_min(SCHED_RR) failed: errno=%d", err); |
| 722 | + zassert_ok(pthread_attr_init(&attr1)); |
| 723 | + zassert_ok(pthread_attr_setschedparam(&attr1, ¶m), |
| 724 | + "pthread_attr_setschedparam() failed"); |
| 725 | + zassert_ok(pthread_attr_setstack(&attr1, &stack_s[0][0], STACKS)); |
| 726 | + zassert_ok(pthread_create(&pthread1, &attr1, create_thread_sched, (void *)¶m), |
| 727 | + "unable to create a test thread"); |
| 728 | + k_msleep(100); |
| 729 | + zassert_ok(pthread_join(pthread1, NULL), "unable to join the test thread"); |
| 730 | +} |
| 731 | + |
650 | 732 | ZTEST(posix_apis, test_sched_policy)
|
651 | 733 | {
|
652 | 734 | /*
|
|
0 commit comments