5 * Copyright(c) 2015 Intel Corporation. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
18 * * Neither the name of Intel Corporation nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #include <sys/types.h>
42 #include <sys/queue.h>
50 #include <rte_common.h>
51 #include <rte_lcore.h>
52 #include <rte_per_lcore.h>
53 #include <rte_timer.h>
55 #include "lthread_api.h"
56 #include "lthread_diag_api.h"
57 #include "pthread_shim.h"
60 #define HELLOW_WORLD_MAX_LTHREADS 10
62 __thread int print_count;
63 __thread pthread_mutex_t print_lock;
65 __thread pthread_mutex_t exit_lock;
66 __thread pthread_cond_t exit_cond;
69 * A simple thread that demonstrates use of a mutex, a condition
70 * variable, thread local storage, explicit yield, and thread exit.
72 * The thread uses a mutex to protect a shared counter which is incremented
73 * and then it waits on condition variable before exiting.
75 * The thread argument is stored in and retrieved from TLS, using
76 * the pthread key create, get and set specific APIs.
78 * The thread yields while holding the mutex, to provide opportunity
79 * for other threads to contend.
81 * All of the pthread API functions used by this thread are actually
82 * resolved to corresponding lthread functions by the pthread shim
83 * implemented in pthread_shim.c
85 void *helloworld_pthread(void *arg);
86 void *helloworld_pthread(void *arg)
90 /* create a key for TLS */
91 pthread_key_create(&key, NULL);
93 /* store the arg in TLS */
94 pthread_setspecific(key, arg);
96 /* grab lock and increment shared counter */
97 pthread_mutex_lock(&print_lock);
100 /* yield thread to give opportunity for lock contention */
103 /* retrieve arg from TLS */
104 uint64_t thread_no = (uint64_t) pthread_getspecific(key);
106 printf("Hello - lcore = %d count = %d thread_no = %d thread_id = %p\n",
110 (void *)pthread_self());
112 /* release the lock */
113 pthread_mutex_unlock(&print_lock);
116 * wait on condition variable
119 pthread_mutex_lock(&exit_lock);
120 pthread_cond_wait(&exit_cond, &exit_lock);
121 pthread_mutex_unlock(&exit_lock);
124 pthread_exit((void *) thread_no);
129 * This is the initial thread
131 * It demonstrates pthread, mutex and condition variable creation,
132 * broadcast and pthread join APIs.
134 * This initial thread must always start life as an lthread.
136 * This thread creates many more threads then waits a short time
137 * before signalling them to exit using a broadcast.
139 * All of the pthread API functions used by this thread are actually
140 * resolved to corresponding lthread functions by the pthread shim
141 * implemented in pthread_shim.c
143 * After all threads have finished the lthread scheduler is shutdown
144 * and normal pthread operation is restored
146 __thread pthread_t tid[HELLOW_WORLD_MAX_LTHREADS];
148 static void initial_lthread(void *args);
149 static void initial_lthread(void *args __attribute__((unused)))
151 int lcore = (int) rte_lcore_id();
154 * We can now enable pthread API override
155 * and start to use the pthread APIs
157 pthread_override_set(1);
161 /* initialize mutex for shared counter */
163 pthread_mutex_init(&print_lock, NULL);
165 /* initialize mutex and condition variable controlling thread exit */
166 pthread_mutex_init(&exit_lock, NULL);
167 pthread_cond_init(&exit_cond, NULL);
169 /* spawn a number of threads */
170 for (i = 0; i < HELLOW_WORLD_MAX_LTHREADS; i++) {
173 * Not strictly necessary but
174 * for the sake of this example
175 * use an attribute to pass the desired lcore
181 CPU_SET(lcore, &cpuset);
182 pthread_attr_init(&attr);
183 pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &cpuset);
185 /* create the thread */
186 pthread_create(&tid[i], &attr, helloworld_pthread, (void *) i);
189 /* wait for 1s to allow threads
190 * to block on the condition variable
191 * N.B. nanosleep() is resolved to lthread_sleep()
194 struct timespec time;
198 nanosleep(&time, NULL);
200 /* wake up all the threads */
201 pthread_cond_broadcast(&exit_cond);
203 /* wait for them to finish */
204 for (i = 0; i < HELLOW_WORLD_MAX_LTHREADS; i++) {
208 pthread_join(tid[i], (void *) &thread_no);
210 printf("error on thread exit\n");
213 pthread_cond_destroy(&exit_cond);
214 pthread_mutex_destroy(&print_lock);
215 pthread_mutex_destroy(&exit_lock);
217 /* shutdown the lthread scheduler */
218 lthread_scheduler_shutdown(rte_lcore_id());
224 /* This thread creates a single initial lthread
225 * and then runs the scheduler
226 * An instance of this thread is created on each thread
230 lthread_scheduler(void *args);
232 lthread_scheduler(void *args __attribute__((unused)))
234 /* create initial thread */
237 lthread_create(<, -1, initial_lthread, (void *) NULL);
239 /* run the lthread scheduler */
242 /* restore genuine pthread operation */
243 pthread_override_set(0);
247 int main(int argc, char **argv)
251 /* basic DPDK initialization is all that is necessary to run lthreads*/
252 int ret = rte_eal_init(argc, argv);
255 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
257 /* enable timer subsystem */
258 rte_timer_subsystem_init();
261 lthread_diagnostic_set_mask(LT_DIAG_ALL);
264 /* create a scheduler on every core in the core mask
265 * and launch an initial lthread that will spawn many more.
269 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
270 if (rte_lcore_is_enabled(lcore_id))
274 /* set the number of schedulers, this forces all schedulers synchronize
275 * before entering their main loop
277 lthread_num_schedulers_set(num_sched);
279 /* launch all threads */
280 rte_eal_mp_remote_launch(lthread_scheduler, (void *)NULL, CALL_MASTER);
282 /* wait for threads to stop */
283 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
284 rte_eal_wait_lcore(lcore_id);