1 2 5 14 package org.jacorb.trading.util; 15 16 20 public class RWLock 21 { 22 private int m_numWaitingReaders = 0; 23 private int m_numWaitingWriters = 0; 24 private int m_refCount = 0; 25 26 27 public RWLock() 28 { 29 } 30 31 32 33 public synchronized void acquireWrite() 34 { 35 while (m_refCount != 0) { 36 m_numWaitingWriters++; 37 try { 38 wait(); 39 } 40 catch (InterruptedException e) { 41 } 42 m_numWaitingWriters--; 43 } 44 45 m_refCount = -1; 47 } 48 49 50 51 public synchronized void acquireRead() 52 { 53 while (m_refCount < 0 || m_numWaitingWriters > 0) { 55 m_numWaitingReaders++; 56 try { 57 wait(); 58 } 59 catch (InterruptedException e) { 60 } 61 m_numWaitingReaders--; 62 } 63 64 m_refCount++; 66 } 67 68 69 70 public synchronized void release() 71 { 72 if (m_refCount > 0) m_refCount--; 74 else if (m_refCount == -1) m_refCount = 0; 76 else 77 throw new RuntimeException ("refCount should not be 0"); 78 79 notifyAll(); 80 } 81 } 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | Popular Tags |