diff --git a/CONCURRENCY.md b/CONCURRENCY.md index ef770f339fd..1da6ac15819 100644 --- a/CONCURRENCY.md +++ b/CONCURRENCY.md @@ -23,6 +23,9 @@ Once a worker is started with the `Worker.start` function call, it can be addressed with its own unique integer worker id. Other workers, or non-worker concurrency primitives, such as OS threads, can send a message to the worker with the `execute` call. + +
+ ```kotlin val future = execute(TransferMode.SAFE, { SomeDataForWorker() }) { // data returned by the second function argument comes to the @@ -39,6 +42,9 @@ result -> println("result is $result") } ``` + +
+ The call to `execute` uses a function passed as its second parameter to produce an object subgraph (i.e. set of mutually referring objects) which is then passed as a whole to that worker, it is then no longer available to the thread that initiated the request. This property is checked if the first parameter @@ -92,7 +98,10 @@ mentioned above it is possible to build popular data structures, like concurrent hashmap or shared cache with Kotlin/Native. It is possible to rely upon shared C data, and store in it references to detached object subgraphs. Consider the following .def file: -``` + +
+ +```c package = global --- @@ -103,14 +112,23 @@ typedef struct { SharedData sharedData; ``` + +
+ After running the cinterop tool it can share Kotlin data in a versionized global structure, and interact with it from Kotlin transparently via autogenerated Kotlin like this: + +
+ ```kotlin class SharedData(rawPtr: NativePtr) : CStructVar(rawPtr) { var version: Int var kotlinObject: COpaquePointer? } ``` + +
+ So in combination with the top level variable declared above, it can allow looking at the same memory from different threads and building traditional concurrent structures with platform-specific synchronization primitives.