first public release
[dpdk.git] / app / test / test_rwlock.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without 
8  *   modification, are permitted provided that the following conditions 
9  *   are met:
10  * 
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 
16  *       distribution.
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.
20  * 
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.
32  * 
33  *  version: DPDK.L.1.2.3-3
34  */
35
36 #include <stdio.h>
37 #include <stdint.h>
38 #include <unistd.h>
39 #include <sys/queue.h>
40
41 #include <cmdline_parse.h>
42
43 #include <rte_common.h>
44 #include <rte_memory.h>
45 #include <rte_memzone.h>
46 #include <rte_per_lcore.h>
47 #include <rte_launch.h>
48 #include <rte_atomic.h>
49 #include <rte_rwlock.h>
50 #include <rte_tailq.h>
51 #include <rte_eal.h>
52 #include <rte_per_lcore.h>
53 #include <rte_lcore.h>
54 #include <rte_cycles.h>
55
56 #include "test.h"
57
58 /*
59  * rwlock test
60  * ===========
61  *
62  * - There is a global rwlock and a table of rwlocks (one per lcore).
63  *
64  * - The test function takes all of these locks and launches the
65  *   ``test_rwlock_per_core()`` function on each core (except the master).
66  *
67  *   - The function takes the global write lock, display something,
68  *     then releases the global lock.
69  *   - Then, it takes the per-lcore write lock, display something, and
70  *     releases the per-core lock.
71  *   - Finally, a read lock is taken during 100 ms, then released.
72  *
73  * - The main function unlocks the per-lcore locks sequentially and
74  *   waits between each lock. This triggers the display of a message
75  *   for each core, in the correct order.
76  *
77  *   Then, it tries to take the global write lock and display the last
78  *   message. The autotest script checks that the message order is correct.
79  */
80
81 static rte_rwlock_t sl;
82 static rte_rwlock_t sl_tab[RTE_MAX_LCORE];
83
84 static int
85 test_rwlock_per_core(__attribute__((unused)) void *arg)
86 {
87         rte_rwlock_write_lock(&sl);
88         printf("Global write lock taken on core %u\n", rte_lcore_id());
89         rte_rwlock_write_unlock(&sl);
90
91         rte_rwlock_write_lock(&sl_tab[rte_lcore_id()]);
92         printf("Hello from core %u !\n", rte_lcore_id());
93         rte_rwlock_write_unlock(&sl_tab[rte_lcore_id()]);
94
95         rte_rwlock_read_lock(&sl);
96         printf("Global read lock taken on core %u\n", rte_lcore_id());
97         rte_delay_ms(100);
98         printf("Release global read lock on core %u\n", rte_lcore_id());
99         rte_rwlock_read_unlock(&sl);
100
101         return 0;
102 }
103
104 int
105 test_rwlock(void)
106 {
107         int i;
108
109         rte_rwlock_init(&sl);
110         for (i=0; i<RTE_MAX_LCORE; i++)
111                 rte_rwlock_init(&sl_tab[i]);
112
113         rte_rwlock_write_lock(&sl);
114
115         RTE_LCORE_FOREACH_SLAVE(i) {
116                 rte_rwlock_write_lock(&sl_tab[i]);
117                 rte_eal_remote_launch(test_rwlock_per_core, NULL, i);
118         }
119
120         rte_rwlock_write_unlock(&sl);
121
122         RTE_LCORE_FOREACH_SLAVE(i) {
123                 rte_rwlock_write_unlock(&sl_tab[i]);
124                 rte_delay_ms(100);
125         }
126
127         rte_rwlock_write_lock(&sl);
128         /* this message should be the last message of test */
129         printf("Global write lock taken on master core %u\n", rte_lcore_id());
130         rte_rwlock_write_unlock(&sl);
131
132         rte_eal_mp_wait_lcore();
133
134         return 0;
135 }