/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.cache;

import org.jgroups.blocks.Xid;

/**
 * Defines how read or write-locks are acquired. Implementations will typically
 * maintain some form of locking table, where they associate transactions and
 * the locks acquired by a transaction.
 * @author Bela Ban
 * @version $Revision: 1.1.2.1 $
 */
public interface LockingPolicy {

    /** Sets the cache on which locks are acquired */
    void setCache(CacheImpl cache);

    /**
     * Acquires a read lock for a given transaction. If <tt>dirty</tt> is true,
     * then no lock needs to be acquired.
     * @param transaction The transaction with which the read-lock is to be
     * associated. If the transaction already holds a read or write-lock, no new
     * lock will be acquired, but we will proceed immediately
     * @param dirty Whether of not a lock needs to be acquired
     * @param lock_acquisition_timeout Number of milliseconds within which the
     * lock has to be acquired. A value of <=0 means to wait forever.
     * @throws LockingException Thrown if the lock cannot be acquired within
     * <tt>lock_acquisition_timeout</tt> milliseconds
     */
    void getReadLock(Xid transaction, boolean dirty, long lock_acquisition_timeout)
            throws LockingException;

    /**
     *
     * @param transaction The transaction with which the write-lock is to be
     * associated. If the transaction already holds a write-lock, no new lock
     * will be acquired, but we will proceed immediately. If the transaction
     * already holds a read-lock, the read-lock will have to be upgraded to a
     * write-lock. If this is not possible, an UpgradeException will need to be
     * thrown.
     * @param lock_acquisition_timeout Number of milliseconds within which the
     * lock has to be acquired. A value of <=0 means to wait forever.
     * @param entire_map Whether or not the entire map needs to be locked. This
     * will only happen on {@link Cache#putAll(java.util.Map)}.
     * @throws LockingException Thrown if the lock cannot be acquired within
     * <tt>lock_acquisition_timeout</tt> milliseconds
     * @throws UpgradeException Thrown when a read-lock cannot be upgraded to a
     * write-lock
     */
    void getWriteLock(Xid transaction, long lock_acquisition_timeout, boolean entire_map)
            throws LockingException, UpgradeException;

    /**
     * Releases all locks held by a given transaction
     * @param transaction The transaction
     */
    void releaseLocks(Xid transaction);
}
