-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
PlatformAtomicReference.java
150 lines (122 loc) · 4.8 KB
/
PlatformAtomicReference.java
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ignite.internal.processors.platform.datastructures;
import org.apache.ignite.IgniteCheckedException;
import org.apache.ignite.internal.binary.BinaryRawReaderEx;
import org.apache.ignite.internal.binary.BinaryRawWriterEx;
import org.apache.ignite.internal.processors.datastructures.GridCacheAtomicReferenceImpl;
import org.apache.ignite.internal.processors.platform.PlatformAbstractTarget;
import org.apache.ignite.internal.processors.platform.PlatformContext;
import org.apache.ignite.internal.processors.platform.memory.PlatformMemory;
/**
* Platform atomic reference wrapper.
*/
@SuppressWarnings("unchecked")
public class PlatformAtomicReference extends PlatformAbstractTarget {
/** */
private static final int OP_GET = 1;
/** */
private static final int OP_SET = 2;
/** */
private static final int OP_COMPARE_AND_SET_AND_GET = 3;
/** */
private static final int OP_CLOSE = 4;
/** */
private static final int OP_IS_CLOSED = 5;
/** */
private final GridCacheAtomicReferenceImpl atomicRef;
/**
* Creates an instance or returns null.
*
* @param ctx Context.
* @param name Name.
* @param memPtr Pointer to a stream with initial value. 0 for default value.
* @param create Create flag.
* @return Instance of a PlatformAtomicReference, or null when Ignite reference with specific name is null.
*/
public static PlatformAtomicReference createInstance(PlatformContext ctx, String name, long memPtr,
boolean create) {
assert ctx != null;
assert name != null;
Object initVal = null;
if (memPtr != 0) {
try (PlatformMemory mem = ctx.memory().get(memPtr)) {
initVal = ctx.reader(mem).readObjectDetached();
}
}
GridCacheAtomicReferenceImpl atomicRef =
(GridCacheAtomicReferenceImpl)ctx.kernalContext().grid().atomicReference(name, initVal, create);
if (atomicRef == null)
return null;
return new PlatformAtomicReference(ctx, atomicRef);
}
/**
* Ctor.
*
* @param ctx Context.
* @param ref Atomic reference to wrap.
*/
private PlatformAtomicReference(PlatformContext ctx, GridCacheAtomicReferenceImpl ref) {
super(ctx);
assert ref != null;
atomicRef = ref;
}
/** {@inheritDoc} */
@Override protected void processOutStream(int type, BinaryRawWriterEx writer) throws IgniteCheckedException {
if (type == OP_GET)
writer.writeObject(atomicRef.get());
else
super.processOutStream(type, writer);
}
/** {@inheritDoc} */
@Override protected long processInStreamOutLong(int type, BinaryRawReaderEx reader)
throws IgniteCheckedException {
if (type == OP_SET) {
atomicRef.set(reader.readObjectDetached());
return 0;
}
return super.processInStreamOutLong(type, reader);
}
/** {@inheritDoc} */
@Override protected void processInStreamOutStream(int type, BinaryRawReaderEx reader,
BinaryRawWriterEx writer) throws IgniteCheckedException {
if (type == OP_COMPARE_AND_SET_AND_GET) {
Object val = reader.readObjectDetached();
final Object cmp = reader.readObjectDetached();
Object res = atomicRef.compareAndSetAndGet(val, cmp);
if (cmp == res)
writer.writeBoolean(true);
else {
writer.writeBoolean(false);
writer.writeObject(res);
}
}
else
super.processInStreamOutStream(type, reader, writer);
}
/** {@inheritDoc} */
@Override protected long processInLongOutLong(int type, long val) throws IgniteCheckedException {
switch (type) {
case OP_CLOSE:
atomicRef.close();
return TRUE;
case OP_IS_CLOSED:
return atomicRef.removed() ? TRUE : FALSE;
}
return super.processInLongOutLong(type, val);
}
}