• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Zewo/Venice: Coroutines, structured concurrency and CSP for Swift on macOS and L ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

Zewo/Venice

开源软件地址(OpenSource Url):

https://github.com/Zewo/Venice

开源编程语言(OpenSource Language):

Swift 100.0%

开源软件介绍(OpenSource Introduction):

Venice

Swift License Slack Travis Codecov Codebeat Documentation

Venice provides structured concurrency and CSP for Swift.

Features

  • Coroutines
  • Coroutine cancelation
  • Coroutine groups
  • Channels
  • Receive-only channels
  • Send-only channels
  • File descriptor polling

Venice wraps a fork of the C library libdill.

Installation

Before using Venice you need to install our libdill fork. Follow the instruction for your operating system.

macOS

On macOS install libdill using brew.

brew install zewo/tap/libdill

Linux

On Linux we have to add our apt source first. You only need to run this command once in a lifetime. You don't need to run it again if you already have.

echo "deb [trusted=yes] http://apt.zewo.io ./" | sudo tee -a /etc/apt/sources.list
sudo apt-get update

Now just install the libdill apt package.

sudo apt-get install libdill

Add Venice to Package.swift

After installing libdill just add Venice as a dependency in your Package.swift file.

import PackageDescription

let package = Package(
    dependencies: [
        .Package(url: "https://github.com/Zewo/Venice.git", majorVersion: 0, minor: 19)
    ]
)

Test Coverage

Coverage Sunburst

The inner-most circle is the entire project, moving away from the center are folders then, finally, a single file. The size and color of each slice is represented by the number of statements and the coverage, respectively.

Documentation

You can check the Venice API reference for more in-depth documentation.

Structured Concurrency

Structured concurrency means that lifetimes of concurrent functions are cleanly nested. If coroutine foo launches coroutine bar, then bar must finish before foo finishes.

This is not structured concurrency:

Not Structured Concurrency

This is structured concurrency:

Structured Concurrency

The goal of structured concurrency is to guarantee encapsulation. If the main function calls foo, which in turn launches bar in a concurrent fashion, main will be guaranteed that once foo has finished, there will be no leftover functions still running in the background.

What you end up with is a tree of coroutines rooted in the main function. This tree spreads out towards the smallest worker functions, and you may think of this as a generalization of the call stack — a call tree, if you will. In it, you can walk from any particular function towards the root until you reach the main function:

Call Tree

Venice implements structured concurrency by allowing you to cancel a running coroutine.

let coroutine = try Coroutine {
    let resource = malloc(1000)
    
    defer {
        free(resource)
    }
    
    while true {
        try Coroutine.wakeUp(100.milliseconds.fromNow())
        print(".")
    }
}

try Coroutine.wakeUp(1.second.fromNow())
coroutine.cancel()

When a coroutine is being canceled all coroutine-blocking calls will start to throw VeniceError.canceledCoroutine. On one hand, this forces the function to finish quickly (there's not much you can do without coroutine-blocking functions); on the other hand, it provides an opportunity for cleanup.

In the example above, when coroutine.cancel is called the call to Coroutine.wakeUp inside the coroutine will throw VeniceError.canceledCoroutine and then the defer statement will run, thus releasing the memory allocated for resource.

Threads

You can use Venice in multi-threaded programs. However, individual threads are strictly separated. You may think of each thread as a separate process.

In particular, a coroutine created in a thread will be executed in that same thread, and it will never migrate to a different one.

In a similar manner, a handle, such as a channel or a coroutine handle, created in one thread cannot be used in a different thread.

License

This project is released under the MIT license. See LICENSE for details.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
MarioCatuogno/Clean-macOS: 发布时间:2022-08-18
下一篇:
syscl/M3800: auto patch macOS for M3800/XPS9530发布时间:2022-08-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap