test/threads: add unit test for lifetime API

Test basic functionality and demonstrate use of following thread
lifetime api.

    * rte_thread_create
    * rte_thread_detach
    * rte_thread_equal
    * rte_thread_join

Existing tests are updated to adapt to the thread_main function
prototype change.
The dependency on the pthread API can then be removed.

Signed-off-by: Narcisa Vasile <navasile@linux.microsoft.com>
Signed-off-by: Tyler Retzlaff <roretzla@linux.microsoft.com>
Acked-by: Dmitry Kozlyuk <dmitry.kozliuk@gmail.com>
This commit is contained in:
Tyler Retzlaff 2022-10-05 10:07:29 -07:00 committed by David Marchand
parent a2e94ca89f
commit e1d35c1599

View File

@ -3,7 +3,6 @@
*/
#include <string.h>
#include <pthread.h>
#include <rte_thread.h>
#include <rte_debug.h>
@ -14,27 +13,76 @@ RTE_LOG_REGISTER(threads_logtype_test, test.threads, INFO);
static uint32_t thread_id_ready;
static void *
static uint32_t
thread_main(void *arg)
{
*(rte_thread_t *)arg = rte_thread_self();
if (arg != NULL)
*(rte_thread_t *)arg = rte_thread_self();
__atomic_store_n(&thread_id_ready, 1, __ATOMIC_RELEASE);
while (__atomic_load_n(&thread_id_ready, __ATOMIC_ACQUIRE) == 1)
;
return NULL;
return 0;
}
static int
test_thread_create_join(void)
{
rte_thread_t thread_id;
rte_thread_t thread_main_id;
thread_id_ready = 0;
RTE_TEST_ASSERT(rte_thread_create(&thread_id, NULL, thread_main, &thread_main_id) == 0,
"Failed to create thread.");
while (__atomic_load_n(&thread_id_ready, __ATOMIC_ACQUIRE) == 0)
;
RTE_TEST_ASSERT(rte_thread_equal(thread_id, thread_main_id) != 0,
"Unexpected thread id.");
__atomic_store_n(&thread_id_ready, 2, __ATOMIC_RELEASE);
RTE_TEST_ASSERT(rte_thread_join(thread_id, NULL) == 0,
"Failed to join thread.");
return 0;
}
static int
test_thread_create_detach(void)
{
rte_thread_t thread_id;
rte_thread_t thread_main_id;
thread_id_ready = 0;
RTE_TEST_ASSERT(rte_thread_create(&thread_id, NULL, thread_main,
&thread_main_id) == 0, "Failed to create thread.");
while (__atomic_load_n(&thread_id_ready, __ATOMIC_ACQUIRE) == 0)
;
RTE_TEST_ASSERT(rte_thread_equal(thread_id, thread_main_id) != 0,
"Unexpected thread id.");
__atomic_store_n(&thread_id_ready, 2, __ATOMIC_RELEASE);
RTE_TEST_ASSERT(rte_thread_detach(thread_id) == 0,
"Failed to detach thread.");
return 0;
}
static int
test_thread_priority(void)
{
pthread_t id;
rte_thread_t thread_id;
enum rte_thread_priority priority;
thread_id_ready = 0;
RTE_TEST_ASSERT(pthread_create(&id, NULL, thread_main, &thread_id) == 0,
RTE_TEST_ASSERT(rte_thread_create(&thread_id, NULL, thread_main, NULL) == 0,
"Failed to create thread");
while (__atomic_load_n(&thread_id_ready, __ATOMIC_ACQUIRE) == 0)
@ -81,13 +129,12 @@ test_thread_priority(void)
static int
test_thread_affinity(void)
{
pthread_t id;
rte_thread_t thread_id;
rte_cpuset_t cpuset0;
rte_cpuset_t cpuset1;
thread_id_ready = 0;
RTE_TEST_ASSERT(pthread_create(&id, NULL, thread_main, &thread_id) == 0,
RTE_TEST_ASSERT(rte_thread_create(&thread_id, NULL, thread_main, NULL) == 0,
"Failed to create thread");
while (__atomic_load_n(&thread_id_ready, __ATOMIC_ACQUIRE) == 0)
@ -123,6 +170,8 @@ static struct unit_test_suite threads_test_suite = {
.setup = NULL,
.teardown = NULL,
.unit_test_cases = {
TEST_CASE(test_thread_create_join),
TEST_CASE(test_thread_create_detach),
TEST_CASE(test_thread_affinity),
TEST_CASE(test_thread_priority),
TEST_CASES_END()