Skip to content

Commit

Permalink
tests: posix: pthread: test that big stacks can be allocated
Browse files Browse the repository at this point in the history
In some cases, users want to allocate (comparatively) massive
thread stacks.

Add a test to ensure we can allocate such a stack by default.

Signed-off-by: Christopher Friedt <[email protected]>
  • Loading branch information
cfriedt committed Dec 24, 2023
1 parent fcd139d commit b702c8e
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions tests/posix/common/src/pthread.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#define ONE_SECOND 1

/* arbitrary number that is also a legal stack size */
#define OKAY_STACK_SIZE (STACKS + 42)
#define OKAY_STACK_SIZE (STACKS + 1)

/* Macros to test invalid states */
#define PTHREAD_CANCEL_INVALID -1
Expand Down Expand Up @@ -959,11 +959,14 @@ ZTEST(posix_apis, test_pthread_attr_setguardsize)
size_t size_after;
size_t size_before;
pthread_attr_t attr;
size_t sizes[] = {0, OKAY_STACK_SIZE, UINT16_MAX};
size_t sizes[] = {0, BIT_MASK(CONFIG_POSIX_PTHREAD_ATTR_GUARDSIZE_BITS / 2),
BIT_MASK(CONFIG_POSIX_PTHREAD_ATTR_GUARDSIZE_BITS)};

attr = (pthread_attr_t){0};
zassert_equal(pthread_attr_setguardsize(&attr, 0), EINVAL);
zassert_ok(pthread_attr_init(&attr));
zassert_ok(pthread_attr_getguardsize(&attr, &size_before));
zassert_equal(size_before, CONFIG_POSIX_PTHREAD_ATTR_GUARDSIZE_DEFAULT);
zassert_equal(pthread_attr_setguardsize(NULL, SIZE_MAX), EINVAL);
zassert_equal(pthread_attr_setguardsize(NULL, 0), EINVAL);
zassert_equal(pthread_attr_setguardsize(&attr, SIZE_MAX), EINVAL);
Expand All @@ -976,3 +979,16 @@ ZTEST(posix_apis, test_pthread_attr_setguardsize)
}
zassert_ok(pthread_attr_destroy(&attr));
}

ZTEST(posix_apis, test_pthread_attr_large_stacksize)
{
size_t actual_size;
const size_t expect_size = BIT(CONFIG_POSIX_PTHREAD_ATTR_STACKSIZE_BITS);
pthread_attr_t attr;

zassert_ok(pthread_attr_init(&attr));
zassert_ok(pthread_attr_setstacksize(&attr, expect_size));
zassert_ok(pthread_attr_getstacksize(&attr, &actual_size));
zassert_equal(actual_size, expect_size);
zassert_ok(pthread_attr_destroy(&attr));
}

0 comments on commit b702c8e

Please sign in to comment.