• Stars
    star
    368
  • Rank 115,958 (Top 3 %)
  • Language
    Kotlin
  • License
    Apache License 2.0
  • Created over 5 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Kotlin Coroutine-based workers for native

CoroutineWorker

Build Status

Maven Central

Specs

  • Supported on Native, JVM, and JS (legacy and IR) (feel free to contribute adding more targets)
  • Kotlin 1.7.10

Gradle

To use in your multiplatform project, update your common dependencies in your gradle configuration:

kotlin {
    sourceSets {
        commonMain {
            dependencies {
                implementation "com.autodesk:coroutineworker:0.8.3"
            }
        }
    }
}

CoroutineWorker uses gradle module metadata. We recommend adding the following to your settings.gradle to take advantage of that (not necessary for Gradle 6+):

enableFeaturePreview('GRADLE_METADATA')

About

CoroutineWorker helps support multi-threaded coroutine usage in common code that works in Kotlin/Native and on JVM until kotlinx.coroutines has full support for native, multi-threaded coroutines.

Projects Using this on your Devices

Sample Usage

Spawning Asynchronous Work

Use execute to start background work from common code:

val worker = CoroutineWorker.execute {
  // - In here, `this` is a `CoroutineScope`
  // - Run suspend functions, call launch, etc.
  // - This code runs in a thread pool
}

// Tells the worker to cancel (uses standard coroutine cancellation)
worker.cancel()

// Tells the worker to cancel; it suspends until cancellation is finished
worker.cancelAndJoin()

Waiting on Asynchronous Work to Complete

From a coroutine context (i.e. somewhere you can call a suspend fun), use withContext to kick off work to another thread. It will non-blocking/suspend wait for the cross-thread work to complete:

suspend fun doWork() {
  val result = CoroutineWorker.withContext {
    // This is similar to execute, but it returns
    // the result of the work at the end of this lambda
    1
  }
  print(result) // prints 1
}

This is like using withContext on JVM to switch coroutine contexts. You can also properly pass a dispatcher, which will be used on JVM: withContext(Dispatchers.IO) { … }. The idea here is that this will be easy to migrate when we do get multi-threaded coroutine support in Kotlin/Native.

Waiting on Asynchronous Callback-based Work

Use threadSafeSuspendCallback to bridge callback-style async work into your code as a suspend fun:

suspend fun performNetworkFetch() {
  val result = threadSafeSuspendCallback { completion ->
    // example: fetch network data that isn't coroutine-compatible
    fetchNetworkData { networkResult ->
      // notify that async work is complete
      completion(networkResult)
    }
  }

  // result is now available here
}

Sample Project

In the sample directory, there is a sample project that demonstrates adding CoroutineWorker to an iOS + JVM library. We just used the sample library from IntelliJ's template for a "Mobile Shared Library." In the sample is a function called performWork (common code) that takes a completion lambda and demonstrates CoroutineWorker.execute. In tests, we use K/N concurrency helpers from kotlinx.atomicfu to demonstrate capturing a result across threads in K/N and executing this function.

CoroutineWorker Prefers Frozen State

Object detachment (i.e. transferring object ownership from one thread to another) is relatively difficult to achieve (outside of simple scenarios) compared to working with objects that are frozen and immutable. Because of this, CoroutineWorker prefers taking the frozen, immutable route:

  • Lambdas passed to CoroutineWorker are automatically frozen when they are going to be passed across threads.
  • The result value from withContext is also frozen.

Tips for Working with Frozen State

  • Be careful about what your frozen lambdas capture; those objects will be frozen too. Especially, watch for implicit references to this.
  • Call ensureNeverFrozen() on objects that you don't expect to ever be frozen.

IO-Bound Work

In the JVM world, you typically write code like this for managing IO-bound work with coroutines:

withContext(Dispatchers.IO) {
    // IO writes
}

Similar behavior is supported in CoroutineWorker for Kotlin/Native via the IODispatcher. To use it in common code, make an expect val Dispatchers.IO: CoroutineDispatcher that returns IODispatcher for Kotlin/Native and Dispatchers.IO for JVM, and pass that to CoroutineWorker.withContext when performing IO-bound worker.

More Repositories

1

react-base-table

A react table component to display large datasets with high performance and flexibility
JavaScript
1,464
star
2

maya-usd

A common USD (Universal Scene Description) plugin for Autodesk Maya
Mathematica
761
star
3

Aurora

Real-time GPU path tracing with a USD Hydra render delegate
C++
430
star
4

revit-ifc

IFC for Revit and Navisworks (2019+)
C#
415
star
5

standard-surface

White paper describing the Autodesk Standard Surface shader.
CSS
341
star
6

continuous-threat-modeling

A Continuous Threat Modeling methodology
282
star
7

arnold-usd

Arnold components for USD
Python
243
star
8

animx

A project to make it easier for people to use Maya animation in their tools and pipelines.
C++
200
star
9

hig

Autodesk's unified design system
JavaScript
180
star
10

hubble

πŸ›° Collaboration, usage, and health data visualization for GitHub Enterprise
Python
176
star
11

molecular-design-toolkit

Notebook-integrated tools for molecular simulation and visualization
Python
164
star
12

synthesis

A Robotics Simulator for Autodesk Fusion CAD Designs
JavaScript
149
star
13

LiveLink

An Unreal Live Link plugin for Autodesk Maya
C++
120
star
14

quack

Quack - Reuse git modules in smart way
Python
115
star
15

civilconnection

CivilConnection enables the exchange of information between Civil 3D, Dynamo and Revit.
C#
87
star
16

XLB

XLB: Accelerated Lattice Boltzmann (XLB) based on JAX for Physics-based ML
Python
83
star
17

notebook-molecular-visualization

2D and 3D molecular visualization in Jupyter notebooks using 3DMol.js and D3.js
Python
67
star
18

molecule-3d-for-react

3D molecular visualization React component using 3Dmol.js
JavaScript
60
star
19

3DViewerComponent

A ThreeJS 3D Viewer Web Component composed with (Google's) Polymer
JavaScript
47
star
20

revit-stl-extension

An Autodesk Revit add-in to export Revit models to STL format.
C#
40
star
21

enterprise-config-for-git

A painless Git setup with an easy way to share Git configs and scripts within a company using GitHub Enterprise.
Shell
39
star
22

ADAppRater-iOS

Objective-C
39
star
23

bifrost-usd

Bifrost nodes for USD
C++
38
star
24

AutomaticComponentToolkit

A toolkit to automatically generate software components: abstract API, implementation stubs and language bindings
Go
38
star
25

sitoa

Arnold plugin for Softimage
Python
34
star
26

bionano-wetLabAccelerator

A tool for researchers working in synthetic biology and virology to design robotic wet lab protocols using a visual UI without coding.
JavaScript
33
star
27

aomi

Provide secrets to build and release pipelines in a self service way using Hashicorp Vault.
Python
32
star
28

sydevs

Simulation-based analysis of complex systems involving people, devices, physical elements, and dynamic environments.
C++
32
star
29

Neon

Multi-GPU Framework for Voxel Grid Computations
C++
31
star
30

machine-collaboration-utility

A CNC controller interface that allows for browser based job dispatching and real-time direct communication with devices over serial, usb, http, telnet, or any other communication protocol.
JavaScript
27
star
31

PowerShapeAndPowerMillAPI

An API for Autodesk PowerShape and PowerMill
C#
26
star
32

vault-admin-tools

Autodesk vault administration and data migration tools
C#
24
star
33

goatnative-inject

C++11 Dependency Injection (IoC - inversion of control) class using variadic templates and shared pointers.
C++
23
star
34

orion-ui

UI Components that work seamlessly in multiple UI frameworks
JavaScript
22
star
35

maya-hydra

A Maya plugin that replaces the main Maya viewport with a Hydra viewer.
C++
22
star
36

AutodeskMachineControlFramework

Middleware framework to integrate CAD/CAM software with machine hardware systems into a production-ready, complete and cohesive closed loop system.
C++
22
star
37

molecule-2d-for-react

2D molecule visualization component
ApacheConf
21
star
38

theo-loader

A webpack loader that transforms Design Tokens files using Salesforce's theo
JavaScript
21
star
39

go-awsecs

Library and tools for AWS ECS operations
Go
21
star
40

cam-posteditor

Autodesk HSM Post Processor for Visual Studio Code
HTML
19
star
41

flame-family

Custom Actions add-ins for Flame, Flare and Flame Assist.
Python
19
star
42

cloud-compute-cannon

Cloud Compute Cannon is a tool aimed at scientists and more general users who want to use cheap cloud providers (such as Amazon) to perform large scale computes (number crunching).
Haxe
18
star
43

genetic-constructor-ce

Genetic Constructor
JavaScript
17
star
44

shore

Go
16
star
45

molecular-simulation-tools

Web applications for molecular modeling using the Molecular Design Toolkit
JavaScript
16
star
46

RolloutManager-iOS

The Rollout Manager component is an iOS (Objective C) component that brings its users the ability to roll out features gradually to production.
Objective-C
16
star
47

watchdog4git

A GitHub πŸ€– that checks for common Git related errors and notifies the responsible engineers
Go
15
star
48

pyccc

Asynchronous job and file i/o management for containers
Python
15
star
49

nanodesign

Python package for working with nucleic acid structural designs.
Python
15
star
50

pgbelt

Pgbelt is a CLI tool used to manage Postgres data migrations from beginning to end, for a single database or a fleet, leveraging pglogical replication.
Python
15
star
51

ADNetPromoterScoreSurvey-iOS

ADNetPromoterScoreSurvey is an iOS component that presents Net Promoter Score (NPS) survey dialog to the user.
Swift
15
star
52

autodesk.github.io

A listing of open source efforts at Autodesk on GitHub
HTML
14
star
53

biomolecular-design-database

The Biomolecular Design Database Project is a web application that allows users to share, search and use nucleic acid nanotechnology designs. This includes DNA Origami, RNA Origami, DNA Bricks and many other types of systems.
JavaScript
14
star
54

powermill-api-examples

These examples export data from the PowerMill product and format it for use in various third-party simulation tools: Vericut, NCSimul, ViMill.
C#
13
star
55

sculpt-vr

A sculpting program for WebVR
JavaScript
13
star
56

composr

Live visualization of audio data in 3D
JavaScript
12
star
57

Central64

Central Grid Path Planning with up to 64 Neighbors
C++
10
star
58

forge-cli

Forge Command Line Interface (CLI) is a unified tool to manage your Forge services
JavaScript
8
star
59

NetfabbApplicationServer

The "Netfabb Application Server" (short: NAS) is a server (or: service) program written in Go, exposing a RESTful webservice API to client programs for a central storage system and distributed task management.
Go
7
star
60

jira-ldap-group-sync-plugin

LDAP Groups Sync for JIRA plugin
Java
7
star
61

IGA-saveload

C++
6
star
62

ADAppRater-UWP

A library for UWP designed to facilitate easy prompting of users to rate your app within the Windows store.
C#
6
star
63

jenkinsci-benchmark-plugin

Java
6
star
64

sjson

Simplified JSON parser
JavaScript
3
star
65

cryptorito

Very lightweight library for some cryptographic interactions
Python
3
star
66

powerinspect-api-examples

This addin exports measurement data from the PowerInspect product and formats it for use by various third-party Statistical Process Control (SPC) packages.
C#
2
star
67

motion-controller-emulator

A marlin firmware emulator. Created as a tool for supporting Machine Collaboration Utility (https://github.com/Autodesk/machine-collaboration-utility)
JavaScript
2
star
68

data-unit-test

JDBC test framework
Java
2
star
69

jira-action-reminders-plugin

JIRA Action Reminders Plugin
Java
2
star
70

featurecam-api-examples

Feature CAM API Examples (No Maintenance)
C#
1
star
71

viriondb

JavaScript
1
star