1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2015 Intel Corporation
10 #include <sys/types.h>
12 #include <sys/queue.h>
20 #include <rte_common.h>
21 #include <rte_lcore.h>
22 #include <rte_per_lcore.h>
23 #include <rte_timer.h>
25 #include "lthread_api.h"
26 #include "lthread_diag_api.h"
27 #include "pthread_shim.h"
30 #define HELLOW_WORLD_MAX_LTHREADS 10
32 #ifndef __GLIBC__ /* sched_getcpu() is glibc-specific */
33 #define sched_getcpu() rte_lcore_id()
36 __thread int print_count;
37 __thread pthread_mutex_t print_lock;
39 __thread pthread_mutex_t exit_lock;
40 __thread pthread_cond_t exit_cond;
43 * A simple thread that demonstrates use of a mutex, a condition
44 * variable, thread local storage, explicit yield, and thread exit.
46 * The thread uses a mutex to protect a shared counter which is incremented
47 * and then it waits on condition variable before exiting.
49 * The thread argument is stored in and retrieved from TLS, using
50 * the pthread key create, get and set specific APIs.
52 * The thread yields while holding the mutex, to provide opportunity
53 * for other threads to contend.
55 * All of the pthread API functions used by this thread are actually
56 * resolved to corresponding lthread functions by the pthread shim
57 * implemented in pthread_shim.c
59 void *helloworld_pthread(void *arg);
60 void *helloworld_pthread(void *arg)
64 /* create a key for TLS */
65 pthread_key_create(&key, NULL);
67 /* store the arg in TLS */
68 pthread_setspecific(key, arg);
70 /* grab lock and increment shared counter */
71 pthread_mutex_lock(&print_lock);
74 /* yield thread to give opportunity for lock contention */
77 /* retrieve arg from TLS */
78 uint64_t thread_no = (uint64_t) pthread_getspecific(key);
80 printf("Hello - lcore = %d count = %d thread_no = %d thread_id = %p\n",
84 (void *)pthread_self());
86 /* release the lock */
87 pthread_mutex_unlock(&print_lock);
90 * wait on condition variable
93 pthread_mutex_lock(&exit_lock);
94 pthread_cond_wait(&exit_cond, &exit_lock);
95 pthread_mutex_unlock(&exit_lock);
98 pthread_exit((void *) thread_no);
103 * This is the initial thread
105 * It demonstrates pthread, mutex and condition variable creation,
106 * broadcast and pthread join APIs.
108 * This initial thread must always start life as an lthread.
110 * This thread creates many more threads then waits a short time
111 * before signalling them to exit using a broadcast.
113 * All of the pthread API functions used by this thread are actually
114 * resolved to corresponding lthread functions by the pthread shim
115 * implemented in pthread_shim.c
117 * After all threads have finished the lthread scheduler is shutdown
118 * and normal pthread operation is restored
120 __thread pthread_t tid[HELLOW_WORLD_MAX_LTHREADS];
122 static void initial_lthread(void *args);
123 static void initial_lthread(void *args __attribute__((unused)))
125 int lcore = (int) rte_lcore_id();
128 * We can now enable pthread API override
129 * and start to use the pthread APIs
131 pthread_override_set(1);
136 /* initialize mutex for shared counter */
138 pthread_mutex_init(&print_lock, NULL);
140 /* initialize mutex and condition variable controlling thread exit */
141 pthread_mutex_init(&exit_lock, NULL);
142 pthread_cond_init(&exit_cond, NULL);
144 /* spawn a number of threads */
145 for (i = 0; i < HELLOW_WORLD_MAX_LTHREADS; i++) {
148 * Not strictly necessary but
149 * for the sake of this example
150 * use an attribute to pass the desired lcore
156 CPU_SET(lcore, &cpuset);
157 pthread_attr_init(&attr);
158 pthread_attr_setaffinity_np(&attr, sizeof(rte_cpuset_t), &cpuset);
160 /* create the thread */
161 ret = pthread_create(&tid[i], &attr,
162 helloworld_pthread, (void *) i);
164 rte_exit(EXIT_FAILURE, "Cannot create helloworld thread\n");
167 /* wait for 1s to allow threads
168 * to block on the condition variable
169 * N.B. nanosleep() is resolved to lthread_sleep()
172 struct timespec time;
176 nanosleep(&time, NULL);
178 /* wake up all the threads */
179 pthread_cond_broadcast(&exit_cond);
181 /* wait for them to finish */
182 for (i = 0; i < HELLOW_WORLD_MAX_LTHREADS; i++) {
186 pthread_join(tid[i], (void *) &thread_no);
188 printf("error on thread exit\n");
191 pthread_cond_destroy(&exit_cond);
192 pthread_mutex_destroy(&print_lock);
193 pthread_mutex_destroy(&exit_lock);
195 /* shutdown the lthread scheduler */
196 lthread_scheduler_shutdown(rte_lcore_id());
202 /* This thread creates a single initial lthread
203 * and then runs the scheduler
204 * An instance of this thread is created on each thread
208 lthread_scheduler(void *args);
210 lthread_scheduler(void *args __attribute__((unused)))
212 /* create initial thread */
215 lthread_create(<, -1, initial_lthread, (void *) NULL);
217 /* run the lthread scheduler */
220 /* restore genuine pthread operation */
221 pthread_override_set(0);
225 int main(int argc, char **argv)
229 /* basic DPDK initialization is all that is necessary to run lthreads*/
230 int ret = rte_eal_init(argc, argv);
233 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
235 /* enable timer subsystem */
236 rte_timer_subsystem_init();
239 lthread_diagnostic_set_mask(LT_DIAG_ALL);
242 /* create a scheduler on every core in the core mask
243 * and launch an initial lthread that will spawn many more.
247 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
248 if (rte_lcore_is_enabled(lcore_id))
252 /* set the number of schedulers, this forces all schedulers synchronize
253 * before entering their main loop
255 lthread_num_schedulers_set(num_sched);
257 /* launch all threads */
258 rte_eal_mp_remote_launch(lthread_scheduler, (void *)NULL, CALL_MASTER);
260 /* wait for threads to stop */
261 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
262 rte_eal_wait_lcore(lcore_id);