From: Chengwen Feng <fengchengwen@huawei.com>
To: <thomas@monjalon.net>, <ferruh.yigit@amd.com>
Cc: <dev@dpdk.org>
Subject: [RFC 2/3] examples/coroutine: support coroutine examples
Date: Mon, 24 Apr 2023 13:02:07 +0000 [thread overview]
Message-ID: <20230424130208.9517-3-fengchengwen@huawei.com> (raw)
In-Reply-To: <20230424130208.9517-1-fengchengwen@huawei.com>
This patch adds coroutine example, usage:
1. start examples: dpdk-coroutine -a 0000:7d:00.2 -l 10-11
2. will output:
Start yield coroutine test!
I am in yield coroutine 111!
I am in yield coroutine 222!
I am in yield coroutine 333!
I am in yield coroutine 111!
I am in yield coroutine 222!
I am in yield coroutine 333!
...
Start delay coroutine test!
I am in delay coroutine 111!
I am in delay coroutine 222!
I am in delay coroutine 222!
I am in delay coroutine 111!
I am in delay coroutine 222!
I am in delay coroutine 222!
...
3. use ctrl+c to exit example.
Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
---
examples/coroutine/main.c | 153 +++++++++++++++++++++++++++++++++
examples/coroutine/meson.build | 10 +++
examples/meson.build | 1 +
3 files changed, 164 insertions(+)
create mode 100644 examples/coroutine/main.c
create mode 100644 examples/coroutine/meson.build
diff --git a/examples/coroutine/main.c b/examples/coroutine/main.c
new file mode 100644
index 0000000000..2704ad1dc9
--- /dev/null
+++ b/examples/coroutine/main.c
@@ -0,0 +1,153 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2023 HiSilicon Limited
+ */
+
+#include <stdint.h>
+#include <stdlib.h>
+#include <getopt.h>
+#include <signal.h>
+#include <stdbool.h>
+#include <unistd.h>
+
+#include <rte_common.h>
+#include <rte_coroutine.h>
+#include <rte_lcore.h>
+#include <rte_launch.h>
+
+static volatile bool force_quit;
+
+static struct rte_schedule *target_s;
+
+static void
+yield_coroutine_1(void *arg)
+{
+ RTE_SET_USED(arg);
+ int i = 10;
+ while (i--) {
+ printf("\tI am in yield coroutine 111!\n");
+ rte_co_yield();
+ }
+}
+
+static void
+yield_coroutine_2(void *arg)
+{
+ RTE_SET_USED(arg);
+ int i = 10;
+ while (i--) {
+ printf("\tI am in yield coroutine 222!\n");
+ rte_co_yield();
+ }
+}
+
+static void
+yield_coroutine_3(void *arg)
+{
+ RTE_SET_USED(arg);
+ int i = 10;
+ while (i--) {
+ printf("\tI am in yield coroutine 333!\n");
+ rte_co_yield();
+ }
+}
+
+static void
+yield_coroutine_test(void)
+{
+ printf("Start yield coroutine test!\n");
+ rte_co_create(target_s, yield_coroutine_1, NULL, 0);
+ rte_co_create(target_s, yield_coroutine_2, NULL, 0);
+ rte_co_create(target_s, yield_coroutine_3, NULL, 0);
+ sleep(1);
+}
+
+static void
+delay_coroutine_1(void *arg)
+{
+ RTE_SET_USED(arg);
+ int i = 10;
+ while (i--) {
+ printf("\tI am in delay coroutine 111!\n");
+ rte_co_delay(100 * 1000);
+ }
+}
+
+static void
+delay_coroutine_2(void *arg)
+{
+ RTE_SET_USED(arg);
+ int i = 20;
+ while (i--) {
+ printf("\tI am in delay coroutine 222!\n");
+ rte_co_delay(50 * 1000);
+ }
+}
+
+static void
+delay_coroutine_test(void)
+{
+ printf("Start delay coroutine test!\n");
+ rte_co_create(target_s, delay_coroutine_1, NULL, 0);
+ rte_co_create(target_s, delay_coroutine_2, NULL, 0);
+ sleep(1);
+}
+
+static int
+co_main_loop(void *arg)
+{
+ RTE_SET_USED(arg);
+ while (!force_quit)
+ rte_schedule_run(target_s);
+ return 0;
+}
+
+static void
+signal_handler(int signum)
+{
+ if (signum == SIGINT || signum == SIGTERM) {
+ printf("\n\nSignal %d received, preparing to exit...\n",
+ signum);
+ force_quit = true;
+ }
+}
+
+int
+main(int argc, char **argv)
+{
+ uint32_t lcore_id = rte_lcore_id();
+ int ret;
+
+ /* Init EAL. 8< */
+ ret = rte_eal_init(argc, argv);
+ if (ret < 0)
+ rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
+
+ force_quit = false;
+ signal(SIGINT, signal_handler);
+ signal(SIGTERM, signal_handler);
+
+ /* Check if there is enough lcores for all ports. */
+ if (rte_lcore_count() < 2)
+ rte_exit(EXIT_FAILURE,
+ "There should be at least one worker lcore.\n");
+
+ target_s = rte_schedule_create("co-sched-test", 128);
+ if (target_s == NULL)
+ rte_exit(EXIT_FAILURE,
+ "Create target scheduler failed!\n");
+
+ lcore_id = rte_get_next_lcore(lcore_id, true, true);
+ rte_eal_remote_launch(co_main_loop, NULL, lcore_id);
+
+ yield_coroutine_test();
+ delay_coroutine_test();
+
+ /* force_quit is true when we get here */
+ rte_eal_mp_wait_lcore();
+
+ /* clean up the EAL */
+ rte_eal_cleanup();
+
+ printf("Bye...\n");
+ return 0;
+}
diff --git a/examples/coroutine/meson.build b/examples/coroutine/meson.build
new file mode 100644
index 0000000000..c3576fe2f3
--- /dev/null
+++ b/examples/coroutine/meson.build
@@ -0,0 +1,10 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2023 HiSilicon Limited
+
+allow_experimental_apis = true
+
+deps += ['coroutine']
+
+sources = files(
+ 'main.c',
+)
diff --git a/examples/meson.build b/examples/meson.build
index 6968c09252..111d628065 100644
--- a/examples/meson.build
+++ b/examples/meson.build
@@ -11,6 +11,7 @@ all_examples = [
'bbdev_app',
'bond',
'cmdline',
+ 'coroutine',
'distributor',
'dma',
'ethtool',
--
2.17.1
next prev parent reply other threads:[~2023-04-24 13:09 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-04-24 13:02 [RFC 0/3] introduce coroutine library Chengwen Feng
2023-04-24 13:02 ` [RFC 1/3] lib/coroutine: add " Chengwen Feng
2023-04-26 11:28 ` Ferruh Yigit
2023-04-24 13:02 ` Chengwen Feng [this message]
2023-04-24 13:02 ` [RFC 3/3] net/hns3: refactor reset process with coroutine Chengwen Feng
2023-04-24 16:08 ` [RFC 0/3] introduce coroutine library Stephen Hemminger
2023-04-25 2:11 ` fengchengwen
2023-04-25 2:16 ` Stephen Hemminger
2023-04-25 2:50 ` fengchengwen
2023-04-25 2:59 ` Garrett D'Amore
2023-04-25 21:06 ` Stephen Hemminger
2023-04-26 11:27 ` Ferruh Yigit
2023-04-28 7:20 ` fengchengwen
2023-04-25 9:27 ` Mattias Rönnblom
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230424130208.9517-3-fengchengwen@huawei.com \
--to=fengchengwen@huawei.com \
--cc=dev@dpdk.org \
--cc=ferruh.yigit@amd.com \
--cc=thomas@monjalon.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).