2020-07-24 19:38:48 +02:00
|
|
|
package io.netty.buffer.b2;
|
|
|
|
|
|
|
|
import java.lang.invoke.VarHandle;
|
|
|
|
|
|
|
|
import static io.netty.buffer.b2.Statics.*;
|
|
|
|
import static java.lang.invoke.MethodHandles.*;
|
|
|
|
|
2020-08-24 16:34:32 +02:00
|
|
|
class TransferSend<T extends Rc<T> & Owned<T>> implements Send<T> {
|
2020-07-24 19:38:48 +02:00
|
|
|
private static final VarHandle RECEIVED = findVarHandle(lookup(), TransferSend.class, "received", boolean.class);
|
|
|
|
private final T outgoing;
|
|
|
|
private final Drop<T> drop;
|
|
|
|
@SuppressWarnings("unused")
|
|
|
|
private volatile boolean received; // Accessed via VarHandle
|
|
|
|
|
|
|
|
TransferSend(T outgoing, Drop<T> drop) {
|
|
|
|
this.outgoing = outgoing;
|
|
|
|
this.drop = drop;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public T receive() {
|
|
|
|
if (!RECEIVED.compareAndSet(this, false, true)) {
|
|
|
|
throw new IllegalStateException("This object has already been received.");
|
|
|
|
}
|
2020-08-11 10:01:21 +02:00
|
|
|
var copy = outgoing.transferOwnership(Thread.currentThread(), drop);
|
2020-08-10 12:15:02 +02:00
|
|
|
drop.accept(copy);
|
|
|
|
return copy;
|
2020-07-24 19:38:48 +02:00
|
|
|
}
|
|
|
|
}
|