Open mukul-rathi opened 4 years ago
A better solution: See https://github.com/openjdk-mirror/jdk7u-jdk/blob/master/src/share/classes/java/util/concurrent/locks/ReentrantReadWriteLock.java
Store reader + writer lock asshorts
(16 bits) each - so the upper bits of a int32 refers to reader count, the lower bits refer to reader.
Then update = a single CAS instead of updating reader & writer count individually.
This means you don't need all this faff of another bool field.
the lock / unlock functions should take in two arguments - the memory addresses of the pthread id and lock counter respectively. (llvm::PointerType ?)
when releasing writer lock need to set threadID to null (to prevent other threads reading a stale copy of the owning thread).
fix #122 whilst at it
Could instead try to use a pthread_rw_mutex()
with a custom field that checks owner thread set - so don't try to acquire lock if thread has already acquired it. This simplifies the custom lock and also makes sure we have reentrancy as pthread_rw_mutex()
deadlocks on reentrancy
(Bug - not updating field)
let (i = 0; i< 10; i++){
x.f := x.f + i
}
x.f is set to 0 if using a locked capability, rather than 55 (1+2+...+10).
Just write the lock in C and link in the function, and use C atomics / insert memory fence using LLVM.
Right now the lock is taken on the whole object, we should instead have a lock per capability, covering only those fields touched by the capability.
Create a struct containing locking info and have a pointer in the class
And then we refactor so calling lock / unlock will call a function rather than generating IR for each instance of lock/unlock.
And for the unlock operation