2012-06-04 22:31:44 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2012 The Netty Project
|
|
|
|
*
|
|
|
|
* The Netty Project 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.
|
|
|
|
*/
|
2012-04-29 16:08:59 +02:00
|
|
|
package io.netty.util;
|
|
|
|
|
2014-02-04 10:37:40 +01:00
|
|
|
import io.netty.util.internal.PlatformDependent;
|
|
|
|
|
2012-04-29 16:08:59 +02:00
|
|
|
import java.util.concurrent.atomic.AtomicReference;
|
2014-05-13 14:23:23 +02:00
|
|
|
import java.util.concurrent.atomic.AtomicReferenceArray;
|
2013-02-22 00:49:51 +01:00
|
|
|
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
|
2012-04-29 16:08:59 +02:00
|
|
|
|
2012-12-21 08:16:00 +01:00
|
|
|
/**
|
2014-05-13 14:23:23 +02:00
|
|
|
* Default {@link AttributeMap} implementation which use simple synchronization per bucket to keep the memory overhead
|
2012-12-21 08:16:00 +01:00
|
|
|
* as low as possible.
|
|
|
|
*/
|
2012-04-29 16:08:59 +02:00
|
|
|
public class DefaultAttributeMap implements AttributeMap {
|
|
|
|
|
2013-02-22 00:49:51 +01:00
|
|
|
@SuppressWarnings("rawtypes")
|
2014-05-13 14:23:23 +02:00
|
|
|
private static final AtomicReferenceFieldUpdater<DefaultAttributeMap, AtomicReferenceArray> updater;
|
2014-02-04 10:37:40 +01:00
|
|
|
|
|
|
|
static {
|
|
|
|
@SuppressWarnings("rawtypes")
|
2014-05-13 14:23:23 +02:00
|
|
|
AtomicReferenceFieldUpdater<DefaultAttributeMap, AtomicReferenceArray> referenceFieldUpdater =
|
|
|
|
PlatformDependent.newAtomicReferenceFieldUpdater(DefaultAttributeMap.class, "attributes");
|
2014-02-04 10:37:40 +01:00
|
|
|
if (referenceFieldUpdater == null) {
|
2014-05-13 14:23:23 +02:00
|
|
|
referenceFieldUpdater = AtomicReferenceFieldUpdater
|
|
|
|
.newUpdater(DefaultAttributeMap.class, AtomicReferenceArray.class, "attributes");
|
2014-02-04 10:37:40 +01:00
|
|
|
}
|
|
|
|
updater = referenceFieldUpdater;
|
|
|
|
}
|
2013-02-22 00:49:51 +01:00
|
|
|
|
2014-05-13 14:23:23 +02:00
|
|
|
private static final int BUCKET_SIZE = 4;
|
|
|
|
private static final int MASK = BUCKET_SIZE - 1;
|
|
|
|
|
2013-02-22 00:49:51 +01:00
|
|
|
// Initialize lazily to reduce memory consumption; updated by AtomicReferenceFieldUpdater above.
|
|
|
|
@SuppressWarnings("UnusedDeclaration")
|
2014-05-13 14:23:23 +02:00
|
|
|
private volatile AtomicReferenceArray<DefaultAttribute<?>> attributes;
|
2012-04-29 16:08:59 +02:00
|
|
|
|
2014-05-13 14:23:23 +02:00
|
|
|
@SuppressWarnings({ "unchecked", "rawtypes" })
|
2012-04-29 16:08:59 +02:00
|
|
|
@Override
|
2013-02-22 00:49:51 +01:00
|
|
|
public <T> Attribute<T> attr(AttributeKey<T> key) {
|
2014-05-13 14:23:23 +02:00
|
|
|
if (key == null) {
|
|
|
|
throw new NullPointerException("key");
|
|
|
|
}
|
|
|
|
AtomicReferenceArray<DefaultAttribute<?>> attributes = this.attributes;
|
|
|
|
if (attributes == null) {
|
2012-04-29 16:08:59 +02:00
|
|
|
// Not using ConcurrentHashMap due to high memory consumption.
|
2014-05-13 14:23:23 +02:00
|
|
|
attributes = new AtomicReferenceArray<DefaultAttribute<?>>(BUCKET_SIZE);
|
|
|
|
|
|
|
|
if (!updater.compareAndSet(this, null, attributes)) {
|
|
|
|
attributes = this.attributes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int i = index(key);
|
|
|
|
DefaultAttribute<?> head = attributes.get(i);
|
|
|
|
if (head == null) {
|
|
|
|
// No head exists yet which means we may be able to add the attribute without synchronization and just
|
|
|
|
// use compare and set. At worst we need to fallback to synchronization
|
|
|
|
head = new DefaultAttribute(key);
|
|
|
|
if (attributes.compareAndSet(i, null, head)) {
|
|
|
|
// we were able to add it so return the head right away
|
|
|
|
return (Attribute<T>) head;
|
|
|
|
} else {
|
|
|
|
head = attributes.get(i);
|
2013-02-22 00:49:51 +01:00
|
|
|
}
|
2012-04-29 16:08:59 +02:00
|
|
|
}
|
|
|
|
|
2014-05-13 14:23:23 +02:00
|
|
|
synchronized (head) {
|
|
|
|
DefaultAttribute<?> curr = head;
|
|
|
|
for (;;) {
|
|
|
|
if (!curr.removed && curr.key == key) {
|
|
|
|
return (Attribute<T>) curr;
|
|
|
|
}
|
|
|
|
|
|
|
|
DefaultAttribute<?> next = curr.next;
|
|
|
|
if (next == null) {
|
|
|
|
DefaultAttribute<T> attr = new DefaultAttribute<T>(head, key);
|
|
|
|
curr.next = attr;
|
|
|
|
attr.prev = curr;
|
2014-06-01 13:06:50 +02:00
|
|
|
return attr;
|
|
|
|
} else {
|
|
|
|
curr = next;
|
2014-05-13 14:23:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public <T> boolean hasAttr(AttributeKey<T> key) {
|
|
|
|
if (key == null) {
|
|
|
|
throw new NullPointerException("key");
|
|
|
|
}
|
|
|
|
AtomicReferenceArray<DefaultAttribute<?>> attributes = this.attributes;
|
|
|
|
if (attributes == null) {
|
|
|
|
// no attribute exists
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int i = index(key);
|
|
|
|
DefaultAttribute<?> head = attributes.get(i);
|
|
|
|
if (head == null) {
|
|
|
|
// No attribute exists which point to the bucket in which the head should be located
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check on the head can be done without synchronization
|
|
|
|
if (head.key == key && !head.removed) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
synchronized (head) {
|
|
|
|
// we need to synchronize on the head
|
|
|
|
DefaultAttribute<?> curr = head.next;
|
|
|
|
while (curr != null) {
|
|
|
|
if (!curr.removed && curr.key == key) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
curr = curr.next;
|
2013-02-22 00:49:51 +01:00
|
|
|
}
|
2014-05-13 14:23:23 +02:00
|
|
|
return false;
|
2012-04-29 16:08:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-13 14:23:23 +02:00
|
|
|
private static int index(AttributeKey<?> key) {
|
|
|
|
return key.id() & MASK;
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("serial")
|
2013-02-22 00:49:51 +01:00
|
|
|
private static final class DefaultAttribute<T> extends AtomicReference<T> implements Attribute<T> {
|
2012-04-29 16:08:59 +02:00
|
|
|
|
|
|
|
private static final long serialVersionUID = -2661411462200283011L;
|
|
|
|
|
2014-05-13 14:23:23 +02:00
|
|
|
// The head of the linked-list this attribute belongs to, which may be itself
|
|
|
|
private final DefaultAttribute<?> head;
|
2013-02-22 00:49:51 +01:00
|
|
|
private final AttributeKey<T> key;
|
|
|
|
|
2014-05-13 14:23:23 +02:00
|
|
|
// Double-linked list to prev and next node to allow fast removal
|
|
|
|
private DefaultAttribute<?> prev;
|
|
|
|
private DefaultAttribute<?> next;
|
|
|
|
|
|
|
|
// Will be set to true one the attribute is removed via getAndRemove() or remove()
|
|
|
|
private volatile boolean removed;
|
|
|
|
|
|
|
|
DefaultAttribute(DefaultAttribute<?> head, AttributeKey<T> key) {
|
|
|
|
this.head = head;
|
|
|
|
this.key = key;
|
|
|
|
}
|
|
|
|
|
|
|
|
DefaultAttribute(AttributeKey<T> key) {
|
|
|
|
head = this;
|
2013-02-22 00:49:51 +01:00
|
|
|
this.key = key;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public AttributeKey<T> key() {
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
2012-04-29 16:08:59 +02:00
|
|
|
@Override
|
|
|
|
public T setIfAbsent(T value) {
|
2013-01-31 11:49:52 +01:00
|
|
|
while (!compareAndSet(null, value)) {
|
|
|
|
T old = get();
|
|
|
|
if (old != null) {
|
|
|
|
return old;
|
|
|
|
}
|
2012-04-29 16:08:59 +02:00
|
|
|
}
|
2013-01-31 11:49:52 +01:00
|
|
|
return null;
|
2012-04-29 16:08:59 +02:00
|
|
|
}
|
|
|
|
|
2013-02-22 00:49:51 +01:00
|
|
|
@Override
|
|
|
|
public T getAndRemove() {
|
2014-05-13 14:23:23 +02:00
|
|
|
removed = true;
|
2013-02-22 00:49:51 +01:00
|
|
|
T oldValue = getAndSet(null);
|
|
|
|
remove0();
|
|
|
|
return oldValue;
|
|
|
|
}
|
|
|
|
|
2012-04-29 16:08:59 +02:00
|
|
|
@Override
|
|
|
|
public void remove() {
|
2014-05-13 14:23:23 +02:00
|
|
|
removed = true;
|
2013-02-22 00:49:51 +01:00
|
|
|
set(null);
|
|
|
|
remove0();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void remove0() {
|
2014-05-13 14:23:23 +02:00
|
|
|
synchronized (head) {
|
|
|
|
// We only update the linked-list structure if prev != null because if it is null this
|
|
|
|
// DefaultAttribute acts also as head. The head must never be removed completely and just be
|
|
|
|
// marked as removed as all synchronization is done on the head itself for each bucket.
|
|
|
|
// The head itself will be GC'ed once the DefaultAttributeMap is GC'ed. So at most 5 heads will
|
|
|
|
// be removed lazy as the array size is 5.
|
|
|
|
if (prev != null) {
|
|
|
|
prev.next = next;
|
|
|
|
|
|
|
|
if (next != null) {
|
|
|
|
next.prev = prev;
|
|
|
|
}
|
|
|
|
}
|
2013-02-21 19:38:23 +01:00
|
|
|
}
|
2012-04-29 16:08:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|