Skip to content

Package: Writer$Reentrant$1

Writer$Reentrant$1

nameinstructionbranchcomplexitylinemethod
initialValue()
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
{...}
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 2008, 2020 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Public License v. 2.0, which is available at
6: * http://www.eclipse.org/legal/epl-2.0.
7: *
8: * This Source Code may also be made available under the following Secondary
9: * Licenses when the conditions for such availability set forth in the
10: * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11: * version 2 with the GNU Classpath Exception, which is available at
12: * https://www.gnu.org/software/classpath/license.html.
13: *
14: * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15: */
16:
17: package org.glassfish.grizzly;
18:
19: import java.io.IOException;
20: import java.util.concurrent.Future;
21:
22: import org.glassfish.grizzly.asyncqueue.MessageCloner;
23: import org.glassfish.grizzly.asyncqueue.WritableMessage;
24:
25: /**
26: * Implementations of this interface are able to write data from a {@link Buffer} to {@link Connection}.
27: *
28: * There are two basic Writer implementations in Grizzly: {@link org.glassfish.grizzly.asyncqueue.AsyncQueueWriter},
29: * {@link org.glassfish.grizzly.nio.tmpselectors.TemporarySelectorWriter}.
30: *
31: * @param <L> the writer address type
32: *
33: * @author Alexey Stashok
34: */
35: @SuppressWarnings("deprecation")
36: public interface Writer<L> {
37: /**
38: * Method writes the {@link WritableMessage}.
39: *
40: *
41: * @param connection the {@link org.glassfish.grizzly.Connection} to write to
42: * @param message the {@link WritableMessage}, from which the data will be written
43: * @return {@link Future}, using which it's possible to check the result
44: * @throws java.io.IOException not thrown
45: */
46: GrizzlyFuture<WriteResult<WritableMessage, L>> write(Connection<L> connection, WritableMessage message) throws IOException;
47:
48: /**
49: * Method writes the {@link WritableMessage}.
50: *
51: *
52: * @param connection the {@link org.glassfish.grizzly.Connection} to write to
53: * @param message the {@link WritableMessage}, from which the data will be written
54: * @param completionHandler {@link org.glassfish.grizzly.CompletionHandler}, which will get notified, when write will be
55: * completed
56: */
57: void write(Connection<L> connection, WritableMessage message, CompletionHandler<WriteResult<WritableMessage, L>> completionHandler);
58:
59: /**
60: * Method writes the {@link WritableMessage} to the specific address.
61: *
62: *
63: * @param connection the {@link org.glassfish.grizzly.Connection} to write to
64: * @param dstAddress the destination address the {@link WritableMessage} will be sent to
65: * @param message the {@link WritableMessage}, from which the data will be written
66: * @return {@link Future}, using which it's possible to check the result
67: */
68: GrizzlyFuture<WriteResult<WritableMessage, L>> write(Connection<L> connection, L dstAddress, WritableMessage message);
69:
70: /**
71: * Method writes the {@link WritableMessage} to the specific address.
72: *
73: *
74: * @param connection the {@link org.glassfish.grizzly.Connection} to write to
75: * @param dstAddress the destination address the {@link WritableMessage} will be sent to
76: * @param message the {@link WritableMessage}, from which the data will be written
77: * @param completionHandler {@link org.glassfish.grizzly.CompletionHandler}, which will get notified, when write will be
78: * completed
79: */
80: void write(Connection<L> connection, L dstAddress, WritableMessage message, CompletionHandler<WriteResult<WritableMessage, L>> completionHandler);
81:
82: /**
83: * Method writes the {@link WritableMessage} to the specific address.
84: *
85: *
86: * @param connection the {@link org.glassfish.grizzly.Connection} to write to
87: * @param dstAddress the destination address the {@link WritableMessage} will be sent to
88: * @param message the {@link WritableMessage}, from which the data will be written
89: * @param completionHandler {@link org.glassfish.grizzly.CompletionHandler}, which will get notified, when write will be
90: * completed
91: * @param pushBackHandler {@link org.glassfish.grizzly.asyncqueue.PushBackHandler}, which will be notified if message
92: * was accepted by transport write queue or refused
93: * @deprecated push back logic is deprecated
94: */
95: @Deprecated
96: void write(Connection<L> connection, L dstAddress, WritableMessage message, CompletionHandler<WriteResult<WritableMessage, L>> completionHandler,
97: org.glassfish.grizzly.asyncqueue.PushBackHandler pushBackHandler);
98:
99: /**
100: * Method writes the {@link WritableMessage} to the specific address.
101: *
102: *
103: * @param connection the {@link org.glassfish.grizzly.Connection} to write to
104: * @param dstAddress the destination address the {@link WritableMessage} will be sent to
105: * @param message the {@link WritableMessage}, from which the data will be written
106: * @param completionHandler {@link org.glassfish.grizzly.CompletionHandler}, which will get notified, when write will be
107: * completed
108: * @param messageCloner the {@link MessageCloner}, which will be able to clone the message in case it can't be
109: * completely written in the current thread.
110: */
111: void write(Connection<L> connection, L dstAddress, WritableMessage message, CompletionHandler<WriteResult<WritableMessage, L>> completionHandler,
112: MessageCloner<WritableMessage> messageCloner);
113:
114: /**
115: * Return <code>true</code> if the connection has not exceeded it's maximum size in bytes of pending writes, otherwise
116: * <code>false</code>.
117: *
118: * @param connection the {@link Connection} to test whether or not the specified number of bytes can be written to.
119: * @return <code>true</code> if the connection has not exceeded it's maximum size in bytes of pending writes, otherwise
120: * <code>false</code>
121: *
122: * @since 2.3
123: */
124: boolean canWrite(final Connection<L> connection);
125:
126: /**
127: * Registers {@link WriteHandler}, which will be notified ones at least one byte can be written.
128: *
129: * This method call is equivalent to call notifyWritePossible(connection, writeHandler, <tt>1</tt>);
130: *
131: * Note: using this method from different threads simultaneously may lead to quick situation changes, so at time
132: * {@link WriteHandler} is called - the queue may become busy again.
133: *
134: * @param connection {@link Connection}
135: * @param writeHandler {@link WriteHandler} to be notified.
136: *
137: * @since 2.3
138: */
139: void notifyWritePossible(final Connection<L> connection, final WriteHandler writeHandler);
140:
141: /**
142: * Write reentrants counter
143: */
144: final class Reentrant {
145: private static final ThreadLocal<Reentrant> REENTRANTS_COUNTER = new ThreadLocal<Reentrant>() {
146:
147: @Override
148: protected Reentrant initialValue() {
149: return new Reentrant();
150: }
151: };
152:
153: private static final int maxWriteReentrants = Integer.getInteger("org.glassfish.grizzly.Writer.max-write-reentrants", 10);
154:
155: /**
156: * Returns the maximum number of write() method reentrants a thread is allowed to made. This is related to possible
157: * write()->onComplete()->write()->... chain, which may grow infinitely and cause StackOverflow. Using
158: * maxWriteReentrants value it's possible to limit such a chain.
159: *
160: * @return the maximum number of write() method reentrants a thread is allowed to make.
161: */
162: public static int getMaxReentrants() {
163: return maxWriteReentrants;
164: }
165:
166: /**
167: * Returns the current write reentrants counter. Might be useful, if developer wants to use custom notification
168: * mechanism, based on on {@link #canWrite(org.glassfish.grizzly.Connection)} and various write methods.
169: *
170: * @return current reentrants counter
171: */
172: public static Reentrant getWriteReentrant() {
173: // ThreadLocal otherwise
174: return REENTRANTS_COUNTER.get();
175: }
176:
177: private int counter;
178:
179: /**
180: * Returns the value of the reentrants counter for the current thread.
181: */
182: public int get() {
183: return counter;
184: }
185:
186: /**
187: * Increments the reentrants counter by one.
188: *
189: * @return <tt>true</tt> if the counter (after incrementing) didn't reach {@link #getMaxReentrants()} limit, or
190: * <tt>false</tt> otherwise.
191: */
192: public boolean inc() {
193: return ++counter <= maxWriteReentrants;
194: }
195:
196: /**
197: * Decrements the reentrants counter by one.
198: *
199: * @return <tt>true</tt> if the counter (after decrementing) didn't reach {@link #getMaxReentrants()} limit, or
200: * <tt>false</tt> otherwise.
201: */
202: public boolean dec() {
203: return --counter <= maxWriteReentrants;
204: }
205:
206: /**
207: * Returns <tt>true</tt>, if max number of write->completion-handler reentrants has been reached for the passed
208: * {@link Reentrant} object, and next write will happen in the separate thread.
209: *
210: * @return <tt>true</tt>, if max number of write->completion-handler reentrants has been reached for the passed
211: * {@link Reentrant} object, and next write will happen in the separate thread.
212: */
213: public boolean isMaxReentrantsReached() {
214: return get() >= getMaxReentrants();
215: }
216: }
217: }