Giter Site home page Giter Site logo

Comments (4)

ChuanqiXu9 avatar ChuanqiXu9 commented on June 2, 2024

The behavior of coro::sleep depends on the behavior of executors. Concretely, it depends on the implementation of Executor::after(std::chrono::duration<T,U>). And it is the general idea of async_simple to make the behavior configurable with the executors.

from async_simple.

4kangjc avatar 4kangjc commented on June 2, 2024

SimpleExecutor is just an Executor for testing. It is not part of libasync_simple. You need to define your own Executor.

from async_simple.

RainMark avatar RainMark commented on June 2, 2024

We use SimpleExecutor + brpc TimerThread for production currently. This is an example of custom coroutine sleep.

class BGExecutor : public async_simple::executors::SimpleExecutor {
 public:
  static StatusOr<std::unique_ptr<BGExecutor>> NewBGExecutor(
      size_t thread_num) noexcept {
    auto ptr = std::make_unique<BGExecutor>(thread_num);
    auto s = ptr->Init();
    if (!s.ok()) {
      return s;
    }
    return ptr;
  }

  ~BGExecutor() override = default;

  static void TimerTaskFn(void* arg) {
    auto fptr = std::unique_ptr<std::function<void()>>(
        static_cast<std::function<void()>*>(arg));
    (*fptr)();
  }

  BGExecutor(size_t thread_num)
      : async_simple::executors::SimpleExecutor(thread_num) {}

 private:
  Status Init() {
    bthread::TimerThreadOptions options;
    const int rc = timer_.start(&options);
    // rc != 0 when pthread create failed
    // CHECK_IF_RET(rc == 0, Status::Err(),
    //                 "Init BGExecutor. start timer failed : {}. ", rc);
    return Status::OK();
  }

  void schedule(Func func, Duration dur) override {
    async_simple::Executor::Context ctx = checkout();
    auto fptr = std::make_unique<std::function<void()>>(
        [this, ctx, func = std::move(func)]() {
          checkin(func, ctx, async_simple::ScheduleOptions{});
        });
    const auto sec = std::chrono::duration_cast<std::chrono::seconds>(dur);
    auto ts = timespec{
        .tv_sec = sec.count(),
        .tv_nsec =
            std::chrono::duration_cast<std::chrono::nanoseconds>(dur - sec)
                .count()};
    auto* args = fptr.release();
    auto task_id = timer_.schedule(TimerTaskFn, args, ts);
    if (unlikely(task_id == bthread::TimerThread::INVALID_TASK_ID)) {
      // BG3_ERROR("schedule timer task failed");
      TimerTaskFn(args);
    }
  }

  bthread::TimerThread timer_;
};

from async_simple.

ChuanqiXu9 avatar ChuanqiXu9 commented on June 2, 2024

Closed due to no more questions. Feel free to require to reopen this.

from async_simple.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.