1 14 15 package com.sun.corba.se.impl.orbutil.concurrent; 16 17 import com.sun.corba.se.impl.orbutil.ORBUtility ; 18 19 131 132 public class CondVar { 133 134 protected boolean debug_ ; 135 136 137 protected final Sync mutex_; 138 protected final ReentrantMutex remutex_; 139 140 private int releaseMutex() 141 { 142 int count = 1 ; 143 144 if (remutex_!=null) 145 count = remutex_.releaseAll() ; 146 else 147 mutex_.release() ; 148 149 return count ; 150 } 151 152 private void acquireMutex( int count ) throws InterruptedException 153 { 154 if (remutex_!=null) 155 remutex_.acquireAll( count ) ; 156 else 157 mutex_.acquire() ; 158 } 159 160 175 176 public CondVar(Sync mutex, boolean debug) { 177 debug_ = debug ; 178 mutex_ = mutex; 179 if (mutex instanceof ReentrantMutex) 180 remutex_ = (ReentrantMutex)mutex; 181 else 182 remutex_ = null; 183 } 184 185 public CondVar( Sync mutex ) { 186 this( mutex, false ) ; 187 } 188 189 200 public void await() throws InterruptedException { 201 int count = 0 ; 202 if (Thread.interrupted()) 203 throw new InterruptedException (); 204 205 try { 206 if (debug_) 207 ORBUtility.dprintTrace( this, "await enter" ) ; 208 209 synchronized(this) { 210 count = releaseMutex() ; 211 try { 212 wait(); 213 } catch (InterruptedException ex) { 214 notify(); 215 throw ex; 216 } 217 } 218 } finally { 219 boolean interrupted = false; 221 for (;;) { 222 try { 223 acquireMutex( count ); 224 break; 225 } catch (InterruptedException ex) { 226 interrupted = true; 227 } 228 } 229 230 if (interrupted) { 231 Thread.currentThread().interrupt(); 232 } 233 234 if (debug_) 235 ORBUtility.dprintTrace( this, "await exit" ) ; 236 } 237 } 238 239 255 256 public boolean timedwait(long msecs) throws InterruptedException { 257 258 if (Thread.interrupted()) 259 throw new InterruptedException (); 260 261 boolean success = false; 262 int count = 0; 263 264 try { 265 if (debug_) 266 ORBUtility.dprintTrace( this, "timedwait enter" ) ; 267 268 synchronized(this) { 269 count = releaseMutex() ; 270 try { 271 if (msecs > 0) { 272 long start = System.currentTimeMillis(); 273 wait(msecs); 274 success = System.currentTimeMillis() - start <= msecs; 275 } 276 } catch (InterruptedException ex) { 277 notify(); 278 throw ex; 279 } 280 } 281 } finally { 282 boolean interrupted = false; 284 for (;;) { 285 try { 286 acquireMutex( count ) ; 287 break; 288 } catch (InterruptedException ex) { 289 interrupted = true; 290 } 291 } 292 293 if (interrupted) { 294 Thread.currentThread().interrupt(); 295 } 296 297 if (debug_) 298 ORBUtility.dprintTrace( this, "timedwait exit" ) ; 299 } 300 return success; 301 } 302 303 308 public synchronized void signal() { 309 notify(); 310 } 311 312 313 public synchronized void broadcast() { 314 notifyAll(); 315 } 316 } 317 | Popular Tags |