4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include <sys/queue.h>
39 #include <rte_common.h>
40 #include <rte_memory.h>
41 #include <rte_per_lcore.h>
42 #include <rte_launch.h>
43 #include <rte_atomic.h>
44 #include <rte_rwlock.h>
46 #include <rte_lcore.h>
47 #include <rte_cycles.h>
55 * - There is a global rwlock and a table of rwlocks (one per lcore).
57 * - The test function takes all of these locks and launches the
58 * ``test_rwlock_per_core()`` function on each core (except the master).
60 * - The function takes the global write lock, display something,
61 * then releases the global lock.
62 * - Then, it takes the per-lcore write lock, display something, and
63 * releases the per-core lock.
64 * - Finally, a read lock is taken during 100 ms, then released.
66 * - The main function unlocks the per-lcore locks sequentially and
67 * waits between each lock. This triggers the display of a message
68 * for each core, in the correct order.
70 * Then, it tries to take the global write lock and display the last
71 * message. The autotest script checks that the message order is correct.
74 static rte_rwlock_t sl;
75 static rte_rwlock_t sl_tab[RTE_MAX_LCORE];
78 test_rwlock_per_core(__attribute__((unused)) void *arg)
80 rte_rwlock_write_lock(&sl);
81 printf("Global write lock taken on core %u\n", rte_lcore_id());
82 rte_rwlock_write_unlock(&sl);
84 rte_rwlock_write_lock(&sl_tab[rte_lcore_id()]);
85 printf("Hello from core %u !\n", rte_lcore_id());
86 rte_rwlock_write_unlock(&sl_tab[rte_lcore_id()]);
88 rte_rwlock_read_lock(&sl);
89 printf("Global read lock taken on core %u\n", rte_lcore_id());
91 printf("Release global read lock on core %u\n", rte_lcore_id());
92 rte_rwlock_read_unlock(&sl);
102 rte_rwlock_init(&sl);
103 for (i=0; i<RTE_MAX_LCORE; i++)
104 rte_rwlock_init(&sl_tab[i]);
106 rte_rwlock_write_lock(&sl);
108 RTE_LCORE_FOREACH_SLAVE(i) {
109 rte_rwlock_write_lock(&sl_tab[i]);
110 rte_eal_remote_launch(test_rwlock_per_core, NULL, i);
113 rte_rwlock_write_unlock(&sl);
115 RTE_LCORE_FOREACH_SLAVE(i) {
116 rte_rwlock_write_unlock(&sl_tab[i]);
120 rte_rwlock_write_lock(&sl);
121 /* this message should be the last message of test */
122 printf("Global write lock taken on master core %u\n", rte_lcore_id());
123 rte_rwlock_write_unlock(&sl);
125 rte_eal_mp_wait_lcore();
130 REGISTER_TEST_COMMAND(rwlock_autotest, test_rwlock);