Checkstyle / Overall clean-up / Fix serialization

This commit is contained in:
Trustin Lee 2014-06-21 17:46:38 +09:00
parent 0925bc3e79
commit a2400d9eb2
5 changed files with 163 additions and 56 deletions

View File

@ -18,63 +18,15 @@
*/
package io.netty.util.internal;
import static java.util.concurrent.atomic.AtomicReferenceFieldUpdater.newUpdater;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Queue;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
abstract class MpscLinkedQueuePad0<E> {
long p00, p01, p02, p03, p04, p05, p06, p07;
long p30, p31, p32, p33, p34, p35, p36, p37;
}
abstract class MpscLinkedQueueHeadRef<E> extends MpscLinkedQueuePad0<E> {
@SuppressWarnings("rawtypes")
private static final AtomicReferenceFieldUpdater<MpscLinkedQueueHeadRef, MpscLinkedQueueNode> UPDATER =
newUpdater(MpscLinkedQueueHeadRef.class, MpscLinkedQueueNode.class, "headRef");
private volatile MpscLinkedQueueNode<E> headRef;
protected final MpscLinkedQueueNode<E> headRef() {
return headRef;
}
protected final void headRef(MpscLinkedQueueNode<E> val) {
headRef = val;
}
protected final void lazySetHeadRef(MpscLinkedQueueNode<E> newVal) {
UPDATER.lazySet(this, newVal);
}
}
abstract class MpscLinkedQueuePad1<E> extends MpscLinkedQueueHeadRef<E> {
long p00, p01, p02, p03, p04, p05, p06, p07;
long p30, p31, p32, p33, p34, p35, p36, p37;
}
abstract class MpscLinkedQueueTailRef<E> extends MpscLinkedQueuePad1<E> {
@SuppressWarnings("rawtypes")
private static final AtomicReferenceFieldUpdater<MpscLinkedQueueTailRef, MpscLinkedQueueNode> UPDATER =
newUpdater(MpscLinkedQueueTailRef.class, MpscLinkedQueueNode.class, "tailRef");
private volatile MpscLinkedQueueNode<E> tailRef;
protected final MpscLinkedQueueNode<E> tailRef() {
return tailRef;
}
protected final void tailRef(MpscLinkedQueueNode<E> val) {
tailRef = val;
}
@SuppressWarnings("unchecked")
protected final MpscLinkedQueueNode<E> getAndSetTailRef(MpscLinkedQueueNode<E> newVal) {
return (MpscLinkedQueueNode<E>) UPDATER.getAndSet(this, newVal);
}
}
/**
* A lock-free concurrent single-consumer multi-producer {@link Queue}.
@ -113,7 +65,10 @@ abstract class MpscLinkedQueueTailRef<E> extends MpscLinkedQueuePad1<E> {
* data structure modified to avoid false sharing between head and tail Ref as per implementation of MpscLinkedQueue
* on <a href="https://github.com/JCTools/JCTools">JCTools project</a>.
*/
public final class MpscLinkedQueue<E> extends MpscLinkedQueueTailRef<E> implements Queue<E> {
final class MpscLinkedQueue<E> extends MpscLinkedQueueTailRef<E> implements Queue<E> {
private static final long serialVersionUID = -1878402552271506449L;
// offer() occurs at the tail of the linked list.
// poll() occurs at the head of the linked list.
//
@ -131,8 +86,8 @@ public final class MpscLinkedQueue<E> extends MpscLinkedQueueTailRef<E> implemen
// since Unsafe does not expose XCHG operation intrinsically.
MpscLinkedQueue() {
MpscLinkedQueueNode<E> tombstone = new DefaultNode<E>(null);
headRef(tombstone);
tailRef(tombstone);
setHeadRef(tombstone);
setTailRef(tombstone);
}
/**
@ -403,8 +358,8 @@ public final class MpscLinkedQueue<E> extends MpscLinkedQueueTailRef<E> implemen
in.defaultReadObject();
final MpscLinkedQueueNode<E> tombstone = new DefaultNode<E>(null);
headRef(tombstone);
tailRef(tombstone);
setHeadRef(tombstone);
setTailRef(tombstone);
for (;;) {
@SuppressWarnings("unchecked")
@ -416,9 +371,7 @@ public final class MpscLinkedQueue<E> extends MpscLinkedQueueTailRef<E> implemen
}
}
private static final class DefaultNode<T> extends MpscLinkedQueueNode<T> implements Serializable {
private static final long serialVersionUID = 1006745279405945948L;
private static final class DefaultNode<T> extends MpscLinkedQueueNode<T> {
private T value;

View File

@ -0,0 +1,54 @@
/*
* Copyright 2014 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.
*/
package io.netty.util.internal;
import java.io.Serializable;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
abstract class MpscLinkedQueueHeadRef<E> extends MpscLinkedQueuePad0<E> implements Serializable {
private static final long serialVersionUID = 8467054865577874285L;
@SuppressWarnings("rawtypes")
private static final AtomicReferenceFieldUpdater<MpscLinkedQueueHeadRef, MpscLinkedQueueNode> UPDATER;
static {
@SuppressWarnings("rawtypes")
AtomicReferenceFieldUpdater<MpscLinkedQueueHeadRef, MpscLinkedQueueNode> updater;
updater = PlatformDependent.newAtomicReferenceFieldUpdater(MpscLinkedQueueHeadRef.class, "headRef");
if (updater == null) {
updater = AtomicReferenceFieldUpdater.newUpdater(
MpscLinkedQueueHeadRef.class, MpscLinkedQueueNode.class, "headRef");
}
UPDATER = updater;
}
private transient volatile MpscLinkedQueueNode<E> headRef;
protected final MpscLinkedQueueNode<E> headRef() {
return headRef;
}
protected final void setHeadRef(MpscLinkedQueueNode<E> headRef) {
this.headRef = headRef;
}
protected final void lazySetHeadRef(MpscLinkedQueueNode<E> headRef) {
UPDATER.lazySet(this, headRef);
}
}

View File

@ -0,0 +1,22 @@
/*
* Copyright 2014 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.
*/
package io.netty.util.internal;
abstract class MpscLinkedQueuePad0<E> {
long p00, p01, p02, p03, p04, p05, p06, p07;
long p30, p31, p32, p33, p34, p35, p36, p37;
}

View File

@ -0,0 +1,25 @@
/*
* Copyright 2014 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.
*/
package io.netty.util.internal;
abstract class MpscLinkedQueuePad1<E> extends MpscLinkedQueueHeadRef<E> {
private static final long serialVersionUID = 2886694927079691637L;
long p00, p01, p02, p03, p04, p05, p06, p07;
long p30, p31, p32, p33, p34, p35, p36, p37;
}

View File

@ -0,0 +1,53 @@
/*
* Copyright 2014 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.
*/
package io.netty.util.internal;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
abstract class MpscLinkedQueueTailRef<E> extends MpscLinkedQueuePad1<E> {
private static final long serialVersionUID = 8717072462993327429L;
@SuppressWarnings("rawtypes")
private static final AtomicReferenceFieldUpdater<MpscLinkedQueueTailRef, MpscLinkedQueueNode> UPDATER;
static {
@SuppressWarnings("rawtypes")
AtomicReferenceFieldUpdater<MpscLinkedQueueTailRef, MpscLinkedQueueNode> updater;
updater = PlatformDependent.newAtomicReferenceFieldUpdater(MpscLinkedQueueTailRef.class, "tailRef");
if (updater == null) {
updater = AtomicReferenceFieldUpdater.newUpdater(
MpscLinkedQueueTailRef.class, MpscLinkedQueueNode.class, "tailRef");
}
UPDATER = updater;
}
private transient volatile MpscLinkedQueueNode<E> tailRef;
protected final MpscLinkedQueueNode<E> tailRef() {
return tailRef;
}
protected final void setTailRef(MpscLinkedQueueNode<E> tailRef) {
this.tailRef = tailRef;
}
@SuppressWarnings("unchecked")
protected final MpscLinkedQueueNode<E> getAndSetTailRef(MpscLinkedQueueNode<E> tailRef) {
return (MpscLinkedQueueNode<E>) UPDATER.getAndSet(this, tailRef);
}
}