Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-dev] IllegalMonitorStateException using locks

Also, see my suggestion to your question on the forum: http://www.eclipse.org/forums/index.php/t/485975/


On Wed, May 1, 2013 at 12:40 PM, Giles Reger <oneandonlygiles@xxxxxxxxx> wrote:

When you call wait in lockRead and lockWrite you are not holding the monitor if the enclosing RWPriorityMonitorLock - you hold the monitor of a different lock.

On May 1, 2013 6:06 PM, "Pedro Martins" <pmp.martins@xxxxxxxxxxxxxxxxx> wrote:
Hellow, I have a code that uses multiple threads, and so I need read and write locks for it. Simply put, this is what I have so far:

[code]
public interface IReadWriteLock{
    public void lockWrite();
    public void unlockWrite();
   
    public void lockRead();
    public void unlockRead();
}
[/code]

This interface would then be implemented by a class:

[code]
public class RWPriorityMonitorLock implements IReadWriteLock{
   
    private boolean dbWritting, writterWantsIn;
    private int numReaders;
   
    public RWPriorityMonitorLock() {
        dbWritting = false;
        numReaders = 0;
        writterWantsIn = false;
    }
   
    @Override
    public void lockRead() {
        while(dbWritting || writterWantsIn)
            try{
                wait();
            }catch(InterruptedException e){ e.printStackTrace(); }
           
        numReaders++;
    }

    @Override
    public void unlockRead() {
        numReaders--;
       
        if(numReaders == 0)
            notify();
    }

    @Override
    public void lockWrite() {
        writterWantsIn = true;
        while(numReaders > 0 || dbWritting)
            try{
                wait();
            }catch(InterruptedException e){ e.printStackTrace(); }
       
        dbWritting = true;
        writterWantsIn = false;
    }

    @Override
    public void unlockWrite() {
        dbWritting = false;
        notifyAll();
    }
}
[/code]

Now, in order to force synchronization , I use the aspect:

[code]
public aspect IReadWriteLockAspect {

    private static Map<IReadWriteLock, Lock> allLocks = new HashMap<>();

    pointcut lockReflectInit(): call(public Object
            java.lang.reflect.Constructor.newInstance(..)); 

    pointcut hardWaitMutex():
        call(public void IReadWriteLock+.*());

    void around(): hardWaitMutex(){
        IReadWriteLock currentLock = (IReadWriteLock) thisJoinPoint.getTarget();
        Lock mutex = allLocks.get(currentLock);
        mutex.lock();
        try{
            proceed();
        }finally{
            mutex.unlock();
        }
    }

    Object around(): lockReflectInit() {
        IReadWriteLock lockType = (IReadWriteLock)proceed();
        allLocks.put(lockType, new ReentrantLock(true));
        return lockType;
    }
}
[/code]

The point is, every time I try to execute something, I get an IllegalMonitorStateException. In this link:
http://stackoverflow.com/questions/1537116/illegalmonitorstateexception-on-wait-call

They say that the thread must own the monitor, but I don't see where that is not happening. Can some one help?

_______________________________________________
aspectj-dev mailing list
aspectj-dev@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-dev


_______________________________________________
aspectj-dev mailing list
aspectj-dev@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-dev



Back to the top