Kotlin/Native 1.3.50 relaxed mode

OK, so I was reading the Kotlin/Native 1.3.50 changelog, and I suddently went “Wow! Wow wow wow! Woooooow!”.
What the hell is the relaxed memory mode???
This can mean so much, and so little. I couldn’t find any mention of it in a non C++ file.

So, what does it bring?

It seems the K/N team are writing a memory mode that do not require freeze/detach semantic to work (probably to be coroutine-friendly)

This is an extremely experimental mode of operations, making all objects usable from concurrent contexts, making reference count updates immediate (instead of delayed in strict mode) and avoiding freezing of top level values. It leaks cyclical gargabe and must never be used for any production mode at the moment.

1 Like

So… Woooow indeed.
This is kinda game changing.

I hope you realize that, if this ever goes stable, this will be the near death of strict mode. Considering the headaches it brings (mostly due to unforseen exceptions caused by the lack language support). I think most programmers will never go back to strict mode.

Reviewing the mentioned PR, I saw some test about workers accessing mutable main thread data without exception in relaxed mode, but I did not see any test about mutating such data. Will this be supported in relaxed mode ?

This is exactly why kotlin developers are hesitant to introduce this mode. Kotlin is posed as a safe language. And safety is not possible when everyone is using unsafe features. So it is OK, if some lower level libraries like coroutines will be using those features (people know what they are doing there), but if everyone will start using it - it is a catastrophe.


Is there a way to enable it only locally, in a fashion similar to autoreleasepool { } or memScoped? That would allow libraries to take advantage of it after making sure there’s not adding concurrency issues, while keeping the rest of the code safe from there threading issues.

If it is possible, could such a thing work for coroutines, especially when it comes to wrapping blocking code (e.g. using Grand Central Dispatch)? Would it require extra code for each usage or could kotlinx.coroutines enable it under the hood, with no additional config required?

Finally, isn’t relaxed mode similar to what Swift currently has?