text: #pragma once
  
  #include <brutal/mem/units.h>
  #include <brutal/time/types.h>
  
  #define FIBER_STACK_SIZE KiB(16)
  
  #define FIBER_CURRENT_RUNNING_PROC (nullptr)
  
  typedef void FiberFn(void);
  
  typedef struct PACKED
  {
      uint64_t rip, rsp;
      uint64_t rbx, rbp, r12, r13, r14, r15;
  
      uint32_t fc_mxcsr;
      uint32_t fc_x86_cw;
  } FibersContext;
  
  typedef enum
  {
      FIBER_EMBRYO,
      FIBER_RUNNING,
      FIBER_CANCELING,
      FIBER_CANCELED,
      FIBER_BLOCKED,
      FIBER_IDLE,
  
      FIBER_ERROR,
  } FiberState;
  
  typedef bool FiberBlockerFn(void *context);
  
  typedef enum
  {
      FIBER_SUCCESS,
      FIBER_TIMEOUT,
      FIBER_INTERRUPTED,
  } FiberBlockResult;
  
  typedef struct
  {
      FiberBlockerFn *function;
      void *context;
      Timeout deadline;
  
      FiberBlockResult result;
  } FiberBlocker;
  
  typedef struct fiber Fiber;
  
  struct fiber
  {
      int id;
      FiberState state;
      FiberBlocker blocker;
  
      FiberFn *fn;
  
      void *stack;
      FibersContext ctx;
  
      Fiber *next;
      Fiber *prev;
  };
  
  void fiber_yield(void);
  
  Fiber *fiber_start(FiberFn call);
  
  FiberBlockResult fiber_block(FiberBlocker blocker);
  
  void fiber_ret(void);
  
  void fiber_await(Fiber *fiber);
  
  Fiber *fiber_self(void);
  
  Tick fiber_deadline(void);
  