I have a parrent coroutine which uses a lock, and inside of that lock Im launching new coroutines. Everythink worked until I realised that I should join the coroutines before I give away the lock. Using join inside withlock{} gives me the exception (I think thats because another thread which doesnt hold the lock continues the parrent coroutine. A simple delay() inside the lock also gives the same Exception).
java.lang.IllegalMonitorStateException
at java.util.concurrent.locks.ReentrantReadWriteLock$Sync.tryRelease(ReentrantReadWriteLock.java:371)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.release(AbstractQueuedSynchronizer.java:1261)
at java.util.concurrent.locks.ReentrantReadWriteLock$WriteLock.unlock(ReentrantReadWriteLock.java:1131)
What I think I need is either some kind of a (read write) lock which opperates on coroutine level instead of thread level (does this exist?), or some kind of Job.join() which does a bussy wait (not a big deal since the jobs are really short lived). Maybe something like a tryJoin which can be used in a loop. Does Kotlin have anything that can do this things?
Edit: I also tried to use coroutineScope {} instead of job.join() but that also throws the Exception inside of the lock.