Would the use of `compareAndSet` patterns simplify this code to not need these win/loss checks?

The nice thing about `compareAndSet` is it gives that in a very idiomatic manner. 

Reason I suggest it is that this code flow is non-trivial to read and understand with the synchronization gates and conditional flows that all need to be just right in order to work.

Here is an example where I use `compareAndSet` for state changes to achieve timeout in Hystrix: https://github.com/Netflix/Hystrix/blob/master/hystrix-core/src/main/java/com/netflix/hystrix/HystrixCommand.java#L1015

An `AtomicReference` is used to atomically switch between the 3 possible states:

```java
private static enum TimedOutStatus {NOT_EXECUTED, COMPLETED, TIMED_OUT};
private AtomicReference<TimedOutStatus> isTimedOut = new AtomicReference<TimedOutStatus>();
```

The successful flow would attempt this when receiving `onNext`:

```java
// if we can go from NOT_EXECUTED to COMPLETED then we did not timeout
if (isTimedOut.compareAndSet(TimedOutStatus.NOT_EXECUTED, TimedOutStatus.COMPLETED)) {

} else {
    // this means we lost the race and the timeout logic has or is being executed

}
```

The timeout flow would attempt this if the scheduler triggers it:

```java
// if we can go from NOT_EXECUTED to TIMED_OUT then we have not yet completed and should perform timeout logic
if (isTimedOut.compareAndSet(TimedOutStatus.NOT_EXECUTED, TimedOutStatus.TIMED_OUT)) {

} else {
    // this means we lost the race and the work completed before timeout

}
```