question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is reentrant lock for coroutines on the roadmap?

I’ve seen a discussion of it where a problem of coroutine identity was raised (https://github.com/Kotlin/kotlinx.coroutines/issues/965). But couldn’t a Job in the coroutine context be that identity?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:19 (8 by maintainers)

github_iconTop GitHub Comments

10reactions
vkliducommented, Feb 11, 2021

There is already a nice example of solution from Roman Elizarov here https://elizarov.medium.com/phantom-of-the-coroutine-afc63b03a131

suspend fun <T> Mutex.withReentrantLock(block: suspend () -> T): T {
  val key = ReentrantMutexContextKey(this)
  // call block directly when this mutex is already locked in the context
  if (coroutineContext[key] != null) return block()
  // otherwise add it to the context and lock the mutex
  return withContext(ReentrantMutexContextElement(key)) {
    withLock { block() }
  }
}

class ReentrantMutexContextElement(
  override val key: ReentrantMutexContextKey
) : CoroutineContext.Element

data class ReentrantMutexContextKey(
  val mutex: Mutex
) : CoroutineContext.Key<ReentrantMutexContextElement>

8reactions
elizarovcommented, Sep 13, 2021

IMO, not including reentrant mutex into the library so that you have to actually spend some time on copying the implementation of the reentrant mutex into your code and thinking on whether you really need it, works better than any @herebedragons annotation would.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ReentrantLock (Java Platform SE 7 ) - Oracle Help Center
A ReentrantLock is owned by the thread last successfully locking, but not yet unlocking it. A thread invoking lock will return, successfully acquiring...
Read more >
Reentrant Lock in Java - GeeksforGeeks
The ReentrantLock class implements the Lock interface and provides synchronization to methods while accessing shared resources. The code which ...
Read more >
What is the Re-entrant lock and concept in general?
A reentrant lock is one where a process can claim the lock multiple times without blocking on itself. It's useful in situations where...
Read more >
Java Lock Example - ReentrantLock - DigitalOcean
ReentrantLock : This is the most widely used implementation class of Lock interface. This class implements the Lock interface in similar way ...
Read more >
ReentrantLock Example in Java, Difference between ...
As per Javadoc, ReentrantLock is a mutual exclusive lock, similar to implicit locking provided by synchronized keyword in Java, with extended features like ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found