reset password
Author Message
cysun
Posts: 2935
Posted 15:38 Aug 27, 2009 |

SimpleDB's locking implementation is a bit weird as the condition checks are done in two different classes, but after reading the code more carefully I think it does work.

Here's the slock() and xlock() methods in ConcurrentMgr, and these are the methods called by a transaction:

    public void sLock( Block blk )
    {
        if( locks.get( blk ) == null )
        {
            locktbl.sLock( blk );
            locks.put( blk, "S" );
        }
    }

    public void xLock( Block blk )
    {
        if( !hasXLock( blk ) )
        {
            sLock( blk );
            locktbl.xLock( blk );
            locks.put( blk, "X" );
        }
    }

Notice that

  • If a transaction already holds a shared lock on a block, a second call to slock() does nothing.
  • To get an exclusive lock, a transaction always gets a shared lock first.

Now here are slock() and xlock() methods in LockTable, and these methods are called by the slock()/xlock() method in ConcurrentMgr:

    public synchronized void sLock( Block blk )
    {
        try
        {
            long timestamp = System.currentTimeMillis();
            while( hasXlock( blk ) && !waitingTooLong( timestamp ) )
                wait( MAX_TIME );
            if( hasXlock( blk ) ) throw new LockAbortException();
            int val = getLockVal( blk ); // will not be negative
            locks.put( blk, val + 1 );
        }
        catch( InterruptedException e )
        {
            throw new LockAbortException();
        }
    }

    synchronized void xLock( Block blk )
    {
        try
        {
            long timestamp = System.currentTimeMillis();
            while( hasOtherSLocks( blk ) && !waitingTooLong( timestamp ) )
                wait( MAX_TIME );
            if( hasOtherSLocks( blk ) ) throw new LockAbortException();
            locks.put( blk, -1 );
        }
        catch( InterruptedException e )
        {
            throw new LockAbortException();
        }
    }

The unusual thing is that xlock() only checks whether there are other shared locks, but not other exclusive lock, and this is because the transaction must already hold a shared lock on the block (remember it's done in ConcurrentMgr's xlock()).

 

Last edited by cysun at 15:38 Aug 27, 2009.