/* * COM1317 Project * LockManager class * LCB class * RCB class * Copyright (C) Jing Shan 2003 */ import java.util.*; class LockManager{ private Hashtable locktbl; private int TIMEOUT = 2000; LockManager(){ locktbl = new Hashtable(); } public boolean lock(Transaction t, String mode, String resource) throws DeadLockException{ /* your code goes here */ return true; } public boolean unlockAll(Transaction t){ /* your code goes here */ return true; } private boolean unlock(int id, String resource, LCB l){ /* your code goes here */ return true; } } class LCB{ private int status; /* 0-- granted, 1 -- waiting*/ private int mode; /* 0-- read, 1 -- write */ private int Xid; private boolean notified; private String resource; LCB(int stus, int mde, String r, int id){ status = stus; mode = mde; Xid = id; notified = false; resource = r; System.out.println(); System.out.println("create a new LCB: status = "+ status+", mode ="+mode+" ,resource= "+resource+", Xid ="+Xid); System.out.println(); } public synchronized void LCBWait(int t){ try { wait(t); }catch (InterruptedException e){ System.out.println("interrupted by others:: "+e); } } public synchronized void LCBNotify(){ notify(); notified = true; } public boolean isNotified(){ return notified; } public int getStatus(){ return status; } public int getMode(){ return mode; } public String getResource(){ return resource; } public void setMode(int m){ mode = m; } public void setStatus(int s){ status = s; } } class RCB{ private String name;//resource name private LinkedList lockQueue; private int mode; /* 0-- read lock, 1-- write lock*/ private int status; /* 0-- no wait, 1-- there is at least one LCB waiting */ RCB(String n){ name = n; lockQueue = new LinkedList(); System.out.println("create an RCB for resource "+ name); } public void addRequest(LCB l) { if (lockQueue.size() == 0){ mode = l.getMode(); status = 0; } lockQueue.addLast(l); } public void removeRequest(LCB l){ if (! (lockQueue.remove(l))) System.out.println("Exception:: LCB"+l+" is not in the lock queue"); } public int getMode(){ return mode; } public int getStatus(){ return status; } public LinkedList getLockQueue(){ return lockQueue; } public int getLockNumber(){ return lockQueue.size(); } public void setStatus(int i){ status = i; } public void setMode(int i){ mode = i; } public String name(){ return name; } public void print(){ System.out.println(); System.out.println("RCB for resource "+name+", lock mode="+ mode+" , status ="+status+" , number of LCBs in queue="+lockQueue.size()); System.out.println(); } }