1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
10 #include <rte_common.h>
11 #include <rte_memory.h>
12 #include <rte_per_lcore.h>
13 #include <rte_launch.h>
14 #include <rte_atomic.h>
15 #include <rte_rwlock.h>
17 #include <rte_lcore.h>
18 #include <rte_cycles.h>
26 * - There is a global rwlock and a table of rwlocks (one per lcore).
28 * - The test function takes all of these locks and launches the
29 * ``test_rwlock_per_core()`` function on each core (except the master).
31 * - The function takes the global write lock, display something,
32 * then releases the global lock.
33 * - Then, it takes the per-lcore write lock, display something, and
34 * releases the per-core lock.
35 * - Finally, a read lock is taken during 100 ms, then released.
37 * - The main function unlocks the per-lcore locks sequentially and
38 * waits between each lock. This triggers the display of a message
39 * for each core, in the correct order.
41 * Then, it tries to take the global write lock and display the last
42 * message. The autotest script checks that the message order is correct.
45 static rte_rwlock_t sl;
46 static rte_rwlock_t sl_tab[RTE_MAX_LCORE];
49 test_rwlock_per_core(__attribute__((unused)) void *arg)
51 rte_rwlock_write_lock(&sl);
52 printf("Global write lock taken on core %u\n", rte_lcore_id());
53 rte_rwlock_write_unlock(&sl);
55 rte_rwlock_write_lock(&sl_tab[rte_lcore_id()]);
56 printf("Hello from core %u !\n", rte_lcore_id());
57 rte_rwlock_write_unlock(&sl_tab[rte_lcore_id()]);
59 rte_rwlock_read_lock(&sl);
60 printf("Global read lock taken on core %u\n", rte_lcore_id());
62 printf("Release global read lock on core %u\n", rte_lcore_id());
63 rte_rwlock_read_unlock(&sl);
74 for (i=0; i<RTE_MAX_LCORE; i++)
75 rte_rwlock_init(&sl_tab[i]);
77 rte_rwlock_write_lock(&sl);
79 RTE_LCORE_FOREACH_SLAVE(i) {
80 rte_rwlock_write_lock(&sl_tab[i]);
81 rte_eal_remote_launch(test_rwlock_per_core, NULL, i);
84 rte_rwlock_write_unlock(&sl);
86 RTE_LCORE_FOREACH_SLAVE(i) {
87 rte_rwlock_write_unlock(&sl_tab[i]);
91 rte_rwlock_write_lock(&sl);
92 /* this message should be the last message of test */
93 printf("Global write lock taken on master core %u\n", rte_lcore_id());
94 rte_rwlock_write_unlock(&sl);
96 rte_eal_mp_wait_lcore();
101 REGISTER_TEST_COMMAND(rwlock_autotest, test_rwlock);