summaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/io/UpdateInterest.java
blob: bb718218aeb116170dddcc28e384901f3a41c1f8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.io;

import java.nio.channels.SelectionKey;


/**
 * Command object to perform interest set updates.  Workaround for NIO
 * design flaw which makes it impossible to update the interest set of
 * a SelectionKey while select() is in progress.  There should be a
 * more elegant way around this, but if it turns out to be performant
 * enough we leave it like this.
 *
 * <P>
 * Of course, the ideal would be to have NIO fixed.
 *
 * @author <a href="mailto:travisb@yahoo-inc.com">Bob Travis</a>
 * @author <a href="mailto:borud@yahoo-inc.com">Bjorn Borud</a>
 */
public class UpdateInterest {
    private SelectionKey key;
    private int operation;
    private boolean set;

    /**
     * Make sure this can't be run
     */
    @SuppressWarnings("unused")
    private UpdateInterest() {}

    /**
     * Create an object for encapsulating a interest set change
     * request.
     *
     * @param key The key we wish to update
     * @param operation The operation we wish to set or remove
     * @param set Whether we want to set (true) or clear (false) the
     *            operation in the interest set
     */
    public UpdateInterest(SelectionKey key, int operation, boolean set) {
        this.key = key;
        this.operation = operation;
        this.set = set;
    }

    /**
     * This method is used for actually applying the updates to the
     * SelectionKey in question at a time when it is safe to do so.
     * If the SelectionKey has been invalidated in the meanwhile we
     * do nothing.
     */
    public void doUpdate() {
        // bail if this key isn't valid anymore
        if ((key == null) || (!key.isValid())) {
            return;
        }

        if (set) {
            key.interestOps(key.interestOps() | operation);
        } else {
            key.interestOps(key.interestOps() & (~operation));
        }
    }
}