1 13 14 package com.sun.corba.se.impl.orbutil.concurrent; 15 16 111 112 import org.omg.CORBA.INTERNAL ; 113 114 public class DebugMutex implements Sync { 115 116 117 protected boolean inuse_ = false; 118 protected Thread holder_ = null; 119 120 public void acquire() throws InterruptedException { 121 if (Thread.interrupted()) throw new InterruptedException (); 122 synchronized(this) { 123 Thread thr = Thread.currentThread(); 124 if (holder_ == thr) 125 throw new INTERNAL ( 126 "Attempt to acquire Mutex by thread holding the Mutex" ) ; 127 128 try { 129 while (inuse_) wait(); 130 inuse_ = true; 131 holder_ = Thread.currentThread(); 132 } 133 catch (InterruptedException ex) { 134 notify(); 135 throw ex; 136 } 137 } 138 } 139 140 public synchronized void release() { 141 Thread thr = Thread.currentThread(); 142 if (thr != holder_) 143 throw new INTERNAL ( 144 "Attempt to release Mutex by thread not holding the Mutex" ) ; 145 holder_ = null; 146 inuse_ = false; 147 notify(); 148 } 149 150 151 public boolean attempt(long msecs) throws InterruptedException { 152 if (Thread.interrupted()) throw new InterruptedException (); 153 synchronized(this) { 154 Thread thr = Thread.currentThread() ; 155 156 if (!inuse_) { 157 inuse_ = true; 158 holder_ = thr; 159 return true; 160 } else if (msecs <= 0) 161 return false; 162 else { 163 long waitTime = msecs; 164 long start = System.currentTimeMillis(); 165 try { 166 for (;;) { 167 wait(waitTime); 168 if (!inuse_) { 169 inuse_ = true; 170 holder_ = thr; 171 return true; 172 } 173 else { 174 waitTime = msecs - (System.currentTimeMillis() - start); 175 if (waitTime <= 0) 176 return false; 177 } 178 } 179 } 180 catch (InterruptedException ex) { 181 notify(); 182 throw ex; 183 } 184 } 185 } 186 } 187 } 188 189 | Popular Tags |