00001 class WriterPreferenceReadWriteLock 00002 { 00003 protected int activeReaders_; 00004 protected int activeWriter_; 00005 protected final WriterLock writerLock_; 00006 protected int waitingWriters_; 00007 protected final ReaderLock readerLock_; 00008 protected int waitingReaders_; 00009 00010 public WriterPreferenceReadWriteLock() 00011 { 00012 this.activeReaders_ = 0; 00013 this.activeWriter_ = 0; 00014 this.waitingReaders_ = 0; 00015 this.waitingWriters_ = 0; 00016 this.readerLock_ = new ReaderLock(this); 00017 if (MainReadWriteLock.quantification$wl1 == null) 00018 if (Bandera.choose()) 00019 MainReadWriteLock.quantification$wl1 = new WriterLock(this); 00020 if (MainReadWriteLock.quantification$wl2 == null) 00021 if (Bandera.choose()) 00022 MainReadWriteLock.quantification$wl2 = new WriterLock(this); 00023 this.writerLock_ = new WriterLock(this); 00024 } 00025 00026 protected synchronized boolean startReadFromNewReader () 00027 { 00028 boolean pass = (this.startRead()); 00029 if (!pass) 00030 { 00031 this.waitingReaders_ = (this.waitingReaders_ + 1); 00032 } 00033 return pass; 00034 } 00035 00036 protected synchronized boolean startReadFromWaitingReader () 00037 { 00038 boolean pass = (this.startRead()); 00039 if (pass) 00040 { 00041 this.waitingReaders_ = (this.waitingReaders_ - 1); 00042 } 00043 return pass; 00044 } 00045 00046 protected synchronized void cancelledWaitingReader () 00047 { 00048 this.waitingReaders_ = (this.waitingReaders_ - 1); 00049 } 00050 00051 protected synchronized boolean startRead () 00052 { 00053 boolean allowRead = (this.allowReader()); 00054 if (allowRead) 00055 { 00056 this.activeReaders_ = (this.activeReaders_ + 1); 00057 } 00058 return allowRead; 00059 } 00060 00061 protected synchronized Signaller endRead () 00062 { 00063 if (((this.activeReaders_ - 1) == 0) && (this.waitingWriters_ > 0)) 00064 { 00065 return this.writerLock_; 00066 } else 00067 { 00068 return null; 00069 } 00070 } 00071 00072 protected synchronized boolean startWriteFromNewWriter () 00073 { 00074 boolean pass = (this.startWrite()); 00075 if (!pass) 00076 { 00077 this.waitingWriters_ = (this.waitingWriters_ + 1); 00078 } 00079 return pass; 00080 } 00081 00082 protected synchronized boolean startWriteFromWaitingWriter () 00083 { 00084 boolean pass = (this.startWrite()); 00085 if (pass) 00086 { 00087 this.waitingWriters_ = (this.waitingWriters_ - 1); 00088 } 00089 return pass; 00090 } 00091 00092 protected synchronized void cancelledWaitingWriter () 00093 { 00094 this.waitingWriters_ = (this.waitingWriters_ - 1); 00095 } 00096 00097 protected synchronized boolean startWrite () 00098 { 00099 boolean allowWrite = (this.activeReaders_ == 0); 00100 if (allowWrite) 00101 { 00102 this.activeWriter_ = 1; 00103 } 00104 return allowWrite; 00105 } 00106 00107 protected synchronized Signaller endWrite () 00108 { 00109 this.activeWriter_ = 0; 00110 if ((this.waitingReaders_ > 0) && (this.allowReader())) 00111 { 00112 return this.readerLock_; 00113 } else 00114 { 00115 if (this.waitingWriters_ > 0) 00116 { 00117 return this.writerLock_; 00118 } else 00119 { 00120 return null; 00121 } 00122 } 00123 } 00124 00125 protected boolean allowReader () 00126 { 00127 return (this.waitingWriters_ == 0); 00128 } 00129 00130 public Sync readLock () 00131 { 00132 return this.readerLock_; 00133 } 00134 00135 public Sync writeLock () 00136 { 00137 return this.writerLock_; 00138 } 00139 }