Continuation源码分析

简介

continuation是服务于虚拟线程的一个类,主要提供run及yield的功能,使语言可以在任意点保存执行状态并且在之后的某个点返回。功能的主要实现都是在jvm源码中,Continuation.java 仅是流程代码。

如需使用,需添加以下指令到启动到jvm option:

--add-exports java.base/jdk.internal.vm=ALL-UNNAMED

run执行路径:


run.jpg

yield执行路径:


yield.jpg

1、字段方法说明:

从结构上来看,Continuation有两个维度,一个是纵向的链表维度(存在parent及child节点),一个是横向的scope维度,相同scope的属于同一类continuation(从代码实现角度来说,scope相对于对continuation打上个tag,使得用户在需要yield时不会yield错了对象)

// unsafe实例
    private static final Unsafe U = Unsafe.getUnsafe();
    // 是否开启本地缓存
    private static final boolean PRESERVE_EXTENT_LOCAL_CACHE;
    // JavaLangAccess操作对象,主要用于对 Java 核心类库中的一些非公开方法和字段的访问
    private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();
    // 实际运行的 runnable
    private final Runnable target;

    /* While the native JVM code is aware that every continuation has a scope, it is, for the most part,
     * oblivious to the continuation hierarchy. The only time this hierarchy is traversed in native code
     * is when a hierarchy of continuations is mounted on the native stack.
     */
    // scope对象,使用同一scope的Continuation可以相互之间yield
    private final ContinuationScope scope;
    // 父节点
    private Continuation parent; // null for native stack
    // 子节点
    private Continuation child; // non-null when we're yielded in a child continuation

    // 栈内存空间,在java类中没有赋值,赋值操作是在native方法中进行
    private StackChunk tail;

    // 当前Continuation是否已完成
    private boolean done;
    // 装载状态
    private volatile boolean mounted = false;
    // yield信息(可能是scope,也可能是yield的结果)
    private Object yieldInfo;
    // 
    private boolean preempted;

    private Object[] extentLocalCache;

2、RUN方法


 public final void run() {

    while (true) {
      // 装载
      mount();
      JLA.setExtentLocalCache(extentLocalCache);
      // 已完成时再调用run则抛异常
      if (done)
        throw new IllegalStateException("Continuation terminated");
      // 获取当前载体线程
      Thread t = currentCarrierThread();
      // 当parent和cild都yield时,child先于paret run时,会进入此if
      if (parent != null) {
        if (parent != JLA.getContinuation(t))
          throw new IllegalStateException();
      } else
        this.parent = JLA.getContinuation(t);
      JLA.setContinuation(t, this);

      try {
        boolean isVirtualThread = (scope == JLA.virtualThreadContinuationScope());
        // 此处判断是否存在堆栈内存空间,如不存在则说明未开始
        if (!isStarted()) { // is this the first run? (at this point we know !done)
          // enterSpecial -> enter -> enter0 -> target.run
          enterSpecial(this, false, isVirtualThread);
        } else {
          assert !isEmpty();
          enterSpecial(this, true, isVirtualThread);
        }
      } finally {
        // 此处为什么需要读写屏障
        fence();
        try {
          assert isEmpty() == done : "empty: " + isEmpty() + " done: " + done + " cont: "
              + Integer.toHexString(System.identityHashCode(this));
          //
          JLA.setContinuation(currentCarrierThread(), this.parent);
          if (parent != null)
            parent.child = null;

          postYieldCleanup();

          unmount();
          if (PRESERVE_EXTENT_LOCAL_CACHE) {
            extentLocalCache = JLA.extentLocalCache();
          } else {
            extentLocalCache = null;
          }
          JLA.setExtentLocalCache(null);
        } catch (Throwable e) {
          e.printStackTrace();
          System.exit(1);
        }
      }
      // we're now in the parent continuation

      assert yieldInfo == null || yieldInfo instanceof ContinuationScope;
      // 唯一跳出循环的点
      if (yieldInfo == null || yieldInfo == scope) {
        this.parent = null;
        this.yieldInfo = null;
        return;
      } else {
        // 进入此代码块必要条件是yieldInfo存在且不为当前的scope,既yield的scope非自身的scope
        parent.child = this;
        // 此时调用 yield0 方法,将当前的continuation及需要yield的scope传递,直到匹配到yieldInfo == scope,即链表向上查找
        parent.yield0((ContinuationScope) yieldInfo, this);
        // 断链
        parent.child = null;
      }
    }
  }
 

3、Yield方法

疑问:为什么yield方法要设计成静态的方法?(此处能充分体现scope的作用,但为何如此设计)

此处yield设计成静态方法,个人认为是想要让continuation的维度在scope上控制,而不是在实例上控制。假如我们有一个Continuationd的List对象,list中存在scope=A与scope=B的continuation实例,分别执行任务a和任务b,如果我需要暂停任务a,只需要list.stream().foreach(c → Continuation.yield(scopeA));

猜想:如果把list换成线程池,把continuation换成VirtualThread,是不是就能够对scope范围的VirtualThread进行yield?

/**
   * Suspends the current continuations up to the given scope
   *
   * @param scope The {@link ContinuationScope} to suspend
   * @return {@code true} for success; {@code false} for failure
   * @throws IllegalStateException if not currently in the given {@code scope},
   */
  public static boolean yield(ContinuationScope scope) {
    Continuation cont = JLA.getContinuation(currentCarrierThread());
    Continuation c;
    // 基于Continuation实例当前向父节点遍历,直到匹配虚拟线程类型的ContinuationScope的Continuation,如果没有匹配的Continuation会抛出异常中断流程
    // 此处其实是在校验当前Continuation链表中是否存在需要yield的scope
    for (c = cont; c != null && c.scope != scope; c = c.parent)
      ;
    if (c == null)
      throw new IllegalStateException("Not in scope " + scope);

    return cont.yield0(scope, null);
  }

    /**
   * 此方法有两个调用的地方,一个是yield,另一个是run,其中run方法中会传child
   *
   */
  private boolean yield0(ContinuationScope scope, Continuation child) {
    preempted = false;

    // 此处记录需要yield 的 scope
    if (scope != this.scope)
      this.yieldInfo = scope;
    // 该方法由c++实现,具体:stubGenerator_aarch64.cpp  → generate_cont_doYield() 方法
    // 主要作用是将当前线程的执行状态保存并返回到调用者,即真正实现yield地方
    int res = doYield();
    U.storeFence(); // needed to prevent certain transformations by the compiler

    assert scope != this.scope || yieldInfo == null
        : "scope: " + scope + " this.scope: " + this.scope + " yieldInfo: " + yieldInfo + " res: " + res;
    assert yieldInfo == null || scope == this.scope || yieldInfo instanceof Integer
        : "scope: " + scope + " this.scope: " + this.scope + " yieldInfo: " + yieldInfo + " res: " + res;

    // 此处代码的作用是将this的res传递给child,并将this.yieldInfo = null
    if (child != null) { // TODO: ugly 作者的吐槽,想看后续会怎么优化
      if (res != 0) {
        child.yieldInfo = res;
      } else if (yieldInfo != null) {
        assert yieldInfo instanceof Integer;
        child.yieldInfo = yieldInfo;
      } else {
        child.yieldInfo = res;
      }
      this.yieldInfo = null;
    } else {
      if (res == 0 && yieldInfo != null) {
        // 此处传递yieldInfo至链表最末尾的continuation
        res = (Integer) yieldInfo;
      }
      this.yieldInfo = null;

      if (res == 0)
        onContinue();
      else
        // 非 0 则说明 pinned ,抛异常
        onPinned0(res);
    }
    assert yieldInfo == null;

    return res == 0;
  }

4、实例说明

以下代码为continuation 、child1、child2组成的链表调用,在child2进行yield(scope)操作之后,会遍历链表,将当前节点yield,直至continuation实例的scope等于目标scope为止:

  public static void main(String[] args) {

        ContinuationScope scope = new ContinuationScope("example1");

        ContinuationScope scope2 = new ContinuationScope("example2");

        ContinuationScope scope3 = new ContinuationScope("example3");

        Continuation child2 = new Continuation(scope3, () -> {
            System.out.println("before scope yield");
            Continuation.yield(scope);
            System.out.println("after scope yield");
        });

        Continuation child1 = new Continuation(scope2, () -> {
            System.out.println("before child2 run");
            child2.run();
            System.out.println("after child2 run");
        });

        Continuation continuation = new Continuation(scope, () ->  {
            System.out.println("before child1 run");
            child1.run();
            System.out.println("after child1 run");
        });

        System.out.println("before run");
        continuation.run();
        System.out.println("before run again");
        continuation.run();
        System.out.println("end");

    } 

输出结果:


输出结果.jpg
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 222,627评论 6 517
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 95,180评论 3 399
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 169,346评论 0 362
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 60,097评论 1 300
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 69,100评论 6 398
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,696评论 1 312
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 41,165评论 3 422
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 40,108评论 0 277
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,646评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,709评论 3 342
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,861评论 1 353
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,527评论 5 351
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 42,196评论 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,698评论 0 25
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,804评论 1 274
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 49,287评论 3 379
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,860评论 2 361

推荐阅读更多精彩内容