1 package locklib; 2 3 /** 4 * A lock policy defines how a locking mechanism works. It is parameterized by 5 * a class <code>LockType</code> which represents a type of lock. The policy 6 * defines how different lock types operate with each other. 7 * @param <LockType> the type of lock 8 */ 9 public interface LockPolicy<LockType> { 10 /** 11 * Checks if two locks are compatible. Two locks are compatible if 12 * different holders can hold them in the same target. 13 * @param lock_type_1 the type of lock of one holder 14 * @param lock_type_2 the type of lock of another holder 15 * @return are both locks compatible? 16 */ 17 boolean is_compatible(LockType lock_type_1, LockType lock_type_2); 18 19 /** 20 * Determines what is the type of lock a parent of a target should have. 21 * @param lock_type the type of lock held by the target 22 * @return the type of lock that a parent should hold if its child has 23 * lock <code>lock_type</code>; this must not return <code>null</code> 24 * as the parent must have <em>some</em> lock type to generate the 25 * lock chain 26 */ 27 LockType parent_lock(LockType lock_type); 28 29 /** 30 * If starvation is allowed compatible locks can be acquired even if there 31 * are locks waiting. For example, in SX locks, it means a S lock can be 32 * acquired for a target when there is an X lock waiting. This means that 33 * the X lock can be held forever if S locks keep coming and going. If 34 * starvation is not allowed, then, in the previous example, an S lock 35 * is disallowed if there is an X lock waiting, even if the S lock is 36 * compatible with the currently-held lock. Whether starvation is allowed 37 * or not is irrelevant for lock types that are always exclusive. 38 * @return is starvation allowed 39 */ 40 boolean starvation_allowed(); 41 }