tests: posix: common: add pthread spinlock tests

Exercise the following (which rely mainly on existing Zephyr
utilities)
* `pthread_spin_init()`
* `pthread_spin_destroy()`
* `pthread_spin_lock()`
* `pthread_spin_trylock()`
* `pthread_spin_unlock()`

Additionally, ensure that we do not leak spinlock descriptors.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
This commit is contained in:
Christopher Friedt 2023-07-02 19:41:29 +02:00 committed by Chris Friedt
commit d73bdbf5bd
2 changed files with 76 additions and 0 deletions

View file

@ -0,0 +1,71 @@
/*
* Copyright (c) 2023, Meta
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <pthread.h>
#include <zephyr/ztest.h>
#include <zephyr/sys/util.h>
ZTEST(posix_apis, test_spin_init_destroy)
{
pthread_spinlock_t lock;
zassert_equal(pthread_spin_init(NULL, PTHREAD_PROCESS_PRIVATE), EINVAL,
"pthread_spin_init() did not return EINVAL with NULL lock pointer");
zassert_equal(pthread_spin_init(&lock, 42), EINVAL,
"pthread_spin_init() did not return EINVAL with invalid pshared");
zassert_equal(pthread_spin_destroy(NULL), EINVAL,
"pthread_spin_destroy() did not return EINVAL with NULL lock pointer");
zassert_ok(pthread_spin_init(&lock, PTHREAD_PROCESS_PRIVATE), "pthread_spin_init() failed");
zassert_ok(pthread_spin_destroy(&lock), "pthread_spin_destroy() failed");
}
ZTEST(posix_apis, test_spin_descriptor_leak)
{
pthread_spinlock_t lock[CONFIG_MAX_PTHREAD_SPINLOCK_COUNT];
for (size_t j = 0; j < 2; ++j) {
for (size_t i = 0; i < ARRAY_SIZE(lock); ++i) {
zassert_ok(pthread_spin_init(&lock[i], PTHREAD_PROCESS_PRIVATE),
"failed to initialize spinlock %zu (rep %zu)", i, j);
}
zassert_equal(pthread_spin_init(&lock[CONFIG_MAX_PTHREAD_SPINLOCK_COUNT],
PTHREAD_PROCESS_PRIVATE),
ENOMEM,
"should not be able to initialize more than "
"CONFIG_MAX_PTHREAD_SPINLOCK_COUNT spinlocks");
for (size_t i = 0; i < ARRAY_SIZE(lock); ++i) {
zassert_ok(pthread_spin_destroy(&lock[i]),
"failed to destroy spinlock %zu (rep %zu)", i, j);
}
}
}
ZTEST(posix_apis, test_spin_lock_unlock)
{
pthread_spinlock_t lock;
zassert_equal(pthread_spin_lock(NULL), EINVAL,
"pthread_spin_lock() did not return EINVAL with NULL lock pointer");
zassert_equal(pthread_spin_trylock(NULL), EINVAL,
"pthread_spin_lock() did not return EINVAL with NULL lock pointer");
zassert_equal(pthread_spin_unlock(NULL), EINVAL,
"pthread_spin_lock() did not return EINVAL with NULL lock pointer");
zassert_ok(pthread_spin_init(&lock, PTHREAD_PROCESS_PRIVATE), "pthread_spin_init() failed");
zassert_ok(pthread_spin_lock(&lock), "pthread_spin_lock() failed");
zassert_ok(pthread_spin_unlock(&lock), "pthread_spin_lock() failed");
zassert_ok(pthread_spin_trylock(&lock), "pthread_spin_trylock() failed");
zassert_ok(pthread_spin_unlock(&lock), "pthread_spin_unlock() failed");
zassert_ok(pthread_spin_destroy(&lock), "pthread_spin_init() failed");
zassert_equal(pthread_spin_destroy(&lock), EINVAL, "pthread_spin_unlock() did not fail");
}

View file

@ -75,3 +75,8 @@ tests:
- qemu_x86
extra_configs:
- CONFIG_PICOLIBC=y
portability.posix.common.no_spin_validate:
extra_configs:
- CONFIG_SPIN_VALIDATE=n
integration_platforms:
- mps2_an385