On Fri, Aug 21, 2020 at 01:46:45AM +0800, Du Kaitian wrote:
On Thu, Aug 20, 2020 at 05:03:53PM +0800, Tong Qunfeng wrote:
background: a group of task (associate with same uuid), need blocked until special condition is meet.
implementation: create a Fiber for the uuid, when the condition is meet, let the Filber run (== this Fiber will dead). if the condition is not meet, then keep waiting (== block)
这个是为了多级 启动测试,或者同步任务提供的么,我们可以讨论一下这个逻辑 谢谢 jimmy
是的,多机同步等待可以用这个来辅助同步(未完成同步目标前都block等待)。
这是一个通用的 block_until_finished 逻辑, 同一个uuid可以 同步等待 1个“事件” 的true逻辑。
Signed-off-by: Tong Qunfeng tongqunfeng@huawei.com
src/lib/block_helper.cr | 42 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/lib/block_helper.cr
diff --git a/src/lib/block_helper.cr b/src/lib/block_helper.cr new file mode 100644 index 0000000..db84505 --- /dev/null +++ b/src/lib/block_helper.cr @@ -0,0 +1,42 @@ +# SPDX-License-Identifier: MulanPSL-2.0+
+# helper for (task) block +class BlockHelper
- def initialize
- @block_helper = Hash(String, Fiber).new
- end
- # waiting untill special uuid's task is finished
- # - yield (block) returns false, all uuid's task will block
- # - yield returns true, then all uuid's task will continue
- #
- # examples:
- # block_helper = BlockHelp.new # global instance
- #
- # # fiber-A call below code (checkfile: function / variable)
- # # when "checkfile == fase", then fiber-A blocked
- # # fiber-B call below code too
- # # when "checkfile == true", then fiber-A and B continues
- # block_helper.block_until_finished("1") { checkfile }
- #
- def block_until_finished(uuid)
- if @block_helper[uuid]?
fiber = @block_helper[uuid]
- else
fiber = Fiber.new { puts "uuid {#{uuid}} finished" }
@block_helper[uuid] = fiber
- end
- if yield == true
spawn fiber.run
- end
- until fiber.dead?
Fiber.yield
- end
- @block_helper.delete(uuid)
- end
+end
2.23.0