1 13 14 package com.sun.corba.se.impl.orbutil.concurrent; 15 16 import com.sun.corba.se.impl.orbutil.ORBUtility ; 17 18 115 116 import org.omg.CORBA.INTERNAL ; 117 118 public class ReentrantMutex implements Sync { 119 120 121 protected Thread holder_ = null; 122 123 124 protected int counter_ = 0 ; 125 126 protected boolean debug = false ; 127 128 public ReentrantMutex() 129 { 130 this( false ) ; 131 } 132 133 public ReentrantMutex( boolean debug ) 134 { 135 this.debug = debug ; 136 } 137 138 public void acquire() throws InterruptedException { 139 if (Thread.interrupted()) 140 throw new InterruptedException (); 141 142 synchronized(this) { 143 try { 144 if (debug) 145 ORBUtility.dprintTrace( this, 146 "acquire enter: holder_=" + 147 ORBUtility.getThreadName(holder_) + 148 " counter_=" + counter_ ) ; 149 150 Thread thr = Thread.currentThread(); 151 if (holder_ != thr) { 152 try { 153 while (counter_ > 0) 154 wait(); 155 156 if (counter_ != 0) 158 throw new INTERNAL ( 159 "counter not 0 when first acquiring mutex" ) ; 160 161 holder_ = thr; 162 } catch (InterruptedException ex) { 163 notify(); 164 throw ex; 165 } 166 } 167 168 counter_ ++ ; 169 } finally { 170 if (debug) 171 ORBUtility.dprintTrace( this, "acquire exit: holder_=" + 172 ORBUtility.getThreadName(holder_) + " counter_=" + 173 counter_ ) ; 174 } 175 } 176 } 177 178 void acquireAll( int count ) throws InterruptedException 179 { 180 if (Thread.interrupted()) 181 throw new InterruptedException (); 182 183 synchronized(this) { 184 try { 185 if (debug) 186 ORBUtility.dprintTrace( this, 187 "acquireAll enter: count=" + count + " holder_=" + 188 ORBUtility.getThreadName(holder_) + " counter_=" + 189 counter_ ) ; 190 Thread thr = Thread.currentThread(); 191 if (holder_ == thr) { 192 throw new INTERNAL ( 193 "Cannot acquireAll while holding the mutex" ) ; 194 } else { 195 try { 196 while (counter_ > 0) 197 wait(); 198 199 if (counter_ != 0) 201 throw new INTERNAL ( 202 "counter not 0 when first acquiring mutex" ) ; 203 204 holder_ = thr; 205 } catch (InterruptedException ex) { 206 notify(); 207 throw ex; 208 } 209 } 210 211 counter_ = count ; 212 } finally { 213 if (debug) 214 ORBUtility.dprintTrace( this, "acquireAll exit: count=" + 215 count + " holder_=" + ORBUtility.getThreadName(holder_) + 216 " counter_=" + counter_ ) ; 217 } 218 } 219 } 220 221 public synchronized void release() 222 { 223 try { 224 if (debug) 225 ORBUtility.dprintTrace( this, "release enter: " + 226 " holder_=" + ORBUtility.getThreadName(holder_) + 227 " counter_=" + counter_ ) ; 228 229 Thread thr = Thread.currentThread(); 230 if (thr != holder_) 231 throw new INTERNAL ( 232 "Attempt to release Mutex by thread not holding the Mutex" ) ; 233 else 234 counter_ -- ; 235 236 if (counter_ == 0) { 237 holder_ = null; 238 notify(); 239 } 240 } finally { 241 if (debug) 242 ORBUtility.dprintTrace( this, "release exit: " + 243 " holder_=" + ORBUtility.getThreadName(holder_) + 244 " counter_=" + counter_ ) ; 245 } 246 } 247 248 synchronized int releaseAll() 249 { 250 try { 251 if (debug) 252 ORBUtility.dprintTrace( this, "releaseAll enter: " + 253 " holder_=" + ORBUtility.getThreadName(holder_) + 254 " counter_=" + counter_ ) ; 255 256 Thread thr = Thread.currentThread(); 257 if (thr != holder_) 258 throw new INTERNAL ( 259 "Attempt to releaseAll Mutex by thread not holding the Mutex" ) ; 260 261 int result = counter_ ; 262 counter_ = 0 ; 263 holder_ = null ; 264 notify() ; 265 return result ; 266 } finally { 267 if (debug) 268 ORBUtility.dprintTrace( this, "releaseAll exit: " + 269 " holder_=" + ORBUtility.getThreadName(holder_) + 270 " counter_=" + counter_ ) ; 271 } 272 } 273 274 public boolean attempt(long msecs) throws InterruptedException { 275 if (Thread.interrupted()) 276 throw new InterruptedException (); 277 278 synchronized(this) { 279 try { 280 if (debug) 281 ORBUtility.dprintTrace( this, "attempt enter: msecs=" + 282 msecs + " holder_=" + 283 ORBUtility.getThreadName(holder_) + 284 " counter_=" + counter_ ) ; 285 286 Thread thr = Thread.currentThread() ; 287 288 if (counter_==0) { 289 holder_ = thr; 290 counter_ = 1 ; 291 return true; 292 } else if (msecs <= 0) { 293 return false; 294 } else { 295 long waitTime = msecs; 296 long start = System.currentTimeMillis(); 297 try { 298 for (;;) { 299 wait(waitTime); 300 if (counter_==0) { 301 holder_ = thr; 302 counter_ = 1 ; 303 return true; 304 } else { 305 waitTime = msecs - 306 (System.currentTimeMillis() - start); 307 308 if (waitTime <= 0) 309 return false; 310 } 311 } 312 } catch (InterruptedException ex) { 313 notify(); 314 throw ex; 315 } 316 } 317 } finally { 318 if (debug) 319 ORBUtility.dprintTrace( this, "attempt exit: " + 320 " holder_=" + ORBUtility.getThreadName(holder_) + 321 " counter_=" + counter_ ) ; 322 } 323 } 324 } 325 } 326 327 | Popular Tags |