2013-06-14 07:07:33 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2013 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;
|
|
|
|
|
2013-12-06 07:12:46 +01:00
|
|
|
import io.netty.util.internal.StringUtil;
|
|
|
|
import io.netty.util.internal.logging.InternalLogger;
|
|
|
|
import io.netty.util.internal.logging.InternalLoggerFactory;
|
|
|
|
|
2013-06-14 07:07:33 +02:00
|
|
|
/**
|
|
|
|
* Collection of method to handle objects that may implement {@link ReferenceCounted}.
|
|
|
|
*/
|
|
|
|
public final class ReferenceCountUtil {
|
|
|
|
|
2013-12-06 07:12:46 +01:00
|
|
|
private static final InternalLogger logger = InternalLoggerFactory.getInstance(ReferenceCountUtil.class);
|
|
|
|
|
2013-06-14 07:07:33 +02:00
|
|
|
/**
|
|
|
|
* Try to call {@link ReferenceCounted#retain()} if the specified message implements {@link ReferenceCounted}.
|
|
|
|
* If the specified message doesn't implement {@link ReferenceCounted}, this method does nothing.
|
|
|
|
*/
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public static <T> T retain(T msg) {
|
|
|
|
if (msg instanceof ReferenceCounted) {
|
|
|
|
return (T) ((ReferenceCounted) msg).retain();
|
|
|
|
}
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Try to call {@link ReferenceCounted#retain()} if the specified message implements {@link ReferenceCounted}.
|
|
|
|
* If the specified message doesn't implement {@link ReferenceCounted}, this method does nothing.
|
|
|
|
*/
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public static <T> T retain(T msg, int increment) {
|
|
|
|
if (msg instanceof ReferenceCounted) {
|
|
|
|
return (T) ((ReferenceCounted) msg).retain(increment);
|
|
|
|
}
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2014-01-28 12:00:28 +01:00
|
|
|
/**
|
|
|
|
* Tries to call {@link ReferenceCounted#touch()} if the specified message implements {@link ReferenceCounted}.
|
|
|
|
* If the specified message doesn't implement {@link ReferenceCounted}, this method does nothing.
|
|
|
|
*/
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public static <T> T touch(T msg) {
|
|
|
|
if (msg instanceof ReferenceCounted) {
|
|
|
|
return (T) ((ReferenceCounted) msg).touch();
|
|
|
|
}
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2014-01-29 03:44:59 +01:00
|
|
|
/**
|
|
|
|
* Tries to call {@link ReferenceCounted#touch(Object)} if the specified message implements
|
|
|
|
* {@link ReferenceCounted}. If the specified message doesn't implement {@link ReferenceCounted},
|
|
|
|
* this method does nothing.
|
|
|
|
*/
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public static <T> T touch(T msg, Object hint) {
|
|
|
|
if (msg instanceof ReferenceCounted) {
|
|
|
|
return (T) ((ReferenceCounted) msg).touch(hint);
|
|
|
|
}
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2013-06-14 07:07:33 +02:00
|
|
|
/**
|
|
|
|
* Try to call {@link ReferenceCounted#release()} if the specified message implements {@link ReferenceCounted}.
|
|
|
|
* If the specified message doesn't implement {@link ReferenceCounted}, this method does nothing.
|
|
|
|
*/
|
|
|
|
public static boolean release(Object msg) {
|
|
|
|
if (msg instanceof ReferenceCounted) {
|
|
|
|
return ((ReferenceCounted) msg).release();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Try to call {@link ReferenceCounted#release()} if the specified message implements {@link ReferenceCounted}.
|
|
|
|
* If the specified message doesn't implement {@link ReferenceCounted}, this method does nothing.
|
|
|
|
*/
|
|
|
|
public static boolean release(Object msg, int decrement) {
|
|
|
|
if (msg instanceof ReferenceCounted) {
|
|
|
|
return ((ReferenceCounted) msg).release(decrement);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-12-06 07:12:46 +01:00
|
|
|
/**
|
|
|
|
* Schedules the specified object to be released when the caller thread terminates. Note that this operation is
|
|
|
|
* intended to simplify reference counting of ephemeral objects during unit tests. Do not use it beyond the
|
|
|
|
* intended use case.
|
|
|
|
*/
|
|
|
|
public static <T> T releaseLater(T msg) {
|
|
|
|
return releaseLater(msg, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Schedules the specified object to be released when the caller thread terminates. Note that this operation is
|
|
|
|
* intended to simplify reference counting of ephemeral objects during unit tests. Do not use it beyond the
|
|
|
|
* intended use case.
|
|
|
|
*/
|
|
|
|
public static <T> T releaseLater(T msg, int decrement) {
|
|
|
|
if (msg instanceof ReferenceCounted) {
|
2014-05-30 11:43:40 +02:00
|
|
|
ThreadDeathWatcher.watch(Thread.currentThread(), new ReleasingTask((ReferenceCounted) msg, decrement));
|
2013-12-06 07:12:46 +01:00
|
|
|
}
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Releases the objects when the thread that called {@link #releaseLater(Object)} has been terminated.
|
|
|
|
*/
|
|
|
|
private static final class ReleasingTask implements Runnable {
|
|
|
|
|
2014-05-30 11:43:40 +02:00
|
|
|
private final ReferenceCounted obj;
|
|
|
|
private final int decrement;
|
2013-12-06 07:12:46 +01:00
|
|
|
|
2014-05-30 11:43:40 +02:00
|
|
|
ReleasingTask(ReferenceCounted obj, int decrement) {
|
|
|
|
this.obj = obj;
|
|
|
|
this.decrement = decrement;
|
|
|
|
}
|
2013-12-06 07:12:46 +01:00
|
|
|
|
2014-05-30 11:43:40 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
if (!obj.release(decrement)) {
|
|
|
|
logger.warn("Non-zero refCnt: {}", this);
|
|
|
|
} else {
|
|
|
|
logger.debug("Released: {}", this);
|
2013-12-06 07:12:46 +01:00
|
|
|
}
|
2014-05-30 11:43:40 +02:00
|
|
|
} catch (Exception ex) {
|
|
|
|
logger.warn("Failed to release an object: {}", obj, ex);
|
2013-12-06 07:12:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-30 11:43:40 +02:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return StringUtil.simpleClassName(obj) + ".release(" + decrement + ") refCnt: " + obj.refCnt();
|
2013-12-06 07:12:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-14 07:07:33 +02:00
|
|
|
private ReferenceCountUtil() { }
|
|
|
|
}
|