aboutsummaryrefslogtreecommitdiffstats
path: root/jdisc_core/src/main/java/com/yahoo/jdisc/refcount/ReferencesByCount.java
blob: 0f417c81a8b1bc8d8e857b62463c3722a537f255 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.refcount;

import com.yahoo.jdisc.ResourceReference;
import com.yahoo.jdisc.SharedResource;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * Does reference counting by using atomic counting of references
 * Default in production
 *
 * @author baldersheim
 */
public class ReferencesByCount implements References {
    private final AtomicInteger refCount;
    private final DestructableResource resource;
    private final NoDebugResourceReference initialReference;

    public ReferencesByCount(DestructableResource resource) {
        refCount = new AtomicInteger(1);
        this.resource = resource;
        initialReference = new NoDebugResourceReference(this);
    }

    @Override
    public void release() {
        initialReference.close();
    }

    @Override
    public int referenceCount() {
        return refCount.get();
    }

    @Override
    public ResourceReference refer(Object context) {
        addRef(1);
        return new NoDebugResourceReference(this);
    }

    @Override
    public String currentState() {
        return "Active references: " + refCount.get() + "."
                + " Resource reference debugging is turned off. Consider toggling the "
                + SharedResource.SYSTEM_PROPERTY_NAME_DEBUG
                + " system property to get debugging assistance with reference tracking.";
    }

    private void removeRef() {
        int refCount = addRef(-1);
        if (refCount == 0) {
            resource.close();
        }
    }

    private int addRef(int value) {
        while (true) {
            int prev = refCount.get();
            if (prev == 0) {
                throw new IllegalStateException(getClass().getName() + ".addRef(" + value + "):"
                        + " Object is already destroyed."
                        + " Consider toggling the " + SharedResource.SYSTEM_PROPERTY_NAME_DEBUG
                        + " system property to get debugging assistance with reference tracking.");
            }
            int next = prev + value;
            if (refCount.compareAndSet(prev, next)) {
                return next;
            }
        }
    }

    private static class NoDebugResourceReference extends CloseableOnce {
        private final ReferencesByCount resource;

        NoDebugResourceReference(final ReferencesByCount resource) {
            this.resource = resource;
        }

        @Override final void onClose() { resource.removeRef(); }
        @Override References getReferences() { return resource; }
    }
}