1 25 26 package org.objectweb.perseus.concurrency.pessimistic; 27 28 import org.objectweb.perseus.concurrency.api.ConcurrencyException; 29 import org.objectweb.perseus.concurrency.lib.RWLockValue; 30 import org.objectweb.perseus.dependency.api.DependencyGraph; 31 32 38 public final class MutexLock extends Lock { 39 43 protected Object writer = null; 44 47 int waiter = 0; 48 49 public MutexLock() { 50 } 51 52 public MutexLock(Object hints, DependencyGraph dg) { 53 super(hints, dg); 54 } 55 56 61 public void readIntention(Object ctxt) 62 throws ConcurrencyException { 63 writeIntention(ctxt); 64 } 65 66 71 public synchronized void writeIntention(Object ctxt) 72 throws ConcurrencyException { 73 boolean ok; 74 do { 75 ok = ((writer == null) || writer.equals(ctxt)); 76 if (!ok) { 77 waiter ++; 78 try { 79 wait(); 80 } catch (InterruptedException e) { 81 throw new ConcurrencyException( 82 "Waiting of a read intention has been interupted: ", e); 83 } finally { 84 waiter --; 85 } 86 } 87 } while (!ok); 88 reservations--; 89 writer = ctxt; 90 } 91 92 100 public synchronized boolean close(Object ctxt) { 101 if ((writer != null) && writer.equals(ctxt)) { 103 writer = null; 104 } 105 boolean res = reservations == 0 && writer == null && waiter == 0; 106 if (!res) { 107 notifyAll(); 108 } 109 return res; 110 } 111 112 public synchronized byte getMax() { 113 if (writer != null) return RWLockValue.WRITE; 114 return RWLockValue.NOLOCK; 115 } 116 } 117 | Popular Tags |