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
84
85
/*
 * %W% %E%
 *
 * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package sun.misc;

/**
 * Atomic access to a long value which may be read and updated by multiple
 * threads without explicit sychronization.
 *
 * Depending on the capability of the hardware we are running on, this class
 * may be implemented using Java-level locks, an 8-byte compare-and-swap or a
 * load-reserved/store-conditional pair.
 *
 * written by: Doug Lea  (dl@cs.oswego.edu)
 */

abstract public class AtomicLong { 

    private static native boolean VMSupportsCS8();

    /** 
     * Factory method to create instance of underlying implementation.
     *
     */
    public static AtomicLong newAtomicLong(long initialValue) {
        if (VMSupportsCS8())
            return new AtomicLongCSImpl(initialValue);
        else
            return new AtomicLongLockImpl(initialValue);
    }

    /** 
     * Get the current value, with VOLATILE-ACQUIRE memory semantics.
     */
    public abstract long get();

    /** 
     * Attempt to set the value to newValue only if it is currently oldValue.
     * This will always fail if another thread changes the value
     * to a different value between commencement and completion
     * of the attempted update, and MAY succeed otherwise. 
     * If committed, the operation has VOLATILE-RELEASE semantics.
     *
     * Note:
     * This can be implemented using CAS and with locks in the
     * obvious way. It can be implemented using LL/SC via
     *    if (LoadLLinked(&value) == oldValue)
     *       StoreConditional(&value, newValue)
     */
    public abstract boolean attemptUpdate(long oldValue, long newValue);

    /** 
     * Behaviorally equivalent to:
     * <pre>
     * attemptUpdate(get(), newValue);
     * </pre>
     */
    public abstract boolean attemptSet(long newValue);

    /** 
     * Behaviorally equivalent to:
     * <pre>
     * long v = get();
     * attemptUpdate(v, v+1);
     * </pre>
     *
     * Rationale: Automates and speeds up a very common usage.
     **/
    public abstract boolean attemptIncrememt();

    /** 
     * Behaviorally equivalent to:
     * <pre>
     * long v = get();
     * attemptUpdate(v, v+k);
     * </pre>
     *
     **/
    public abstract boolean attemptAdd(long k);

}
			
			

Browsed Source: [clear]