DPDK patches and discussions
 help / color / mirror / Atom feed
From: Ferruh Yigit <ferruh.yigit@amd.com>
To: Chengwen Feng <fengchengwen@huawei.com>
Cc: dev@dpdk.org, thomas@monjalon.net
Subject: Re: [RFC 1/3] lib/coroutine: add coroutine library
Date: Wed, 26 Apr 2023 12:28:44 +0100	[thread overview]
Message-ID: <332f8d59-1c45-4cc8-dd61-a9495cce6d93@amd.com> (raw)
In-Reply-To: <20230424130208.9517-2-fengchengwen@huawei.com>

On 4/24/2023 2:02 PM, Chengwen Feng wrote:
> This patch adds coroutine library. The main elements are:
> 1. scheduler: container of coroutines, which is responsible for
> scheduling coroutine.
> 2. coroutine: Minimum scheduling unit, it should associated to one
> scheduler.
> 
> In the coroutine callback, application could invoke rte_co_yield() to
> give up the CPU, and invoke rte_co_delay() to delay the specified
> microsecond.
> 
> Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>

<...>

> +++ b/lib/coroutine/rte_coroutine.c
> @@ -0,0 +1,190 @@
> +/* SPDX-License-Identifier: BSD-3-Clause
> + * Copyright(c) 2023 HiSilicon Limited
> + */
> +
> +#include <pthread.h>
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <ucontext.h>
> +
> +#include <rte_alarm.h>
> +#include <rte_ring.h>
> +
> +#include "rte_coroutine.h"
> +#include "rte_coroutine_imp.h"
> +
> +#define FATAL(fmt, args...) printf("[FATAL] %s() %d: " fmt "\n", __func__, __LINE__, ##args)
> +

No 'printf' for logging please.

<...>

> +int
> +rte_schedule_run(struct rte_schedule *s)
> +{
> +	struct rte_cocontext *co = NULL;
> +	uintptr_t ptr;
> +
> +	/* Set local thread variable as input argument. */
> +	co_schedule = s;
> +
> +	while (!rte_ring_empty(s->ring)) {
> +		rte_ring_dequeue(s->ring, (void **)&co);
> +		if (co->state == COROUTINE_READY) {
> +			getcontext(&co->ctx);
> +			co->ctx.uc_stack.ss_sp = co->stack;
> +			co->ctx.uc_stack.ss_size = co->stack_sz;
> +			co->ctx.uc_link = &s->main;
> +			co->state = COROUTINE_RUNNING;
> +			s->running = co;
> +			ptr = (uintptr_t)co;
> +			makecontext(&co->ctx, (void (*)(void))co_run_func, 2,
> +				    (uint32_t)ptr, (uint32_t)(ptr >> 32));

Why passing 'co' address as two 32bit values? Why not just pass address
of it?

Also can it be possible to set context to 'co->cb()' here and do the
cleanup after context returned to 's->main' (below)?

> +			swapcontext(&s->main, &co->ctx);
> +		} else if (co->state == COROUTINE_SUSPEND) {
> +			co->state = COROUTINE_RUNNING;
> +			s->running = co;
> +			swapcontext(&s->main, &co->ctx);
> +		} else {
> +			FATAL("invalid state!");
> +		}
> +	}
> +
> +	while (s->yield_head != NULL) {
> +		co = s->yield_head;
> +		s->yield_head = co->yield_next;
> +		if (co->state == COROUTINE_YIELD) {
> +			co->state = COROUTINE_RUNNING;
> +			s->running = co;
> +			swapcontext(&s->main, &co->ctx);
> +		} else {
> +			FATAL("invalid yield state!");
> +		}
> +	}
> +

As coroutines in 'ready' state stored in a ring, same can be done for
the coroutines in 'yield' state, instead of using a linked list, to
simplify the flow. Just a question, both works fine.

> +	return 0;
> +}
> +
> +int
> +rte_co_create(struct rte_schedule *s, coroutine_callback_t cb, void *arg, uint32_t stack_sz)
> +{
> +	struct rte_cocontext *co = calloc(1, sizeof(struct rte_cocontext));
> +	int ret;
> +	if (co == NULL)
> +		return -ENOMEM;
> +

I guess somewhere here should check the 's->max_coroutines';


  reply	other threads:[~2023-04-26 11:28 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-24 13:02 [RFC 0/3] introduce " Chengwen Feng
2023-04-24 13:02 ` [RFC 1/3] lib/coroutine: add " Chengwen Feng
2023-04-26 11:28   ` Ferruh Yigit [this message]
2023-04-24 13:02 ` [RFC 2/3] examples/coroutine: support coroutine examples Chengwen Feng
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=332f8d59-1c45-4cc8-dd61-a9495cce6d93@amd.com \
    --to=ferruh.yigit@amd.com \
    --cc=dev@dpdk.org \
    --cc=fengchengwen@huawei.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).