Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AbstractQueuedSynchronizer.acquireShared waits infinitely even that waiting condition has changed

I wrote a simple class that uses AbstractQueuedSynchronizer. I wrote a class that represents a "Gate", that can be passed if open, or is blocking if closed. Here is the code:

public class GateBlocking {

  final class Sync extends AbstractQueuedSynchronizer {
    public Sync() {
      setState(0);
    }

    @Override
    protected int tryAcquireShared(int ignored) {
      return getState() == 1 ? 1 : -1;
    }

    public void reset(int newState) {
      setState(newState);
    }
  };

  private Sync sync = new Sync();

  public void open() {
    sync.reset(1);
  }

  public void close() {
    sync.reset(0);
  }

public void pass() throws InterruptedException {
    sync.acquireShared(1);
  }

};

Unfortunately, if a thread blocks on pass method because gate is closed and some other thread opens the gate in meantime, the blocked one doesn't get interrupted - It blocks infinitely. Here is a test that shows it:

public class GateBlockingTest {

    @Test
    public void parallelPassClosedAndOpenGate() throws Exception{
        final GateBlocking g = new GateBlocking();
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                    g.open();
                } catch (InterruptedException e) {
                }
            }
        });


        t.start();
        g.pass();
    }
}

Please help, what should I change to make the gate passing thread acquire the lock successfully.

like image 915
zarzyk Avatar asked Nov 04 '22 09:11

zarzyk


1 Answers

It looks like setState() only changes the state, but doesn't notify blocked threads about the change.

Therefore you should use acquire/release methods instead:

@Override
protected boolean tryReleaseShared(int ignored) {
    setState(1);
    return true;
}
...
public void open() {
   sync.releaseShared(1);
}

So, overall workflow of AbstractQueuedSynchronizer looks like follows:

  • Clients call public acquire/release methods

  • These methods arrange all synchronization functionality and delegate actual locking policy to protected try*() methods

  • You define your locking policy in protected try*() methods using getState()/setState()/compareAndSetState()

like image 108
axtavt Avatar answered Nov 11 '22 07:11

axtavt