关于c++:Folly库代码赏析5FuturePromise模型

前言

通常来说,有两种异步模式,一种是基于goroutine的用户态协程并发模型,另一种是基于Future/Promise的并发模型。后者可能将多个Future串联,改善回调天堂这一状况。其实Javascript早就提供了Promise模型,异步基于事件循环来做,每次都是将异步事件插入工夫序列期待下一次调用。而像C++/Rust这些语言就必须通过额定的executor库就安顿异步事件的执行。Rust中曾经有了十分优良的异步运行时tokioFolly的实现该当是差不多的。

Folly的介绍基于自底向上进行的办法,从底层building block到下层高级原语。

Core

KeepAlive and Deferred

KeepAlive是对executor的平安援用,保障不会在KeepAlive存活时析构executor

Deferred是尚未设置理论executor的代理,后续须要注入指定的executor

Info flow

Core保护三种info flow

  • producer => consumer:resultcallbackexecutorpriority
  • consumer => producer:interruptinterrupt handler 相似于Linux中的信号,用于勾销或xxcallback的执行
  • lifetime control: 2 ref counts(attached_callbackref_),管制析构

上面是一张 producer => consumer 的状态转换图,清晰地转换Core的各种状态以及触发条件。

///   +----------------------------------------------------------------+
///   |                       ---> OnlyResult -----                    |
///   |                     /                       \                  |
///   |                  (setResult())             (setCallback())     |
///   |                   /                           \                |
///   |   Start --------->                              ------> Done   |
///   |     \             \                           /                |
///   |      \           (setCallback())           (setResult())       |
///   |       \             \                       /                  |
///   |        \              ---> OnlyCallback ---                    |
///   |         \           or OnlyCallbackAllowInline                 |
///   |          \                                  \                  |
///   |      (setProxy())                          (setProxy())        |
///   |            \                                  \                |
///   |             \                                   ------> Empty  |
///   |              \                                /                |
///   |               \                            (setCallback())     |
///   |                \                            /                  |
///   |                  --------> Proxy ----------                    |
///   +----------------------------------------------------------------+

模块设计

Core分为CoreBaseCore<T>,将与模板无关的信息集中在Core<T>中。

生命周期治理

因为设计了代理机制,this能够代理其余core对象,因而,应用attached_反映援用计数。

另外应用callbackRef_反映lambda的援用计数。

为什么显式的应用援用计数而不是智能指针呢?

doCallBack设计

对于某些非凡状况,doCallBack能够inline执行callback函数(也就是在setResult时间接执行callBack函数)。

Barrier

这是一个常见的CountDownLatchWaitGroup(前者更适合)的同步原语,返回的所有Promise会在最初一个wait调用后被"resolve"。值得一提的是,外部实现用一个atomic_64保护两个atomic_32状态,别离记录以后正在调用wait的数量和所有曾经调用的数量。当前者 == 0 && 后者 == size 时,进行变量析构、内存开释。

同时,处于性能优化的思考,构造体设计采纳了变长数组 + 定位new。

struct ControlBlockAndPromise {
    ControlBlock cb;
    BoolPromise promises[1];
};

Promise

FuturePromise的一个典型利用如下

///   auto [p, f] = makePromiseContract(executor);
///   g = std::move(f).then([](MyValue&& x) {
///       ...executor runs this code if/when a MyValue is ready...
///     });
///   ...launch the async producer that eventually calls p.setResult()...

Future

SemiFuture vs Future

SemiFuture没有指定executor,通过.via指定executor变身为future

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理