Showing posts with label code. Show all posts
Showing posts with label code. Show all posts

Oct 2, 2026

[coroutine] c9 idiom of cancellation.

1. Who creates the coroutine?
             ↓
2. Who owns the coroutine_handle?
             ↓
3. Who resumes it?
             ↓
4. What event causes the resume?
             ↓
5. Who destroys the frame?

// Minimal single-threaded model of C9-style cancellation.
//
// Key difference from the stop_token version: a cancelled coroutine is NEVER
// resumed. The cancel callback destroys every frame of the logical thread
// (leaf -> entry), running destructors, and then resumes an "on_exit" handle
// so the owner learns the thread is gone. No code checks a token.
//
// Name map to google3/util/c9/internal:
//   PromiseLink      -> CoroutinePromiseLink (waiter link, co_thread)
//   CoThread         -> CoThread (cancelled_ AsyncNotification, on_exit)
//   SuspensionToken  -> SuspensionToken (BecameReady/Cancelled/AlreadyCancelled,
//                       DestroyCallStack)
//   Scheduler        -> ThreadState ready queue + Schedule/CancelWaiter
//   Yield / CondVar  -> leaf awaitables like c9::Event::Awaitable

#include <algorithm>
#include <coroutine>
#include <deque>
#include <exception>
#include <functional>
#include <iostream>
#include <optional>
#include <type_traits>
#include <utility>

struct FrameCounter {
  static inline int live = 0;
  FrameCounter() { ++live; }
  ~FrameCounter() { --live; }
};

class CoThread;

// Base of every Co<T> promise. The `waiter` pointers form the intrusive
// "coroutine call stack" that cancellation walks and destroys.
struct PromiseLink {
  std::coroutine_handle<> self;    // this frame
  PromiseLink* waiter = nullptr;   // caller frame; nullptr for the entry frame
  CoThread* co_thread = nullptr;   // logical thread this frame belongs to
};

// One logical thread of execution = one chain of nested co_awaits.
class CoThread {
 public:
  explicit CoThread(std::coroutine_handle<> on_exit) : on_exit_(on_exit) {}

  std::coroutine_handle<> on_exit() const { return on_exit_; }

  // Called by the leaf awaitable while suspending. False = already cancelled,
  // so the leaf must not park; it must destroy the stack instead.
  bool Register(std::function<void()> on_cancel) {
    if (cancelled_) return false;
    on_cancel_ = std::move(on_cancel);
    return true;
  }
  void Unregister() { on_cancel_ = nullptr; }

  // Request cancellation. If a leaf is parked, its callback runs now and
  // destroys the whole chain. If the chain is currently running, nothing
  // happens here; it dies at its next suspension point (Register -> false).
  void Cancel() {
    cancelled_ = true;
    if (on_cancel_) std::exchange(on_cancel_, nullptr)();
  }

 private:
  std::coroutine_handle<> on_exit_;
  bool cancelled_ = false;
  std::function<void()> on_cancel_;
};

// Handed to a leaf awaitable at suspension. Exactly one of BecameReady,
// Cancelled, AlreadyCancelled is called for each suspension.
class SuspensionToken {
 public:
  explicit SuspensionToken(PromiseLink* leaf) : leaf_(leaf) {}

  bool operator==(const SuspensionToken&) const = default;

  bool RegisterForCancellation(std::function<void()> on_cancel) {
    return co_thread().Register(std::move(on_cancel));
  }

  // Normal wakeup: stop listening for cancel; caller resumes the leaf.
  std::coroutine_handle<> BecameReady() {
    co_thread().Unregister();
    return leaf_->self;
  }

  // Cancel callback won the race: destroy the chain, tell the owner. The leaf
  // coroutine is never resumed.
  void Cancelled() {
    std::coroutine_handle<> on_exit = co_thread().on_exit();  // read first
    DestroyCallStack();                                       // frees leaf_
    on_exit.resume();
  }

  // Register failed (cancelled before we could park): destroy the chain and
  // return on_exit for the caller to symmetric-transfer into.
  std::coroutine_handle<> AlreadyCancelled() {
    std::coroutine_handle<> on_exit = co_thread().on_exit();
    DestroyCallStack();
    return on_exit;
  }

 private:
  CoThread& co_thread() { return *leaf_->co_thread; }

  // Callee before caller, iteratively, so OS stack depth stays O(1).
  void DestroyCallStack() {
    for (PromiseLink* p = leaf_; p != nullptr;) {
      PromiseLink* next = p->waiter;
      p->self.destroy();  // runs this frame's destructors (RAII)
      p = next;
    }
  }

  PromiseLink* leaf_;
};

// Ready queue of wakeups. Holds tokens, not raw handles, so BecameReady runs
// (unregistering the cancel callback) right before resume.
class Scheduler {
 public:
  void Schedule(SuspensionToken t) { ready_.push_back(t); }

  // Pull a scheduled wakeup back out so the frame can be destroyed instead.
  // False = already dequeued and running; cancellation then takes effect at
  // the coroutine's next suspension point.
  bool CancelWaiter(const SuspensionToken& t) {
    return std::erase(ready_, t) > 0;
  }

  void Run() {
    while (!ready_.empty()) {
      SuspensionToken t = ready_.front();
      ready_.pop_front();
      t.BecameReady().resume();
    }
  }

 private:
  std::deque<SuspensionToken> ready_;
};

// Awaitable child coroutine. Lazy start, symmetric transfer, frame destroys
// itself at final_suspend (result is written into the parent's frame first).
template <typename T>
class Co {
  static constexpr bool kVoid = std::is_void_v<T>;
  struct Empty {};
  using Slot = std::conditional_t<kVoid, Empty, std::optional<T>>;

  template <typename U>
  struct ReturnValue {
    std::optional<U>* result = nullptr;  // lives in the awaiting parent
    void return_value(U v) { result->emplace(std::move(v)); }
  };
  struct ReturnVoid {
    void return_void() {}
  };

 public:
  struct promise_type
      : PromiseLink,
        FrameCounter,
        std::conditional_t<kVoid, ReturnVoid, ReturnValue<T>> {
    Co get_return_object() {
      auto h = std::coroutine_handle<promise_type>::from_promise(*this);
      this->self = h;
      return Co(h);
    }
    std::suspend_always initial_suspend() noexcept { return {}; }
    auto final_suspend() noexcept {
      struct Final {
        bool await_ready() noexcept { return false; }
        std::coroutine_handle<> await_suspend(
            std::coroutine_handle<promise_type> h) noexcept {
          PromiseLink* waiter = h.promise().waiter;
          std::coroutine_handle<> on_exit = h.promise().co_thread->on_exit();
          h.destroy();  // frame gone; only locals used from here on
          return waiter != nullptr ? waiter->self : on_exit;
        }
        void await_resume() noexcept {}
      };
      return Final{};
    }
    void unhandled_exception() { std::terminate(); }
  };

  explicit Co(std::coroutine_handle<promise_type> h) : h_(h) {}
  Co(Co&& o) noexcept : h_(std::exchange(o.h_, {})) {}
  ~Co() {
    if (h_) h_.destroy();  // only if never started
  }

  bool await_ready() const noexcept { return false; }
  template <typename P>
  std::coroutine_handle<> await_suspend(std::coroutine_handle<P> parent) {
    promise_type& p = h_.promise();
    p.waiter = &parent.promise();
    p.co_thread = parent.promise().co_thread;
    if constexpr (!kVoid) p.result = &result_;
    return std::exchange(h_, {});  // frame now owns itself
  }
  T await_resume() {
    if constexpr (!kVoid) return std::move(*result_);
  }

  // For bridges only: hand the entry frame to a new CoThread.
  std::coroutine_handle<promise_type> Release() {
    return std::exchange(h_, {});
  }

 private:
  std::coroutine_handle<promise_type> h_;
  Slot result_;
};

// Tiny coroutine used as a CoThread's on_exit handle.
struct OnExit {
  struct promise_type : FrameCounter {
    OnExit get_return_object() {
      return {std::coroutine_handle<promise_type>::from_promise(*this)};
    }
    std::suspend_always initial_suspend() noexcept { return {}; }
    std::suspend_never final_suspend() noexcept { return {}; }
    void return_void() {}
    void unhandled_exception() { std::terminate(); }
  };
  std::coroutine_handle<> handle;
};

OnExit ReportExit(const char* name) {
  std::cout << name << ": thread exited\n";
  co_return;
}

// Bridge: start `co` as the entry frame of a fresh CoThread (what C9 bridges,
// RunConcurrently and WithDeadline do for their children).
void Spawn(Scheduler& sched, CoThread& ct, Co<void> co) {
  auto h = co.Release();
  h.promise().co_thread = &ct;  // waiter stays nullptr: entry frame
  sched.Schedule(SuspensionToken(&h.promise()));
}

// ---- Leaf awaitables (the only place cancellation is "implemented") ----

// Re-queue the current coroutine (simulates work). Cancellable.
class Yield {
 public:
  explicit Yield(Scheduler& s) : sched_(s) {}
  bool await_ready() const noexcept { return false; }
  template <typename P>
  std::coroutine_handle<> await_suspend(std::coroutine_handle<P> h) {
    token_.emplace(&h.promise());
    if (!token_->RegisterForCancellation([this] { Cancel(); })) {
      return token_->AlreadyCancelled();
    }
    sched_.Schedule(*token_);
    return std::noop_coroutine();
  }
  void await_resume() const noexcept {}

 private:
  void Cancel() {
    // Copy out: Cancelled() destroys the frame that holds *this.
    Scheduler& sched = sched_;
    SuspensionToken token = *token_;
    if (sched.CancelWaiter(token)) token.Cancelled();
  }
  Scheduler& sched_;
  std::optional<SuspensionToken> token_;
};

// Condition variable. Note Wait() takes no token: cancellation is implicit.
class CondVar {
 public:
  explicit CondVar(Scheduler& sched) : sched_(sched) {}

  class Awaiter {
   public:
    explicit Awaiter(CondVar& cv) : cv_(cv) {}
    bool await_ready() const noexcept { return false; }
    template <typename P>
    std::coroutine_handle<> await_suspend(std::coroutine_handle<P> h) {
      token_.emplace(&h.promise());
      if (!token_->RegisterForCancellation([this] { Cancel(); })) {
        return token_->AlreadyCancelled();
      }
      cv_.waiters_.push_back(*token_);
      return std::noop_coroutine();
    }
    void await_resume() const noexcept {}

   private:
    void Cancel() {
      CondVar& cv = cv_;
      SuspensionToken token = *token_;
      // Either still parked here, or NotifyOne already moved it to the ready
      // queue. Pull it back from wherever it is, then destroy.
      if (std::erase(cv.waiters_, token) > 0 || cv.sched_.CancelWaiter(token)) {
        token.Cancelled();
      }
    }
    CondVar& cv_;
    std::optional<SuspensionToken> token_;
  };

  Awaiter Wait() { return Awaiter(*this); }

  void NotifyOne() {
    if (waiters_.empty()) return;
    sched_.Schedule(waiters_.front());
    waiters_.pop_front();
  }

 private:
  Scheduler& sched_;
  std::deque<SuspensionToken> waiters_;
};

// ---- User code: no token anywhere ----

class Channel {
 public:
  explicit Channel(Scheduler& sched) : cv_(sched) {}
  void Push(int v) {
    items_.push_back(v);
    cv_.NotifyOne();
  }
  Co<int> Pop() {
    while (items_.empty()) co_await cv_.Wait();
    int v = items_.front();
    items_.pop_front();
    co_return v;
  }

 private:
  std::deque<int> items_;
  CondVar cv_;
};

Co<void> Consumer(Scheduler& sched, Channel& ch, int id) {
  struct Cleanup {
    int id;
    ~Cleanup() { std::cout << "consumer " << id << " cleanup (RAII)\n"; }
  } cleanup{id};

  for (;;) {  // no exit path: cancellation destroys this frame instead
    int item = co_await ch.Pop();
    std::cout << "consumer " << id << " processes " << item << "\n";
    co_await Yield(sched);
  }
}

Co<void> Producer(Scheduler& sched, Channel& ch) {
  for (int i = 0;; ++i) {
    std::cout << "producer pushes " << i << "\n";
    ch.Push(i);
    co_await Yield(sched);
  }
}

Co<void> StopAfter(Scheduler& sched, std::deque<CoThread*> threads, int ticks) {
  for (int i = 0; i < ticks; ++i) co_await Yield(sched);
  std::cout << "=== cancel requested ===\n";
  for (CoThread* t : threads) t->Cancel();  // callbacks destroy the stacks
}

int main() {
  Scheduler sched;
  Channel ch(sched);

  CoThread c1(ReportExit("consumer 1").handle);
  CoThread c2(ReportExit("consumer 2").handle);
  CoThread prod(ReportExit("producer").handle);
  CoThread stopper(ReportExit("stopper").handle);

  Spawn(sched, c1, Consumer(sched, ch, 1));
  Spawn(sched, c2, Consumer(sched, ch, 2));
  Spawn(sched, prod, Producer(sched, ch));
  Spawn(sched, stopper, StopAfter(sched, {&c1, &c2, &prod}, 3));

  sched.Run();

  std::cout << "live coroutine frames after Run(): " << FrameCounter::live
            << "\n";
  return FrameCounter::live == 0 ? 0 : 1;
}

Sep 29, 2026

[coroutine] event/notify example


#include <coroutine>
#include <deque>
#include <exception>
#include <iostream>
#include <utility>

// Single-threaded scheduler: a queue of coroutines ready to resume.
class Scheduler {
 public:
  void Schedule(std::coroutine_handle<> h) {
    std::cout << "  [queue push_back " << h.address() << "]\n";
    ready_.push_back(h);
  }

  void Run() {
    while (!ready_.empty()) {
      std::coroutine_handle<> h = ready_.front();
      ready_.pop_front();
      std::cout << "  [queue pop_front -> resume " << h.address() << "]\n";
      h.resume();
    }
  }

 private:
  std::deque<std::coroutine_handle<>> ready_;
};

// Coroutine condition variable. Wait() parks the handle;
// Notify*() moves parked handles to the scheduler's ready queue.
class CondVar {
 public:
  explicit CondVar(Scheduler& sched) : sched_(sched) {}

  auto Wait() {
    struct Awaiter {
      CondVar& cv;
      bool await_ready() const noexcept { return false; }
      void await_suspend(std::coroutine_handle<> h) {
        std::cout << "  [cv parks " << h.address() << "]\n";
        cv.waiters_.push_back(h);
      }
      void await_resume() const noexcept {}
    };
    return Awaiter{*this};
  }

  void NotifyOne() {
    if (waiters_.empty()) return;
    sched_.Schedule(waiters_.front());
    waiters_.pop_front();
  }

  void NotifyAll() {
    while (!waiters_.empty()) NotifyOne();
  }

 private:
  Scheduler& sched_;
  std::deque<std::coroutine_handle<>> waiters_;
};

// Fire-and-forget top-level coroutine (unchanged from the original).
struct Task {
  struct promise_type {
    Task get_return_object() { return {}; }
    std::suspend_never initial_suspend() noexcept { return {}; }
    std::suspend_never final_suspend() noexcept { return {}; }
    void return_void() {}
    void unhandled_exception() { std::terminate(); }
  };
};

// Awaitable child coroutine: lazy start + symmetric transfer both ways.
struct Co {
  struct promise_type {
    std::coroutine_handle<> continuation;  // parent to resume when done

    Co get_return_object() {
      return Co{std::coroutine_handle<promise_type>::from_promise(*this)};
    }
    // Don't run until the parent co_awaits us (so continuation is set).
    std::suspend_always initial_suspend() noexcept { return {}; }

    auto final_suspend() noexcept {
      struct Final {
        bool await_ready() noexcept { return false; }
        std::coroutine_handle<> await_suspend(
            std::coroutine_handle<promise_type> h) noexcept {
          return h.promise().continuation;  // jump straight back to parent
        }
        void await_resume() noexcept {}
      };
      return Final{};
    }
    void return_void() {}
    void unhandled_exception() { std::terminate(); }
  };

  explicit Co(std::coroutine_handle<promise_type> h) : h_(h) {}
  Co(Co&& o) noexcept : h_(std::exchange(o.h_, {})) {}
  ~Co() {
    if (h_) h_.destroy();  // frame lives until the co_await expression ends
  }

  bool await_ready() const noexcept { return false; }
  std::coroutine_handle<> await_suspend(std::coroutine_handle<> parent) {
    h_.promise().continuation = parent;
    return h_;  // jump straight into child; no queue
  }
  void await_resume() const noexcept {}

  std::coroutine_handle<promise_type> h_;
};

bool data_ready = false;

Co WaitForData(CondVar& cv, int id) {
  while (!data_ready) {  // Predicate loop, same as std::condition_variable.
    std::cout << "child " << id << " waits\n";
    co_await cv.Wait();
  }
  std::cout << "child " << id << " sees data\n";
}

Task Consumer(CondVar& cv, int id) {
  std::cout << "consumer " << id << " calls child\n";
  co_await WaitForData(cv, id);  // nested co_await
  std::cout << "consumer " << id << " woke up\n";
}

Task Producer(CondVar& cv) {
  data_ready = true;
  std::cout << "producer notifies\n";
  cv.NotifyAll();
  co_return;
}

int main() {
  Scheduler sched;
  CondVar cv(sched);
  Consumer(cv, 1);
  Consumer(cv, 2);
  Producer(cv);
  std::cout << "--- sched.Run() ---\n";
  sched.Run();
}


In a C++20 coroutine, a suspension point is any location where the coroutine can pause execution, save its local state to the heap-allocated coroutine frame, and return control to its caller or resumer.

There are four types of suspension points, split between those you write explicitly in the function body and those the compiler injects automatically around your code.


Where They Live: Compiler Transformation

When write a coroutine, the C++20 compiler rewrites its body into a state machine wrapped in boilerplate that invokes your promise_type. Every coroutine contains at least the two implicit suspension points, plus any explicit ones we write:



// What the compiler generates behind the scenes for your coroutine:
{
    promise_type promise;
    auto return_object = promise.get_return_object();

    // --- 1. IMPLICIT: Initial Suspension Point ---
    co_await promise.initial_suspend();

    try {
        // --- YOUR COROUTINE BODY STARTS HERE ---
        
        // --- 2. EXPLICIT: co_await Suspension Point ---
        auto data = co_await async_read();

        // --- 3. EXPLICIT: co_yield Suspension Point ---
        co_yield data; // Rewritten as: co_await promise.yield_value(data);

        // co_return is NOT a suspension point; it sets the result and jumps to final_suspend
        co_return;     // Calls promise.return_void() and goto final_suspend;
        
        // --- YOUR COROUTINE BODY ENDS HERE ---
    } catch (...) {
        promise.unhandled_exception();
    }

final_suspend:
    // --- 4. IMPLICIT: Final Suspension Point ---
    co_await promise.final_suspend();
} // Coroutine frame is automatically destroyed here ONLY if final_suspend does not suspend

Does a Suspension Point Always Suspend?

Hitting a suspension point means the coroutine may suspend, not that it must suspend. At every co_await (including those generated by co_yield, initial_suspend, and final_suspend), the compiler queries an Awaiter object using a 3-step protocol:

  1. awaiter.await_ready(): Checked first as a fast-path optimization.

    • If it returns true, the result is already available. The coroutine does not suspend and immediately calls await_resume().

    • If it returns false, the coroutine prepares to suspend by saving its instruction pointer and local registers into the coroutine frame.

  2. awaiter.await_suspend(handle): Called right after the coroutine state is saved. Its return type controls what happens next:

    • void: Truly suspends the coroutine and returns control to the caller/resumer.

    • bool: Returning true suspends and returns to the caller; returning false aborts the suspension and immediately resumes the coroutine on the current thread.

    • std::coroutine_handle: Suspends the current coroutine and immediately resumes the returned handle via symmetric transfer (without growing the call stack).

  3. awaiter.await_resume(): Called once the coroutine is resumed (or immediately if await_ready() returned true). Its return value becomes the result of the co_await expression.


#include <coroutine>
#include <deque>
#include <exception>
#include <iostream>
#include <optional>
#include <stop_token>
#include <utility>

// Counts live coroutine frames so main() can prove nothing leaked.
struct FrameCounter {
  static inline int live = 0;
  FrameCounter() { ++live; }
  ~FrameCounter() { --live; }
};<

// Single-threaded scheduler: a queue of coroutines ready to resume.
class Scheduler {
 public:
  void Schedule(std::coroutine_handle<> h) { ready_.push_back(h); }

  // Returns when no coroutine is runnable. After a stop request, that means
  // every task has exited (graceful shutdown), unless one is stuck on
  // something that ignores the stop token.
  void Run() {
    while (!ready_.empty()) {
      std::coroutine_handle<> h = ready_.front();
      ready_.pop_front();
      h.resume();
    }
  }

 private:
  std::deque<std::coroutine_handle<>> ready_;
};

// Re-queues the current coroutine so others get a turn (simulates work).
auto Yield(Scheduler& sched) {
  struct Awaiter {
    Scheduler& sched;
    bool await_ready() const noexcept { return false; }
    void await_suspend(std::coroutine_handle<> h) { sched.Schedule(h); }
    void await_resume() const noexcept {}
  };
  return Awaiter{sched};
}

// Coroutine condition variable with cancellation.
// Wait(token) parks the handle. It is woken by NotifyOne/NotifyAll OR by
// token's stop request, whichever comes first. Callers must re-check their
// predicate and the token after waking (same as std::condition_variable_any).
class CondVar {
 public:
  explicit CondVar(Scheduler& sched) : sched_(sched) {}

  class WaitAwaiter {
   public:
    WaitAwaiter(CondVar& cv, std::stop_token token)
        : cv_(cv), token_(std::move(token)) {}

    // Already stopped: don't suspend at all.
    bool await_ready() const noexcept { return token_.stop_requested(); }

    void await_suspend(std::coroutine_handle<> h) {
      h_ = h;
      cv_.waiters_.push_back(h);
      // Runs OnStop inline if stop was already requested. The callback is
      // deregistered when this awaiter is destroyed (end of co_await).
      on_stop_.emplace(token_, OnStop{this});
    }

    void await_resume() const noexcept {}

   private:
    struct OnStop {
      WaitAwaiter* self;
      void operator()() const noexcept { self->cv_.CancelWait(self->h_); }
    };

    CondVar& cv_;
    std::stop_token token_;
    std::coroutine_handle<> h_;
    std::optional<std::stop_callback<OnStop>> on_stop_;
  };

  WaitAwaiter Wait(std::stop_token token) {
    return WaitAwaiter(*this, std::move(token));
  }

  void NotifyOne() {
    if (waiters_.empty()) return;
    sched_.Schedule(waiters_.front());
    waiters_.pop_front();
  }

  void NotifyAll() {
    while (!waiters_.empty()) NotifyOne();
  }

 private:
  // Wakes h only if it is still parked here. If NotifyOne already moved it
  // to the ready queue, do nothing; scheduling it twice would resume a
  // coroutine that is not suspended (UB).
  void CancelWait(std::coroutine_handle<> h) {
    for (auto it = waiters_.begin(); it != waiters_.end(); ++it) {
      if (*it == h) {
        waiters_.erase(it);
        sched_.Schedule(h);
        return;
      }
    }
  }

  Scheduler& sched_;
  std::deque<std::coroutine_handle<>> waiters_;
};

// Fire-and-forget top-level coroutine. Frame self-destroys at the end.
struct Task {
  struct promise_type : FrameCounter {
    Task get_return_object() { return {}; }
    std::suspend_never initial_suspend() noexcept { return {}; }
    std::suspend_never final_suspend() noexcept { return {}; }
    void return_void() {}
    void unhandled_exception() { std::terminate(); }
  };
};

// Awaitable child coroutine returning T: lazy start + symmetric transfer.
template <typename T>
class Co {
 public:
  struct promise_type : FrameCounter {
    std::coroutine_handle<> continuation;  // parent to resume when done
    std::optional<T> result;

    Co get_return_object() {
      return Co(std::coroutine_handle<promise_type>::from_promise(*this));
    }
    std::suspend_always initial_suspend() noexcept { return {}; }
    auto final_suspend() noexcept {
      struct Final {
        bool await_ready() noexcept { return false; }
        std::coroutine_handle<> await_suspend(
            std::coroutine_handle<promise_type> h) noexcept {
          return h.promise().continuation;  // jump straight back to parent
        }
        void await_resume() noexcept {}
      };
      return Final{};
    }
    void return_value(T v) { result.emplace(std::move(v)); }
    void unhandled_exception() { std::terminate(); }
  };

  explicit Co(std::coroutine_handle<promise_type> h) : h_(h) {}
  Co(Co&& o) noexcept : h_(std::exchange(o.h_, {})) {}
  ~Co() {
    if (h_) h_.destroy();
  }

  bool await_ready() const noexcept { return false; }
  // Parent 就是之後的 await_resume resume 的handle.
  // await_suspend 吃的handle就是await_resume resume的handle.
  std::coroutine_handle<> await_suspend(std::coroutine_handle<> parent) {
    h_.promise().continuation = parent;
    return h_;  // jump straight into child; no queue
  }
  
  // await_suspend 吃的handle就是await_resume resume的handle.
  // co_await 回傳值在此. 
  // 若 coroutune function return的type 也為 waitable
  // 其promise_type::void return_value(T v) 會將return value儲存至
  // promise_type, 其final_suspend() return waitable type會resume parent 
  // 跳至此await_resume().
  T await_resume() { return std::move(*h_.promise().result); }

 private:
  std::coroutine_handle<promise_type> h_;
};

// Unbounded work queue.
class Channel {
 public:
  explicit Channel(Scheduler& sched) : cv_(sched) {}

  void Push(int v) {
    items_.push_back(v);
    cv_.NotifyOne();
  }

  // Graceful semantics: keeps handing out queued items even after stop, and
  // returns nullopt only when the queue is empty AND stop was requested.
  Co<std::optional<int>> Pop(std::stop_token token) {
    while (items_.empty()) {
      if (token.stop_requested()) co_return std::nullopt;
      co_await cv_.Wait(token);  // nested co_await; parks THIS child frame
    }
    int v = items_.front();
    items_.pop_front();
    co_return v;
  }

 private:
  std::deque<int> items_;
  CondVar cv_;
};

Task Consumer(Scheduler& sched, Channel& ch, int id, std::stop_token token) {
  struct Cleanup {  // proves destructors run on shutdown
    int id;
    ~Cleanup() { std::cout << "consumer " << id << " cleanup (RAII)\n"; }
  } cleanup{id};

  while (std::optional<int> item = co_await ch.Pop(token)) {
    std::cout << "consumer " << id << " processes " << *item << "\n";
    co_await Yield(sched);  // simulate work
  }
  std::cout << "consumer " << id << " exits\n";
}

Task Producer(Scheduler& sched, Channel& ch, std::stop_token token) {
  for (int i = 0; !token.stop_requested(); ++i) {
    std::cout << "producer pushes " << i << "\n";
    ch.Push(i);
    co_await Yield(sched);
  }
  std::cout << "producer exits (stop requested)\n";
}

// Stands in for a SIGTERM handler / deadline: requests stop after `ticks`.
Task StopAfter(Scheduler& sched, std::stop_source& stop, int ticks) {
  for (int i = 0; i < ticks; ++i) co_await Yield(sched);
  std::cout << "=== shutdown requested ===\n";
  stop.request_stop();  // wakes parked waiters via their stop_callbacks
}

int main() {
  Scheduler sched;
  Channel ch(sched);
  std::stop_source stop;

  Consumer(sched, ch, 1, stop.get_token());
  Consumer(sched, ch, 2, stop.get_token());
  Producer(sched, ch, stop.get_token());
  StopAfter(sched, stop, 3);

  sched.Run();  // returns once every task has exited

  std::cout << "live coroutine frames after Run(): " << FrameCounter::live
            << "\n";
  return FrameCounter::live == 0 ? 0 : 1;
}

// Minimal single-threaded model of C9-style cancellation.
//
// Key difference from the stop_token version: a cancelled coroutine is NEVER
// resumed. The cancel callback destroys every frame of the logical thread
// (leaf -> entry), running destructors, and then resumes an "on_exit" handle
// so the owner learns the thread is gone. No code checks a token.
//
// Name map to google3/util/c9/internal:
//   PromiseLink      -> CoroutinePromiseLink (waiter link, co_thread)
//   CoThread         -> CoThread (cancelled_ AsyncNotification, on_exit)
//   SuspensionToken  -> SuspensionToken (BecameReady/Cancelled/AlreadyCancelled,
//                       DestroyCallStack)
//   Scheduler        -> ThreadState ready queue + Schedule/CancelWaiter
//   Yield / CondVar  -> leaf awaitables like c9::Event::Awaitable

#include <algorithm>
#include <coroutine>
#include <deque>
#include <exception>
#include <functional>
#include <iostream>
#include <optional>
#include <type_traits>
#include <utility>

struct FrameCounter {
  static inline int live = 0;
  FrameCounter() { ++live; }
  ~FrameCounter() { --live; }
};

class CoThread;

// Base of every Co<T> promise. The `waiter` pointers form the intrusive
// "coroutine call stack" that cancellation walks and destroys.
struct PromiseLink {
  std::coroutine_handle<> self;    // this frame
  PromiseLink* waiter = nullptr;   // caller frame; nullptr for the entry frame
  CoThread* co_thread = nullptr;   // logical thread this frame belongs to
};

// One logical thread of execution = one chain of nested co_awaits.
class CoThread {
 public:
  explicit CoThread(std::coroutine_handle<> on_exit) : on_exit_(on_exit) {}

  std::coroutine_handle<> on_exit() const { return on_exit_; }

  // Called by the leaf awaitable while suspending. False = already cancelled,
  // so the leaf must not park; it must destroy the stack instead.
  bool Register(std::function<void()> on_cancel) {
    if (cancelled_) return false;
    on_cancel_ = std::move(on_cancel);
    return true;
  }
  void Unregister() { on_cancel_ = nullptr; }

  // Request cancellation. If a leaf is parked, its callback runs now and
  // destroys the whole chain. If the chain is currently running, nothing
  // happens here; it dies at its next suspension point (Register -> false).
  void Cancel() {
    cancelled_ = true;
    if (on_cancel_) std::exchange(on_cancel_, nullptr)();
  }

 private:
  std::coroutine_handle<> on_exit_;
  bool cancelled_ = false;
  std::function<void()> on_cancel_;
};

// Handed to a leaf awaitable at suspension. Exactly one of BecameReady,
// Cancelled, AlreadyCancelled is called for each suspension.
class SuspensionToken {
 public:
  explicit SuspensionToken(PromiseLink* leaf) : leaf_(leaf) {}

  bool operator==(const SuspensionToken&) const = default;

  bool RegisterForCancellation(std::function<void()> on_cancel) {
    return co_thread().Register(std::move(on_cancel));
  }

  // Normal wakeup: stop listening for cancel; caller resumes the leaf.
  std::coroutine_handle<> BecameReady() {
    co_thread().Unregister();
    return leaf_->self;
  }

  // Cancel callback won the race: destroy the chain, tell the owner. The leaf
  // coroutine is never resumed.
  void Cancelled() {
    std::coroutine_handle<> on_exit = co_thread().on_exit();  // read first
    DestroyCallStack();                                       // frees leaf_
    on_exit.resume();
  }

  // Register failed (cancelled before we could park): destroy the chain and
  // return on_exit for the caller to symmetric-transfer into.
  std::coroutine_handle<> AlreadyCancelled() {
    std::coroutine_handle<> on_exit = co_thread().on_exit();
    DestroyCallStack();
    return on_exit;
  }

 private:
  CoThread& co_thread() { return *leaf_->co_thread; }

  // Callee before caller, iteratively, so OS stack depth stays O(1).
  void DestroyCallStack() {
    for (PromiseLink* p = leaf_; p != nullptr;) {
      PromiseLink* next = p->waiter;
      p->self.destroy();  // runs this frame's destructors (RAII)
      p = next;
    }
  }

  PromiseLink* leaf_;
};

// Ready queue of wakeups. Holds tokens, not raw handles, so BecameReady runs
// (unregistering the cancel callback) right before resume.
class Scheduler {
 public:
  void Schedule(SuspensionToken t) { ready_.push_back(t); }

  // Pull a scheduled wakeup back out so the frame can be destroyed instead.
  // False = already dequeued and running; cancellation then takes effect at
  // the coroutine's next suspension point.
  bool CancelWaiter(const SuspensionToken& t) {
    return std::erase(ready_, t) > 0;
  }

  void Run() {
    while (!ready_.empty()) {
      SuspensionToken t = ready_.front();
      ready_.pop_front();
      t.BecameReady().resume();
    }
  }

 private:
  std::deque<SuspensionToken> ready_;
};

// Awaitable child coroutine. Lazy start, symmetric transfer, frame destroys
// itself at final_suspend (result is written into the parent's frame first).
template <typename T>
class Co {
  static constexpr bool kVoid = std::is_void_v<T>;
  struct Empty {};
  using Slot = std::conditional_t<kVoid, Empty, std::optional<T>>;

  template <typename U>
  struct ReturnValue {
    std::optional<U>* result = nullptr;  // lives in the awaiting parent
    void return_value(U v) { result->emplace(std::move(v)); }
  };
  struct ReturnVoid {
    void return_void() {}
  };

 public:
  struct promise_type
      : PromiseLink,
        FrameCounter,
        std::conditional_t<kVoid, ReturnVoid, ReturnValue<T>> {
    Co get_return_object() {
      auto h = std::coroutine_handle<promise_type>::from_promise(*this);
      this->self = h;
      return Co(h);
    }
    std::suspend_always initial_suspend() noexcept { return {}; }
    auto final_suspend() noexcept {
      struct Final {
        bool await_ready() noexcept { return false; }
        std::coroutine_handle<> await_suspend(
            std::coroutine_handle<promise_type> h) noexcept {
          PromiseLink* waiter = h.promise().waiter;
          std::coroutine_handle<> on_exit = h.promise().co_thread->on_exit();
          h.destroy();  // frame gone; only locals used from here on
          return waiter != nullptr ? waiter->self : on_exit;
        }
        void await_resume() noexcept {}
      };
      return Final{};
    }
    void unhandled_exception() { std::terminate(); }
  };

  explicit Co(std::coroutine_handle<promise_type> h) : h_(h) {}
  Co(Co&& o) noexcept : h_(std::exchange(o.h_, {})) {}
  ~Co() {
    if (h_) h_.destroy();  // only if never started
  }

  bool await_ready() const noexcept { return false; }
  template <typename P>
  std::coroutine_handle<> await_suspend(std::coroutine_handle<P> parent) {
    promise_type& p = h_.promise();
    p.waiter = &parent.promise();
    p.co_thread = parent.promise().co_thread;
    if constexpr (!kVoid) p.result = &result_;
    return std::exchange(h_, {});  // frame now owns itself
  }
  T await_resume() {
    if constexpr (!kVoid) return std::move(*result_);
  }

  // For bridges only: hand the entry frame to a new CoThread.
  std::coroutine_handle<promise_type> Release() {
    return std::exchange(h_, {});
  }

 private:
  std::coroutine_handle<promise_type> h_;
  Slot result_;
};

// Tiny coroutine used as a CoThread's on_exit handle.
struct OnExit {
  struct promise_type : FrameCounter {
    OnExit get_return_object() {
      return {std::coroutine_handle<promise_type>::from_promise(*this)};
    }
    std::suspend_always initial_suspend() noexcept { return {}; }
    std::suspend_never final_suspend() noexcept { return {}; }
    void return_void() {}
    void unhandled_exception() { std::terminate(); }
  };
  std::coroutine_handle<> handle;
};

OnExit ReportExit(const char* name) {
  std::cout << name << ": thread exited\n";
  co_return;
}

// Bridge: start `co` as the entry frame of a fresh CoThread (what C9 bridges,
// RunConcurrently and WithDeadline do for their children).
void Spawn(Scheduler& sched, CoThread& ct, Co<void> co) {
  auto h = co.Release();
  h.promise().co_thread = &ct;  // waiter stays nullptr: entry frame
  sched.Schedule(SuspensionToken(&h.promise()));
}

// ---- Leaf awaitables (the only place cancellation is "implemented") ----

// Re-queue the current coroutine (simulates work). Cancellable.
class Yield {
 public:
  explicit Yield(Scheduler& s) : sched_(s) {}
  bool await_ready() const noexcept { return false; }
  template <typename P>
  std::coroutine_handle<> await_suspend(std::coroutine_handle<P> h) {
    token_.emplace(&h.promise());
    if (!token_->RegisterForCancellation([this] { Cancel(); })) {
      return token_->AlreadyCancelled();
    }
    sched_.Schedule(*token_);
    return std::noop_coroutine();
  }
  void await_resume() const noexcept {}

 private:
  void Cancel() {
    // Copy out: Cancelled() destroys the frame that holds *this.
    Scheduler& sched = sched_;
    SuspensionToken token = *token_;
    if (sched.CancelWaiter(token)) token.Cancelled();
  }
  Scheduler& sched_;
  std::optional<SuspensionToken> token_;
};

// Condition variable. Note Wait() takes no token: cancellation is implicit.
class CondVar {
 public:
  explicit CondVar(Scheduler& sched) : sched_(sched) {}

  class Awaiter {
   public:
    explicit Awaiter(CondVar& cv) : cv_(cv) {}
    bool await_ready() const noexcept { return false; }
    template <typename P>
    std::coroutine_handle<> await_suspend(std::coroutine_handle<P> h) {
      token_.emplace(&h.promise());
      if (!token_->RegisterForCancellation([this] { Cancel(); })) {
        return token_->AlreadyCancelled();
      }
      cv_.waiters_.push_back(*token_);
      return std::noop_coroutine();
    }
    void await_resume() const noexcept {}

   private:
    void Cancel() {
      CondVar& cv = cv_;
      SuspensionToken token = *token_;
      // Either still parked here, or NotifyOne already moved it to the ready
      // queue. Pull it back from wherever it is, then destroy.
      if (std::erase(cv.waiters_, token) > 0 || cv.sched_.CancelWaiter(token)) {
        token.Cancelled();
      }
    }
    CondVar& cv_;
    std::optional<SuspensionToken> token_;
  };

  Awaiter Wait() { return Awaiter(*this); }

  void NotifyOne() {
    if (waiters_.empty()) return;
    sched_.Schedule(waiters_.front());
    waiters_.pop_front();
  }

 private:
  Scheduler& sched_;
  std::deque<SuspensionToken> waiters_;
};

// ---- User code: no token anywhere ----

class Channel {
 public:
  explicit Channel(Scheduler& sched) : cv_(sched) {}
  void Push(int v) {
    items_.push_back(v);
    cv_.NotifyOne();
  }
  Co<int> Pop() {
    while (items_.empty()) co_await cv_.Wait();
    int v = items_.front();
    items_.pop_front();
    co_return v;
  }

 private:
  std::deque<int> items_;
  CondVar cv_;
};

Co<void> Consumer(Scheduler& sched, Channel& ch, int id) {
  struct Cleanup {
    int id;
    ~Cleanup() { std::cout << "consumer " << id << " cleanup (RAII)\n"; }
  } cleanup{id};

  for (;;) {  // no exit path: cancellation destroys this frame instead
    int item = co_await ch.Pop();
    std::cout << "consumer " << id << " processes " << item << "\n";
    co_await Yield(sched);
  }
}

Co<void> Producer(Scheduler& sched, Channel& ch) {
  for (int i = 0;; ++i) {
    std::cout << "producer pushes " << i << "\n";
    ch.Push(i);
    co_await Yield(sched);
  }
}

Co<void> StopAfter(Scheduler& sched, std::deque<CoThread*> threads, int ticks) {
  for (int i = 0; i < ticks; ++i) co_await Yield(sched);
  std::cout << "=== cancel requested ===\n";
  for (CoThread* t : threads) t->Cancel();  // callbacks destroy the stacks
}

int main() {
  Scheduler sched;
  Channel ch(sched);

  CoThread c1(ReportExit("consumer 1").handle);
  CoThread c2(ReportExit("consumer 2").handle);
  CoThread prod(ReportExit("producer").handle);
  CoThread stopper(ReportExit("stopper").handle);

  Spawn(sched, c1, Consumer(sched, ch, 1));
  Spawn(sched, c2, Consumer(sched, ch, 2));
  Spawn(sched, prod, Producer(sched, ch));
  Spawn(sched, stopper, StopAfter(sched, {&c1, &c2, &prod}, 3));

  sched.Run();

  std::cout << "live coroutine frames after Run(): " << FrameCounter::live
            << "\n";
  return FrameCounter::live == 0 ? 0 : 1;
}

Apr 3, 2026

[SIMD][SVE] example


#include <iostream>
#include <arm_sve.h> // The magical SVE library for modern ARM chips

// A function to add two arrays together using SVE
void add_arrays_sve(float* A, float* B, float* C, int n) {
    int i = 0;
    
    // Keep looping until we have processed all 'n' elements
    while (i < n) {
        // 1. THE MAGIC TAPE (Predicate)
        // This generates a true/false mask based on how many numbers are left.
        // It tells the CPU to "turn off" slots we don't need so we don't crash.
        svbool_t mask = svwhilelt_b32(i, n);

        // 2. Load data from A and B into our stretchy vectors, using the mask
        svfloat32_t vecA = svld1_f32(mask, &A[i]);
        svfloat32_t vecB = svld1_f32(mask, &B[i]);

        // 3. Add the vectors together, safely ignoring masked-off slots
        svfloat32_t vecC = svadd_f32_z(mask, vecA, vecB);

        // 4. Store the results back into standard memory
        svst1_f32(mask, &C[i], vecC);

        // 5. THE STRETCHY PART
        // svcntw() asks the CPU: "How many 32-bit words fit in your vector?"
        // We move forward by that amount, whether it's 4, 8, 16, or 64!
        i += svcntw(); 
    }
}

int main() {
    int n = 10; // 10 numbers total
    float A[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    float B[10] = {10, 10, 10, 10, 10, 10, 10, 10, 10, 10};
    float C[10] = {0}; // Where the answers go

    add_arrays_sve(A, B, C, n);

    std::cout << "Results of SVE math: ";
    for(int i = 0; i < n; ++i) {
        std::cout << C[i] << " ";
    }
    std::cout << "\n";

    return 0;
}

Nov 25, 2025

[ACCU 2005] Learning To Stop Writing C++ Code minute

Learning To Stop Writing C++ Code (and Why You Won’t Miss It) - Daisy Hollman - ACCU 2025
https://www.youtube.com/watch?v=mpGx-_uLPDM&t=870s

Best Practices for coding with LLMs

  • Use smaller files
  • Over-test everything.
  • LLMs are pretty good at generating tests for existing code
    • But they also pretty decent at helping you with Test-Driven Development
  • Well-contained unit tests are much easier for LLMs to reason about
  • Encapsulation is critical!

LLMs currently struggle with "long-term learning"
Whereas a human working on the same project for weeks or months can abstract away the details of a complicated workflow and "learn" which poorly encapsulated sharp edges are ignorable, LLMs currently struggle with this kind of thing. (early 2025)

In other words, code coupling is bad—don't connect dissimilar things from different units of encapsulation in unintuitive ways.

  • Naming is more important than ever
  • Intuitive abstraction design goes a long ways
  • Agents often don't know to "check" for unintuitive behavior
    ...or they might "check" sometimes and not other times
  • Writing abstractions that are easy to correctly "guess" how they work is important
  • Write better (but still concise!) comments and documentation
    QUOTE
    The compiler does not read comments and neither do I — Bjarne Stroustrup
    Maybe it's time to revise this? LLMs do read comments


High cohesion is good
  • This is the opposite of code coupling—similar things within a given unit of encapsulation should be grouped together.
  • "Don't Repeat Yourself" (DRY) coding helps make efficient use of the LLM's context window
  • Don't do unexpected things
  • Especially if those things often don't have syntax (e.g., copy constructors in C++, auto-dereferencing in Rust, non-idiomatic __getattribute__ in Python, etc.)
  • In C++, use regular types whenever possible. (read my note about regular type: https://vsdmars.blogspot.com/2018/06/c-regular-type.html , basically, design by contract, precondition)
  • Don't mix owning and non-owning semantics in the same type or template
  • Don't mix value and reference semantics in the same type or template

KEY TAKEAWAY
Writing code that LLMs will understand is not that different from writing code that humans will understand, except that we can start to understand and quantify why these best practices increase understandability.


Design by contract
  • Both contracts and effects systems are ways of encapsulating information and reducing code coupling.
  • Encapsulation is key to effectively working with LLMs because of the context window size constraints.
  • But also, it's a lot easier to train LLMs on small, well-contained problems.
  • Contracts promote Liskov Substitutability, allowing LLMs to infer behavior of a broader category of types.

An "Effects System" is a way for a programming language to track what a function does (its side effects), not just what it returns (its data type).

While C++ doesn't have a full academic effects system (like the research language Koka), it has a "pragmatic" one built into keywords you use every day.

Here is how const and noexcept act as an Effects System to help both the Compiler and LLMs.

The Concept: Labeling the "Black Box"

Without an effects system, a function signature void process(T& data) is a black box. It could do anything: write to disk, throw an error, modify global state, or format your hard drive.

An effects system puts warning labels on the box.

Why LLMs Love This (The "Context" Win)
The slide mentioned "Context Window Constraints." This is where effects systems shine for AI.

If you give an LLM this code:

C++
// Case A: No "Effects"
void transform(Data& d);

The LLM has to "hallucinate" or guess: Does this function throw? Do I need a try-catch block? Does it invalidate my iterators? It has to consider all possibilities, which wastes "reasoning tokens."

If you give it this:

C++
// Case B: Constrained Effects
void transform(Data& d) noexcept;

The search space collapses. The LLM immediately knows: No exception handling logic is needed here. It can focus its limited attention on the actual logic rather than defensive coding.


Nov 9, 2025

[C++] Use string_view _sv over raw char string

Reference:
https://youtu.be/jXQ6WtYmfZw?si=B_C-UXBVCFpAODVh&t=4428


std:: string s("the foo and the bar");
std:: println("{}", std::ranges::contains_subrange(s, "foo" ));
This won't work due to C-style string literal "foo" is actually a range of four characters: 
['f', 'o', 'o', '\0'] 

Easy fix:
#include <iostream>
#include <string>
#include <string_view>
#include <ranges>
#include <print> // C++23 for std::println

int main() {
    using namespace std::literals; // Enables the "sv" suffix

    std::string s("the foo and the bar");
    
    // "foo"sv creates a std::string_view of length 3.
    // This will now print "true".
    std::println("{}", std::ranges::contains_subrange(s, "foo"sv)); 
}

or C++23:
std::string s("the foo and the bar");

// This is the simplest way and does what you expect.
// It will print "true".
std::println("{}", s.contains("foo"));

Oct 17, 2025

[C++][template] Double checked Stop technique

godbolt:
https://godbolt.org/z/Yenv16xzj

#include <functional>
#include <iostream>
#include <optional>


template <size_t I, typename F>
constexpr std::optional<int> ApplyIndexForOverflow(F f) {
    return ApplyIndexForOverflow<I - 1>(f);
}

template <size_t I, typename F>
constexpr std::optional<int> ApplyIndexFor(F f) {
    if (I == 0) {
        return std::nullopt;
    }
    // double checked stop; otherwise introduced stack overflow
    // from the compiler runtime due to has to instantiate
    // unbounded template instance, like above `ApplyIndexForOverflow`
    return ApplyIndexFor<(I == 0 ? 0 : I - 1 )>(f);
}

template <size_t I, typename F>
constexpr std::optional<int> ApplyIndexForConstexpr(F f) {
    if constexpr(sizeof(F) == 1){
        return I;
    }
    if constexpr(I - 1 == 0){
        return std::nullopt;
    } else {
        return ApplyIndexForConstexpr<I-1>(f);
    }
}

int main() {
 auto run = []{};
 ApplyIndexForOverflow<100>(run);
 ApplyIndexFor<100>(run);
 ApplyIndexForConstexpr<100>(run);
}

Oct 4, 2025

[alrotighm] trampoline pattern

using trampoline to deal with recursive stack overflow situation(while tail-call is not applicable):
#include <iostream>
#include <vector>
#include <numeric>
#include <variant>
#include <functional>
#include <utility>

// --- (Paste the Bounce, Step, and trampoline definitions from above here) ---

template<typename T>
struct Bounce;

template<typename T>
using Step = std::variant<T, Bounce<T>>;

template<typename T>
struct Bounce {
    std::function<Step<T>()> thunk;
};

template<typename T>
T trampoline(Step<T> first_step) {
    Step<T> current_step = std::move(first_step);
    while (std::holds_alternative<Bounce<T>>(current_step)) {
        current_step = std::get<Bounce<T>>(current_step).thunk();
    }
    return std::get<T>(current_step);
}

// --- (Paste the sum_trampolined function from above here) ---

Step<long> sum_trampolined(const std::vector<long>& data, size_t index, long current_sum) {
    if (index == data.size()) {
        return current_sum;
    }
    return Bounce<long>{
        [=]() {
            return sum_trampolined(data, index + 1, current_sum + data[index]);
        }
    };
}


int main() {
    // This will now work without crashing!
    std::vector<long> large_vec(200000, 1);

    // To start the process, we create the very first step.
    Step<long> first_step = sum_trampolined(large_vec, 0, 0);

    // The trampoline function runs the computation to completion.
    long total = trampoline(first_step);

    std::cout << "Trampolined sum of large vector: " << total << std::endl;
    std::cout << "The program finished successfully." << std::endl;

    return 0;
}

Jul 29, 2025

[C++] Coroutine examples

  1. C++ coroutine has two scopes: caller scope, coroutine scope (if you familiar with Python yield/send, same concept but more subtle with object lifetime and more flexible. Or Golang goroutine/channel [stackful], or C++ fiber[stackful])
  2. Suspend suspends the coroutine and back to the caller, caller uses handler to resume the coroutine
  3. Underneath using JMP instead of CALL since it's stackless coroutine.
  4. Beware that even `void coroutine(const int& arg)` that has co_await/co_yield/co_return, when it suspends, the binding arg has been stack rewinded thus destroyed.
    i.e.
    If a coroutine has a parameter passed by reference, resuming the coroutine after the lifetime of the entity referred to by that parameter has ended is likely to result in undefined behavior.

    The C++ core guidelines say not to use references at all.
  5. Just avoid references" isn't comprehensive, e.g. we have span, string_view, even though they are not reference, can still facing the same dangling issue after co_await/co_yield/co_return.
    Thus, a better contract is:
    Pass by Value with Owning Types.
  6. The way we ensure caller doesn't compile is by making Co non-moveable, and accepting it by value in the co_await implementation in the promise type.

    This means the only way to await it is if you do so immediately in the same full expression as the function call, so that guaranteed copy elision can kick in. 

    Because temporaries aren't destroyed until that full expression has been evaluated, the lifetimes work out perfectly using the usual language rules around lifetimes.

    Design TIP:
    Eliminated the problem with references by just declaring an entire pattern of code illegal.
      Co<void> UseInt_Async(const int& x) {
         printf("%d\n", x);
         co_await DoSomething();
         printf("%d\n", x);
      }
      
      k3::Co<void> UseInt_Async(const int&);
      // Works fine
      co_await UseInt_Async(17);
        
      // Compiler error due to k3::Co<void> is not moveable.
      k3::Co<void> co = UseInt_Async(17);
      co_await std::move(c);
  7. What if we want to use above pattern?
    Indirection, just like std::bind
    k3::Co<void> UseInt_Async(const int&);
    // Totally safe
    k3::Future<void> future{UseInt_Async, 17};
    printf("I created the future!\n");
    co_await std::move(future).Run();
  8. Co<void> has the interface concept of awaitable, as below.
  9.  Fan out or run in sequence; the implementation is imaginable.
      
    // Run all concurrently, finishing once all finish.
    k3::Co<void> FanOut(std::vector<k3::Future<void>> futures);
    // Run all concurrently, finishing when the first finishes.
    template <typename T>
    k3::Co<T> Race(std::vector<k3::Future<T>> futures);
    

  10. co_await awaitable;
    struct awaitable {
    	bool await_ready() { return false; // false: suspended, true: not suspended}
    	void await_suspend(std::coroutine_handle<> h) { // what to do when suspended}
    	void await_resume() { // in await_suspend's coroutine_handle calling .resume() comes here. and after this resume to coroutine.  }
        int await_resume() {return 42; // co_await returns value from here.}
    }; 
    co_yield
    co_return

Stack:

Go goroutines has dynamic stack size, default to 2kb. linux has thread size default to 8mb, 64k in production.
So in coroutine, how do we avoid stack overflow?

tail call comes to the rescue;
c++ coroutine guarantees a tail call into the other coroutine, and then you can do it again on the way back out. There's no need to have a stack frame to return to; everything is a JUMP instruction.
This is the only guaranteed[as of c++20] tail call in the standard; it's actually kind of a unique mixing of abstraction levels.
Co<void> Foo();
Co<void> Bar() {
  // Tail call into the body of Foo.
  co_await Foo();
  // Tail call back from the body of Foo.
  [...]
}
This indicates that with coroutine embedded inside a coroutine all use the same stack; thus
if there are multiple suspension, the stack can be overflown.

c9 solution:
we are never allowed to have one coroutine directly resume another.

Possible solution to the Notify/Wait pattern:
class Event {
Co<void> Wait();
// If there is a waiter, it will start running concurrently
// on another thread. The calling thread continues on.
// Look at the co_wait example below.
void Notify();
};
We could resolve this by breaking the assumption that there is only one thread available.
Instead we could resume the coroutine on a different thread, letting it run concurrently at the same time as the notifier.


Avoid executors in the coroutine library design:

Don't offer unnecessary configurability.
library should be agnostic to executors. All it has is its one thread-local queue of things that have been resumed by the running coroutine.
Of course individual things you wait for might have an executor internally. Like if you wait for an RPC to finish, you'll probably be resumed on one of a team of threads reading RPC replies from the network.
e.g.


// Hop to a specific executor. We need to run there because…
// look the co_await example below~
co_await Reschedule(my_executor);

Cancellation in library design

  • RPCs to stuck machines
  • Request hedging (idempotent operations)
  • Avoiding wasted work
  • Timeouts

The callee exists only to serve the caller. It must stop promptly if the caller loses interest.

  • the caller of a coroutine is always in control.
  • If the caller no longer wants the callee to run, the callee should stop running.
  • And it should do so promptly. Not after getting a response to its RPC. Not after a timeout expires. Immediately.
    All it should get to do is the kind of thing you want to do before you release a lock or unwind after an exception. Ensure internal invariants are restored; that kind of thing.
  • coroutine's local automatic variable won't be destructed until it is resumed till the end of the coroutine. (while is is on the heap)
  • If cancellation happens (which is to resume after coroutine suspended, and pass the cancellation bit to the coroutine_handler which be consumed by the awaitable, and the awaitable resume checks the bit to cancel the original code path.), the coroutine continues till the end.

Notes on structured concurrency; idea:
Children should not out-live parent.
Prefer structured concurrency wherever possible.
Good API design also helps with usability and safety.

Handler:

// task, aka handler, will be created through promise_type instance's get_return_object()
// and destructed once the coroutine function is returned to the caller.
// the destruct of task does not mean the promise_type instance is destructed, it still resides on the heap where
// coroutine is located.
struct task
{
    struct promise_type
    {
        task get_return_object() { return {}; // Always called first when coroutine being called. }
        std::suspend_never initial_suspend() { return {}; // Always called second when coroutine being called.   }
        std::suspend_never final_suspend() noexcept { return {}; / Always called last when coroutine is finished. (i.e. done)  }
        void return_void() { // called if return from coroutine with void.}
        std::suspend_always yield_value(T value) noexcept
        {
          // used for co_yeild, once co_yeild is called, the yield value is installed here.
          // and suspend afterwards due to return `std::suspend_always`
          return {};
        }
        
        std::suspend_always return_value(T value) {
          // used for co_return, once co_return is called with expr, the expr value is installed here.
          // and suspend afterwards due to return `std::suspend_always`
          return {};
        }
        void unhandled_exception() {}
    };
    
    task(std::coroutin_handler<promise_type> h) {
      // store the coroutin_handler inside the task.
    }
};

#include <iostream>
#include <coroutine>

// 1. The Promise Type
struct MyTaskPromise {
    // The compiler calls this to get the return object.
    // We return a 'MyTask' object, and in its constructor, we pass it the handle.
    struct MyTask get_return_object();

    std::suspend_never initial_suspend() noexcept { return {}; }
    std::suspend_always final_suspend() noexcept { return {}; }
    void return_void() noexcept {}
    void unhandled_exception() noexcept {}
};

// 2. The Task Type
// This is the wrapper around the coroutine_handle.
struct MyTask {
    using promise_type = MyTaskPromise;
    std::coroutine_handle<MyTaskPromise> handle;

    // The constructor takes the handle from the promise's get_return_object() call.
    MyTask(std::coroutine_handle<MyTaskPromise> h) : handle(h) {}
};

// Now we can define get_return_object() because MyTask is defined.
MyTask MyTaskPromise::get_return_object() {
    // This is the key line: we construct the MyTask object with the handle
    // to the coroutine that owns this promise.
    return MyTask{std::coroutine_handle<MyTaskPromise>::from_promise(*this)};
}

// 3. A simple awaitable to demonstrate suspension.
struct Awaitable {
    bool await_ready() { return false; }
    void await_resume() {}

    // This await_suspend returns `true`, which suspends the coroutine
    // and returns control to the caller (main).
    bool await_suspend(std::coroutine_handle<>) noexcept {
        std::cout << "-> Awaitable: Coroutine is suspending." << std::endl;
        return true;
    }
};

// 4. The Coroutine Function
MyTask MyCoroutine() {
    std::cout << "Coroutine: Starting." << std::endl;
    co_await Awaitable{}; // Coroutine suspends here.
    std::cout << "Coroutine: Resumed and finishing." << std::endl;
    co_return;
}

// 5. The Caller (main function)
int main() {
    // 1. The call to MyCoroutine() returns a 'MyTask' object.
    // This object contains the handle to the suspended coroutine.
    MyTask task = MyCoroutine();

    std::cout << "\nMain: Coroutine is suspended. I am the caller.\n" << std::endl;

    // 2. We can now use the handle stored inside the 'task' object
    // to resume the coroutine.
    if (task.handle) {
        std::cout << "Main: Resuming the suspended coroutine." << std::endl;
        task.handle.resume();
    }

    std::cout << "\nMain: Coroutine has finished its execution." << std::endl;

    // 3. Clean up the coroutine's memory.
    if (task.handle) {
        task.handle.destroy();
    }

    return 0;
}



co_yield Example:

#include <coroutine>
#include <iostream>
#include <optional>

template<std::movable T>
class Generator
{
public:
    struct promise_type
    {
        Generator<T> get_return_object()
        {
          std::cout << "get_return_object()\n"; // -2; Generator created with pointer to the heap
          return Generator{Handle::from_promise(*this)};
        }
        static std::suspend_always initial_suspend() noexcept
        {
          std::cout << "suspend_always initial_suspend()\n"; // -4 init. suspend. Go to the caller.
          return {};
        }
        static std::suspend_always final_suspend() noexcept
        {
          std::cout << "suspend_always final_suspend()\n"; // -15 coroutine ends, call this and suspend.
          // Back to the caller. Go to (13)
          return {};
        }
        std::suspend_always yield_value(T value) noexcept
        {
          std::cout << "yield_value() : " << value << "\n"; // -9, suspend. Go to the caller.
          current_value = std::move(value);
          return {};
        }
        // Disallow co_await in generator coroutines.
        void await_transform() = delete;
        [[noreturn]]
        static void unhandled_exception() { throw; }

        std::optional<T> current_value;
    };

    using Handle = std::coroutine_handle<promise_type>;

    explicit Generator(const Handle coroutine) :
        m_coroutine{coroutine}
    {
      std::cout << "Generator constructor\n"; // -3
    }

    Generator() = default;
    ~Generator()
    {
        // make sure no double free through handler.destroy() while
        // caller could have a copy of the handler.
        if (m_coroutine && !m_coroutine.done())
            m_coroutine.destroy();
      std::cout << "Generator destructor\n";
    }

    Generator(const Generator&) = delete;
    Generator& operator=(const Generator&) = delete;

    Generator(Generator&& other) noexcept :
        m_coroutine{other.m_coroutine}
    {
      std::cout << "Generator move constructor\n";
      other.m_coroutine = {};
    }
    Generator& operator=(Generator&& other) noexcept
    {
      std::cout << "Generator assign operator=\n";
        if (this != &other)
        {
            if (m_coroutine)
                m_coroutine.destroy();
            m_coroutine = other.m_coroutine;
            other.m_coroutine = {};
        }
        return *this;
    }

    // Range-based for loop support.
    class Iter
    {
    public:
        void operator++()
        {
          std::cout << "Iter ++ currnet value: " << *m_coroutine.promise().current_value << "\n";
          
          m_coroutine.resume(); // -11, resume from (9) yeild's suspend; JUMP to COROUTIN RIGHT AWAY!
          // Following cout is not run until coroutin suspend again.
          
          // -13, followed by (12) and after (9) yeild's suspend.
          std::cout << "Iter ++ resumed currnet value: " << *m_coroutine.promise().current_value << "\n";
        }
        const T& operator*() const
        {
          // -10, caller print out the value.
          std::cout << " Iter* return value: " << *m_coroutine.promise().current_value << "\n";
          return *m_coroutine.promise().current_value;
        }
        bool operator==(std::default_sentinel_t) const
        {
          // -16, caller is calling this from coroutin's suspend.
          std::cout << "Iter == called: !m_coroutine: " << (!m_coroutine) << " m_coroutine.done(): " << m_coroutine.done() << "\n";
            return !m_coroutine || m_coroutine.done();
        }

        explicit Iter(const Handle coroutine) :
            m_coroutine{coroutine}
        {}

    private:
        Handle m_coroutine;
    };

    Iter begin()
    {
      std::cout << "Iter begin\n"; // -5, caller range for calls begin()
        if (m_coroutine)
            m_coroutine.resume(); // -6 (4) suspended resumed. Go to coroutine.
        return Iter{m_coroutine};
    }

    std::default_sentinel_t end() { return {}; }

private:
    Handle m_coroutine;
};

template<std::integral T>
Generator<T> range(T first, const T last)
{
  // suspended right away since initial_suspend() returns std::suspend_always
  // returns to the caller with Generator instance
  std::cout << "Range\n"; // -7 from (6)
  while (first < last){
    std::cout << "Range first: " << first << "\n";
    co_yield first++; // -8, go to yield_value(), install `first` value, first is + 1, and suspend.
    // // -12, resume from (11) operator++()
    std::cout << "Range after first++: " << first << "\n";
  }
  // -14, coroutine end, suspend_always final_suspend() called.
}

int main()
{
  std::cout << "Start for loop\n"; // -1
  // Generator is destructed only once due to range loop extends lifetime.  
  for (const char i : range(65, 67))
      std::cout << i << "\n";
  // -17, out of range for loop scope, 
  // range(65, 67) returned `Generator` destructs.

  std::cout << "End for loop\n";
    std::cout << '\n';
}
stdout:
Start for loop
get_return_object()
Generator constructor
suspend_always initial_suspend()
Iter begin
Range
Range first: 65
yield_value() : 65
Iter == called: !m_coroutine: 0 m_coroutine.done(): 0
 Iter* return value: 65
A
Iter ++ currnet value: 65
Range after first++: 66
Range first: 66
yield_value() : 66
Iter ++ resumed currnet value: 66
Iter == called: !m_coroutine: 0 m_coroutine.done(): 0
 Iter* return value: 66
B
Iter ++ currnet value: 66
Range after first++: 67
suspend_always final_suspend()
Iter ++ resumed currnet value: 66
Iter == called: !m_coroutine: 0 m_coroutine.done(): 1
Generator destructor
End for loop


co_await Example:

#include <coroutine>
#include <iostream>
#include <stdexcept>
#include <thread>
 
auto switch_to_new_thread(std::jthread& out) {
    std::cout << "switch_to_new_thread start\n";
    struct awaitable {
        std::jthread* p_out;
        bool await_ready() {
		  std::cout << "await ready\n";
		  return false; 
	    };
    
    	void await_suspend(std::coroutine_handle<> h) {
	    	std::cout << "await_suspend\n";
	        std::jthread& out = *p_out;
    	    if (out.joinable())
        	  throw std::runtime_error("Output jthread parameter not empty");
	        out = std::jthread([h] { 
        		std::cout << "calling handler.resume()\n";
				h.resume();
    	    });
            
        // Potential undefined behavior: accessing potentially destroyed *this
        // std::cout << "New thread ID: " << p_out->get_id() << '\n';
	        std::cout << "New thread ID: " << out.get_id() << '\n'; // this is OK
        }
    
    	void await_resume() {
	    	std::cout << "await_resume\n";
	    }
    };

    std::cout << "switch_to_new_thread about to return\n";
    
    return awaitable{&out};
}
 
struct task {
    struct promise_type {
        task get_return_object() {
			std::cout << "get_return_object()\n";
			return {}; 
		}
    
    	std::suspend_never initial_suspend() {
			std::cout << "inital_suspend()\n";
			return {}; 
		}
    
	    std::suspend_never final_suspend() noexcept {
			std::cout << "final_suspend()\n";
			return {}; 
		}
    
    	void return_void() {
			std::cout << "return_void\n";
		}
     
    	 void unhandled_exception() {}
    };

    ~task() {
	    std::cout << "task destruct\n";
    }
};
 
task resuming_on_new_thread(std::jthread& out) {
    std::cout << "Coroutine started on thread: " << std::this_thread::get_id() << '\n';
    co_await switch_to_new_thread(out);
    // awaiter destroyed here
    std::cout << "Coroutine resumed on thread: " << std::this_thread::get_id() << '\n';
}
 
int main() {
    std::jthread out;
    std::cout << "start\n";
    resuming_on_new_thread(out);
    std::cout << "ending main()\n";
}
stdout:
start
get_return_object()
inital_suspend()
Coroutine started on thread: 140248882116480
switch_to_new_thread start
switch_to_new_thread about to return
await ready
await_suspend
New thread ID: 140248877143744
task destruct
ending main()
calling handler.resume()
await_resume
Coroutine resumed on thread: 140248877143744
return_void
final_suspend()

Nov 6, 2024

[C++] include from /dev/stdin

When I was publishing Concurrent LRUCache which is used by my previous job at Linkedin, I used this feature provided by godbolt for demo, which is it could include headers through https protocol. 
e.g: https://godbolt.org/z/Y6he8z9Gf

Turned out this feature can be done through #include "/dev/stdin"

#include "/dev/stdin"

int main(){
  foo f{};
  (void)(f);
}

$ echo "struct foo {};" | clang++ -std=c++20 main.cc -Wall

Just replace the echo part with `wget`

Aug 29, 2024

[C++] ExplicitArgumentBarrier trick to force argument type being deduced during the call.

ExplicitArgumentBarrier used to force caller to have C being deduced instead of explicitly specified.
thus `deref_second_view<std::map>(map{});` won't work.
 
template <int&... ExplicitArgumentBarrier, typename C>
constexpr auto deref_second_view(C&& map ABSL_ATTRIBUTE_LIFETIME_BOUND) {
  return deref_second_view_t<std::remove_reference_t<C>>(map);
}

Aug 16, 2024

[C++] friend function definition inside a type can be found only through ADL if without forward declaration.

void foo(){
  auto f = &foo;
}


struct Test{

  void foo(){
    auto f = &Test::foo;
  }

  friend void bar(){
    // auto f = &bar; // Use of undeclared identifier 'baz'

    void bar();
    auto f = &bar;
  }

  friend void baz(Test t){
    // Find through ADL
    baz(Test{});
    // auto f = &baz; // Use of undeclared identifier 'baz'
  }

};


int main()
{
}

Jul 28, 2024

[C++] tag pointer

Reference:
Storing data in pointers

#include <cstddef>
#include <cstdint>
#include <iostream>


enum ListType {
  kNone,
  kReady,
  kDeleting,
};


static constexpr uintptr_t kListTypeMask = 0b11;
static constexpr uintptr_t kCleanPtrMask = ~kListTypeMask;

struct Tree;

struct Node{
  void set_list_type(ListType list_type) {
    uintptr_t t = static_cast<uintptr_t>(list_type);
    uintptr_t v = ptr_and_list_type_ & kCleanPtrMask;
    ptr_and_list_type_ = (v | t);
  }

  ListType list_type() const {
    return static_cast<ListType>(ptr_and_list_type_ & kListTypeMask);
  }

  void set_prev_next_ptr(Node** p) {
    uintptr_t t = ptr_and_list_type_ & kListTypeMask;
    uintptr_t v = reinterpret_cast<uintptr_t>(p);
    ptr_and_list_type_ = (v | t);
  }

  Node** prev_next_ptr() const {
    return reinterpret_cast<Node**>(ptr_and_list_type_ & kCleanPtrMask);
  }

  Tree* tree_ = nullptr;
  Node* parent_ = nullptr;
  Node* next_ = nullptr;
  uintptr_t ptr_and_list_type_ = 0;
};

struct Tree{
  void MarkReady(Node* p) {
    p->next_ = ready_;
    p->set_prev_next_ptr(&ready_);

    if (ready_ != nullptr) {
      ready_->set_prev_next_ptr(&p->next_);
    }

    ready_ = p;
    p->set_list_type(kReady);
  }
 
  int padding;
  Node* ready_ = nullptr;
};


int main() {
  // Print 8 since padding is type of int occupies 8 bytes.
  std::cout << "offset of ready_: " <<
    reinterpret_cast<void*>(&((Tree*)0)->ready_) << "\n";

  Tree* tree = new Tree{};
  std::cout << "tree: " << reinterpret_cast<void *>(&tree) << "\n";
  std::cout << "tree offset of ready: " <<
    reinterpret_cast<void *>(&tree->ready_) << "\n";

  Node p;
  p.tree_ = tree;
  tree->MarkReady(&p);
  std::cout << reinterpret_cast<void *>(p.prev_next_ptr()) << "\n";
}

MSB tagging vs. LSB tagging

Dec 31, 2023

[Cppcon 2023] Expressing Implementation Sameness and Similarity - Polymorphism in Modern C++, Daisy Hollman

Reference:
https://youtu.be/Fhw43xofyfo?si=PcMFEsqb7NRiCmyX

Two different kinds of sameness

Interface sameness

  • Enables users to treat things the same way in their code(create their own sameness)
  • Cannot be removed later
    • You can't change your user's code(at least not easily)
    • Once you allow users to treat things as the same, it's very hard to change that later
  • When done well: low code coupling(the degree of interdependence between software modules)

Implementation sameness

  • Enables readers to understand and use existing sameness of similarity
  • Can be changed or removed at any point in the future when it stops being helpful
  • When done well: high code cohesion(the degree to which the elements inside a module belong together)
Let this concept sinks in.

Consider about 'type of T'; e.g. template<typename T> vector{};


Reusing customization points.

Mixin mixins.

template<class> struct CRTPMixin;
template<template <class> class Mixin, class Derived>
struct CRTPMixin<Mixin<Derived>> {
  consteval auto& self() { return static_cast<Derived&>(*this); }
  consteval auto const& self() const { return static_cast<Derived const&>(*this); }
};

template<typename Derived>
struct PrintableElementwise : CRTPMixin<PrintableElementwise<Derived>> {
 void print() const {
    auto const& e = self().elements();
    std::apply([](auto const&... el) {
      ([&]{ cout << el << "\n"; }(), ...);
      }, e);
    }
};


As stated by Sy Brand's C++23’s Deducing this can help with the above

https://devblogs.microsoft.com/cppblog/cpp23-deducing-this/
static_cast<Derived const&>(*this); 
part. But really? Consider code uses deduce this from C++23:
struct PrintableElementwise {
  void print(this auto const& self) {
    auto const& e = self.elements();
    std::apply([](auto const&... el) {
      ([&]{ count << el << "\n"; }(), ...);
    }, e);
  }
};

struct Foo : PrintableElementwise {
 int a;
 double b;
 std::string s;
 auto elements() const {
    return std::forward_as_tuple(x, y, z);
 }
};
Above has mixed interface sameness with implementation sameness; which causes error:
auto items = std::vector<PrintableElementwise>{/*...*/};
for (auto* i : items) {
    i->print(); // error if derived type has no memfn elements().
}
Use the idea of 'type of T'. Which indicates the type's embedded behavior.


Qualifier forwarding

e.g.
Perfect forwarding:
template<class T>
void foo(T&& t) {
    bar(std::forward<T>(t));
}
equivalent to:
template<class T>
void foo(T& t) {
 bar(t);
}

template<class T>
using not_forwarding_ref = T;

template<class T>
void foo(not_forwarding_ref<T>&& t) {
  bar(std::move(t));
}


Don't Repeat Yourself(DRY)

'Every piece of knowledge must have a single, unambiguous, authoritative representation within a system' 


Separating the ownership mechanism (typical pattern for a class template customization point) 

From:

template<class Thing>
class OwingCollection {
 private:
   vector<unique_ptr<Thing>> things_;
 protected:
   void for_each(/* concept */ std::invocable<Thing const&> auto&& f) const { /*...*/};
 public:
   void insert(unique_ptr<Thing>) {};
   unique_ptr<Thing> remove(unique_ptr<Thing>) {};
   bool contains(unique_ptr<Thing>) const {};
   void remove_if(invocable<Thing const&> auto&&) {};
};
To:
template<class Thing, template<class...> class Owner = unique_ptr>
class OwingCollection {
 private:
   vector<Owner<Thing>> things_;
 protected:
   void for_each(/* concept */ std::invocable<Thing const&> auto&& f) const { /*...*/};
 public:
   void insert(Owner <Thing>) {};
   Owner <Thing> remove(Owner <Thing>) {};
   bool contains(Owner <Thing>) const {};
   void remove_if(invocable<Thing const&> auto&&) {};
};


STD's example of separable pattern 

[C++14] unique_ptr with type erasure as shared_ptr 


C++20 Concepts

C++20 concepts extract interface sameness without your permission.

Concepts allow library users to accidentally create code coupling between unrelated modules based only on names.
e.g.
template<class T>
  requires requires(T&& t) { { t.clear() } -> convertible_to<bool>; }
void do_the_stuff(T&& t) { /*...*/ }
Those two types fit for above API. 
However; their meaning of memfn clear() is different.
struct Container {
  // returns true if the container was
  // non-empty before the clear
  bool clear();
};

struct Color {
  // returns true if opacity == 0
  bool clear();
};
Concepts does not differentiate namespace.


More places having the concept of 'sameness'

  • 'Normal' functions (C like)
  • Macros and Code generation
    • 'Gross' but sometimes better than repeating things.
  • Customization Point Objects(CPOs)
  • Type erasure
  • constexpr functions
    • 'Sameness' of compile-time and runtime implementations.
  • Dependency injection (needs reflection)
  • Aspect-oriented programming(needs reflection)
  • Decoration(needs reflection)

Dec 12, 2023

[C++] std::unstable_unique implement

std::unstable_unique implement by Andrei Alexandrescu


#include <cassert>
#include <vector>

template <class iterator, class BinaryPredicate>
iterator unstable_unique(iterator first, iterator last, BinaryPredicate p) {
    if (first == last || std::next(first) == last)
        return last;  // 0 or 1-element range

    ++first;  // position first on the first unknown element (first element will stay by definition)
    bool first_is_known_duplicate = false;  // see below for description

    for (; first < last; ++first) {
        if (!first_is_known_duplicate) {
            if (!p(*first, *std::prev(first))) {
                continue;
            }
        }
        // Here we know that `*first` is a dupe and should be replaced. Also the range is not empty.
        assert(first < last);
        for (--last;; --last) {
            if (first == last)
                return first;  // just past the last unique element
            assert(first < last);
            if (!p(*last, *std::prev(last)))
                break;
        }
        assert(!p(*first, *last));
        // Here we know we're good to replace *first with *last.
        // Complicating matter: if we do so, we "forget" whether *std::next(first) is a duplicate of *first.
        // Maintain `first_is_known_duplicate` to keep track of that.
        first_is_known_duplicate = p(*first, *std::next(first));
        *first = std::move(*last);
    }

    return first;
}

template <class iterator>
iterator unstable_unique(iterator first, iterator last) {
    return unstable_unique(first, last, [](auto& a, auto& b) { return a == b; });
}

int main() {
    std::vector<int> v1;
    auto new_end1 = unstable_unique(v1.begin(), v1.end());
    assert(v1.end() == new_end1);

    std::vector<int> v2 = { 1, 2, 3, 4, 5 };
    auto new_end2 = unstable_unique(v2.begin(), v2.end());
    // std::cout << new_end2 - v2.begin() << '\n';
    assert(v2.end() == new_end2);
    // std::copy(v2.begin(), new_end2, std::ostream_iterator<int>(std::cout, " "));
    assert(std::vector<int>({ 1, 2, 3, 4, 5 }) == v2);

    std::vector<int> v3 = { 1, 1, 2, 3, 4, 5 };
    auto new_end3 = unstable_unique(v3.begin(), v3.end());
    assert((v3.begin() + 5 == new_end3));
    assert(std::vector<int>({ 1, 5, 2, 3, 4, 5 }) == v3);

    std::vector<int> v4 = { 1, 1, 2, 2, 3, 3, 4, 4, 5, 5 };
    auto new_end4 = unstable_unique(v4.begin(), v4.end());
    assert((v4.begin() + 5 == new_end4));
    assert(std::vector<int>({ 1, 5, 2, 4, 3, 3, 4, 4, 5, 5 }) == v4);

    std::vector<int> v5 = { 1, 1, 1, 1, 1 };
    auto new_end5 = unstable_unique(v5.begin(), v5.end());
    assert((v5.begin() + 1 == new_end5));
    assert(std::vector<int>({ 1, 1, 1, 1, 1 }) == v5);

    std::vector<int> v6 = { 1, 1, 1, 1, 1, 2 };
    auto new_end6 = unstable_unique(v6.begin(), v6.end());
    assert((v6.begin() + 2 == new_end6));
    assert(std::vector<int>({ 1, 2, 1, 1, 1, 2 }) == v6);

    std::vector<int> v7 = { 1, 2, 2, 3, 3, 3, 4, 4, 4, 4 };
    auto new_end7 = unstable_unique(v7.begin(), v7.end(), [](int a, int b) { return a == b; });
    assert((v7.begin() + 4 == new_end7));
    assert(std::vector<int>({ 1, 2, 4, 3, 3, 3, 4, 4, 4, 4 }) == v7);
};

Dec 7, 2023

[C++] ValueOr(type) type check.

https://godbolt.org/z/qYG6rsY7h

#include <type_traits>
#include <utility>
template <typename T>
struct Foo {
    template <typename U>
    T ValueOr(U&&) {
        static_assert(sizeof(U) > 1);
        return {};
    }
};

template <typename T>
struct ForwardedDeclval {
    T operator()();
};
template <typename... Ts, typename F>
consteval void InstantiateForStaticAssertions(F f) {
    static_assert(std::is_same_v<decltype(f(ForwardedDeclval<Ts>()...)), void>);
}

template <typename T>
struct FooWrapper {
    template <typename U>
    T ValueOr(U&& u) {
        InstantiateForStaticAssertions<Foo<T>, decltype(u)>(
            [](auto t, auto u) { t().ValueOr(u()); });

        return {};
    }
};

int main() {
    FooWrapper<int> f;

    f.ValueOr(1);
    //f.ValueOr('5');
}

Oct 12, 2022

[C++23] std::unreachable / gcc::__builtin_unreachable

std::unreachable

If control flow reaches the point of the std::unreachable, the program is undefined. It is useful in situations where the compiler cannot deduce the unreachability of the code.


Reference:

Jul 18, 2022

[C++23] std::unreachable

Reference:
(msvc) __assume
(gcc/clang) __builtin_unreachable()
(c++23) std::unreachable

folly code:
https://github.com/facebook/folly/blob/0713d3b213a2a2560065d479030da5769ea5f406/folly/lang/Hint-inl.h#L37


[[noreturn]] inline void unreachable()
{
    // Uses compiler specific extensions if possible.
    // Even if no extension is used, undefined behavior is still raised by
    // an empty function body and the noreturn attribute.
#ifdef __GNUC__ // GCC, Clang, ICC
    __builtin_unreachable();
#elifdef _MSC_VER // MSVC
    __assume(false);
#endif
}