Mutex guarantee concurrency-safe access in Native and JS platforms?

Help me sort out this issue. Does Kotlin Mutex.witLock{} guarantee concurrency-safe access in Native and JS platforms?
It is known that JS runs on a single thread. But, one thread can serve several parallel coroutines. Mutex provides
personal code access among parallel coroutines on these platforms?

I didn’t use Mutex for neither JS or native, but I don’t see why it wouldn’t guarantee concurrency-safety. withLock() basically locks at the beginning of the code block and unlocks at the end. It doesn’t matter what thread do you use, nobody should be able to get inside withLock() until some other coroutine leaves it.


If it didn’t do that, what would it do?

I can’t waste time searching for an answer to this question. My question is quite logical.

Do you assume so, or do you know it?

It is pretty clear from the documentation, isn’t it? First sentence explaining the Mutex:

Mutual exclusion for coroutines.

Mutual exclusion, therefore it guarantees that only a single coroutine enters a critical section. Also, documentation for lock() says:

Locks this mutex, suspending caller while the mutex is locked.

So after we locked the mutex, all other coroutines have to wait until we unlock it.

I think it would be easier to understand your question if you start by explaining why you think / you are concerned that mutex in kotlin may do a different thing than in all other languages and concurrency frameworks. Otherwise, this is like a discussion if we are sure that 5 + 4 = 9 in Kotlin - we all know this is true, but it is hard to provide any proofs.

Thank you. That’s exactly what I wanted to know. Where can I see this documentation? And, most importantly: do all the objects, with their properties and functions, described in the documentation, apply to multiplatform? (JVM, JS, Native). If one or another object does not work on all platforms (for example, as a runBlocking section), then where can I see which platform it will work on? Thank you for the answer.

Documentation for Mutex is here: Mutex . You can also read the section about mutexes here: Shared mutable state and concurrency | Kotlin

In the same documentation, there is a remark “Common”. This makes me doubt: if one or another object is available in Native, then the note is appropriate. For example, the println function has remarks: Common, JS, JVM, Native. What says that it is available in any project. Mutex, on the other hand, has only the remark Common. And I can’t figure out if it will work in JS.