# HG changeset patch # User alanb # Date 1570528716 -3600 # Node ID 7f34de3cdfe9c5cdb1c5e902441cc0cf753f0660 # Parent 55a8d95c7787755aade3f713226d7c86d6424214 8231921: (se) SelectorImpl.register does not need to set the attachment when it is null Reviewed-by: bpb diff -r 55a8d95c7787 -r 7f34de3cdfe9 src/java.base/share/classes/java/nio/channels/SelectionKey.java --- a/src/java.base/share/classes/java/nio/channels/SelectionKey.java Tue Oct 08 10:24:22 2019 +0200 +++ b/src/java.base/share/classes/java/nio/channels/SelectionKey.java Tue Oct 08 10:58:36 2019 +0100 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -25,7 +25,8 @@ package java.nio.channels; -import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; +import java.lang.invoke.MethodHandles; +import java.lang.invoke.VarHandle; /** * A token representing the registration of a {@link SelectableChannel} with a @@ -428,13 +429,17 @@ // -- Attachments -- + private static final VarHandle ATTACHMENT; + static { + try { + MethodHandles.Lookup l = MethodHandles.lookup(); + ATTACHMENT = l.findVarHandle(SelectionKey.class, "attachment", Object.class); + } catch (Exception e) { + throw new InternalError(e); + } + } private volatile Object attachment; - private static final AtomicReferenceFieldUpdater - attachmentUpdater = AtomicReferenceFieldUpdater.newUpdater( - SelectionKey.class, Object.class, "attachment" - ); - /** * Attaches the given object to this key. * @@ -450,7 +455,7 @@ * otherwise {@code null} */ public final Object attach(Object ob) { - return attachmentUpdater.getAndSet(this, ob); + return ATTACHMENT.getAndSet(this, ob); } /** diff -r 55a8d95c7787 -r 7f34de3cdfe9 src/java.base/share/classes/sun/nio/ch/SelectorImpl.java --- a/src/java.base/share/classes/sun/nio/ch/SelectorImpl.java Tue Oct 08 10:24:22 2019 +0200 +++ b/src/java.base/share/classes/sun/nio/ch/SelectorImpl.java Tue Oct 08 10:58:36 2019 +0100 @@ -208,7 +208,8 @@ if (!(ch instanceof SelChImpl)) throw new IllegalSelectorException(); SelectionKeyImpl k = new SelectionKeyImpl((SelChImpl)ch, this); - k.attach(attachment); + if (attachment != null) + k.attach(attachment); // register (if needed) before adding to key set implRegister(k);