1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (C) 2022 Microsoft Corporation
8 #include <rte_thread.h>
13 RTE_LOG_REGISTER(threads_logtype_test, test.threads, INFO);
15 static uint32_t thread_id_ready;
18 thread_main(void *arg)
20 *(rte_thread_t *)arg = rte_thread_self();
21 __atomic_store_n(&thread_id_ready, 1, __ATOMIC_RELEASE);
27 test_thread_affinity(void)
30 rte_thread_t thread_id;
34 RTE_TEST_ASSERT(pthread_create(&id, NULL, thread_main, &thread_id) == 0,
35 "Failed to create thread");
37 while (__atomic_load_n(&thread_id_ready, __ATOMIC_ACQUIRE) == 0)
40 RTE_TEST_ASSERT(rte_thread_get_affinity_by_id(thread_id, &cpuset0) == 0,
41 "Failed to get thread affinity");
42 RTE_TEST_ASSERT(rte_thread_get_affinity_by_id(thread_id, &cpuset1) == 0,
43 "Failed to get thread affinity");
44 RTE_TEST_ASSERT(memcmp(&cpuset0, &cpuset1, sizeof(rte_cpuset_t)) == 0,
45 "Affinity should be stable");
48 for (i = 1; i < CPU_SETSIZE; i++)
49 if (CPU_ISSET(i, &cpuset0)) {
55 RTE_TEST_ASSERT(rte_thread_set_affinity_by_id(thread_id, &cpuset0) == 0,
56 "Failed to set thread affinity");
57 RTE_TEST_ASSERT(rte_thread_get_affinity_by_id(thread_id, &cpuset1) == 0,
58 "Failed to get thread affinity");
59 RTE_TEST_ASSERT(memcmp(&cpuset0, &cpuset1, sizeof(rte_cpuset_t)) == 0,
60 "Affinity should be stable");
65 static struct unit_test_suite threads_test_suite = {
66 .suite_name = "threads autotest",
70 TEST_CASE(test_thread_affinity),
78 return unit_test_suite_runner(&threads_test_suite);
81 REGISTER_TEST_COMMAND(threads_autotest, test_threads);