1 29 package org.jruby.internal.runtime; 30 31 36 public class AtomicSpinlock { 37 private volatile int counter; 38 39 public AtomicSpinlock() { 40 counter = 0; 42 } 43 44 public AtomicSpinlock(int start) { 45 counter = start; 46 } 47 48 public synchronized void increment() { 49 counter++; 50 notify(); 51 } 52 53 public synchronized void decrement() { 54 counter--; 55 notify(); 56 } 57 58 public synchronized void waitForZero(long timeout) throws InterruptedException { 59 while (counter > 0) { 61 timeWait(timeout); 62 } 63 } 64 65 public synchronized void waitForValue(long timeout, int value) throws InterruptedException { 66 while (counter != value) { 67 timeWait(timeout); 68 } 69 } 70 71 private void timeWait(long timeout) throws InterruptedException { 72 if (timeout > 0) wait(timeout); 73 else wait(); 74 } 75 } 76 | Popular Tags |