Skip to content

Commit

Permalink
Merge pull request #2901 from jembishop/PV-completablefuture
Browse files Browse the repository at this point in the history
implement completable future on PV class
  • Loading branch information
kasemir authored Jan 10, 2024
2 parents 39e9cef + 1ab912a commit 8f401f6
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 19 deletions.
13 changes: 6 additions & 7 deletions core/pv/src/main/java/org/phoebus/pv/PV.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
Expand Down Expand Up @@ -236,17 +235,17 @@ public VType read()

/** Issue a read request
*
* <p>{@link Future} allows waiting for
* <p>{@link CompletableFuture} allows waiting for
* and obtaining the result, or its <code>get()</code>
* calls will provide an error.
*
* <p>As a side effect, registered listeners will
* also receive the value obtained by this call.
*
* @return {@link Future} for obtaining the result or Exception
* @return {@link CompletableFuture} for obtaining the result or Exception
* @exception Exception on error
*/
public Future<VType> asyncRead() throws Exception
public CompletableFuture<VType> asyncRead() throws Exception
{
// Default: Return last known value
return CompletableFuture.completedFuture(last_value);
Expand All @@ -270,17 +269,17 @@ public void write(final Object new_value) throws Exception

/** Write value with confirmation
*
* <p>{@link Future} can be used to await completion
* <p>{@link CompletableFuture} can be used to await completion
* of the write.
* The <code>get()</code> will not return a useful value (null),
* but they will throw an error if the write failed.
*
* @param new_value Value to write to the PV
* @return {@link Future} for awaiting completion or exception
* @return {@link CompletableFuture} for awaiting completion or exception
* @exception Exception on error
* @see #write(Object)
*/
public Future<?> asyncWrite(final Object new_value) throws Exception
public CompletableFuture<?> asyncWrite(final Object new_value) throws Exception
{ // Default: Normal write, declare 'done' right away
write(new_value);
return CompletableFuture.completedFuture(null);
Expand Down
4 changes: 2 additions & 2 deletions core/pv/src/main/java/org/phoebus/pv/ca/JCA_PV.java
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ public void getCompleted(final GetEvent ev)
}

@Override
public Future<VType> asyncRead() throws Exception
public CompletableFuture<VType> asyncRead() throws Exception
{
final DBRType type = channel.getFieldType();
if (type == null || type == DBRType.UNKNOWN)
Expand Down Expand Up @@ -453,7 +453,7 @@ public void write(final Object new_value) throws Exception
}

@Override
public Future<?> asyncWrite(final Object new_value) throws Exception
public CompletableFuture<?> asyncWrite(final Object new_value) throws Exception
{
final PutCallbackFuture result = new PutCallbackFuture();
performWrite(new_value, result);
Expand Down
5 changes: 3 additions & 2 deletions core/pv/src/main/java/org/phoebus/pv/opva/PVA_PV.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package org.phoebus.pv.opva;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import java.util.logging.Level;

Expand Down Expand Up @@ -249,7 +250,7 @@ public void unlisten(final Monitor monitor)

/** {@inheritDoc} */
@Override
public Future<VType> asyncRead() throws Exception
public CompletableFuture<VType> asyncRead() throws Exception
{
final PVGetHandler result = new PVGetHandler(this);
channel.createChannelGet(result, read_request);
Expand All @@ -265,7 +266,7 @@ public void write(final Object new_value) throws Exception

/** {@inheritDoc} */
@Override
public Future<?> asyncWrite(Object new_value) throws Exception
public CompletableFuture<?> asyncWrite(Object new_value) throws Exception
{
if (enum_labels != null && new_value instanceof String)
{ // Convert string-for-enum into index of corresponding label
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* @author Kay Kasemir
*/
@SuppressWarnings("nls")
class PVGetHandler extends PVRequester implements ChannelGetRequester, Future<VType>
class PVGetHandler extends PVRequester implements ChannelGetRequester
{
final private PVA_PV pv;

Expand Down
7 changes: 4 additions & 3 deletions core/pv/src/main/java/org/phoebus/pv/opva/PVPutHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@
import org.epics.pvdata.pv.Status;
import org.epics.pvdata.pv.Structure;
import org.phoebus.pv.PV;
import org.epics.vtype.VType;

/** A {@link ChannelPutRequester} for writing a value to a {@link PVA_PV},
* indicating completion via a {@link Future}
*
* @author Kay Kasemir
*/
@SuppressWarnings("nls")
class PVPutHandler extends PVRequester implements ChannelPutRequester, Future<Object>
class PVPutHandler extends PVRequester implements ChannelPutRequester
{
final private PV pv;
final private Object new_value;
Expand Down Expand Up @@ -136,7 +137,7 @@ public boolean isDone()

// Future
@Override
public Object get() throws InterruptedException, ExecutionException
public VType get() throws InterruptedException, ExecutionException
{
updates.await();
if (error != null)
Expand All @@ -146,7 +147,7 @@ public Object get() throws InterruptedException, ExecutionException

// Future
@Override
public Object get(final long timeout, final TimeUnit unit) throws InterruptedException,
public VType get(final long timeout, final TimeUnit unit) throws InterruptedException,
ExecutionException, TimeoutException
{
if (! updates.await(timeout, unit))
Expand Down
4 changes: 3 additions & 1 deletion core/pv/src/main/java/org/phoebus/pv/opva/PVRequester.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@

import static org.phoebus.pv.PV.logger;

import java.util.concurrent.CompletableFuture;
import java.util.logging.Level;

import org.epics.pvdata.pv.MessageType;
import org.epics.pvdata.pv.Requester;
import org.epics.vtype.VType;

/** Base for PVAccess {@link Requester}
* @author Kay Kasemir
*/
class PVRequester implements Requester
class PVRequester extends CompletableFuture<VType> implements Requester
{
@Override
public String getRequesterName()
Expand Down
7 changes: 4 additions & 3 deletions core/pv/src/main/java/org/phoebus/pv/pva/PVA_PV.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package org.phoebus.pv.pva;

import java.util.BitSet;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -96,11 +97,11 @@ private void handleMonitor(final PVAChannel channel,
}

@Override
public Future<VType> asyncRead() throws Exception
public CompletableFuture<VType> asyncRead() throws Exception
{
final Future<PVAStructure> data = channel.read(name_helper.getRequest());
// Wrap into Future that converts PVAStructure into VType
return new Future<>()
return new CompletableFuture<>()
{
@Override
public boolean cancel(final boolean mayInterruptIfRunning)
Expand Down Expand Up @@ -190,7 +191,7 @@ public void write(final Object new_value) throws Exception
}

@Override
public Future<?> asyncWrite(final Object new_value) throws Exception
public CompletableFuture<?> asyncWrite(final Object new_value) throws Exception
{
// Perform a put with completion,
// i.e., process target and block until processing completes,
Expand Down

0 comments on commit 8f401f6

Please sign in to comment.