| 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 | |
package com.sun.grizzly.util; |
| 39 | |
|
| 40 | |
import com.sun.grizzly.Controller; |
| 41 | |
import com.sun.grizzly.Pipeline; |
| 42 | |
import java.util.concurrent.Callable; |
| 43 | |
import java.util.logging.Level; |
| 44 | |
import com.sun.grizzly.util.ByteBufferFactory.ByteBufferType; |
| 45 | |
import com.sun.grizzly.util.ThreadAttachment.Mode; |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
public class WorkerThreadImpl extends WorkerThread { |
| 54 | |
|
| 55 | |
private static final int DEFAULT_BYTE_BUFFER_SIZE = 8192; |
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
protected Runnable target; |
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
protected Pipeline<Callable> pipeline; |
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | 352 | protected volatile boolean execute = true; |
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | 1 | protected final static ThreadGroup threadGroup = new ThreadGroup("Grizzly"); |
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
private ThreadAttachment threadAttachment; |
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | 352 | private ByteBufferType byteBufferType = ByteBufferType.HEAP_VIEW; |
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
private int initialByteBufferSize; |
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
public WorkerThreadImpl(ThreadGroup threadGroup, Runnable runnable){ |
| 106 | 0 | this(threadGroup, runnable, DEFAULT_BYTE_BUFFER_SIZE); |
| 107 | 0 | } |
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
public WorkerThreadImpl(ThreadGroup threadGroup, Runnable runnable, |
| 117 | |
int initialByteBufferSize){ |
| 118 | 0 | super(threadGroup, runnable); |
| 119 | 0 | setDaemon(true); |
| 120 | 0 | target = runnable; |
| 121 | 0 | this.initialByteBufferSize = initialByteBufferSize; |
| 122 | 0 | } |
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
public WorkerThreadImpl(Pipeline<Callable> pipeline, String name){ |
| 131 | 0 | this(pipeline, name, DEFAULT_BYTE_BUFFER_SIZE); |
| 132 | 0 | } |
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
public WorkerThreadImpl(Pipeline<Callable> pipeline, String name, |
| 142 | |
int initialByteBufferSize){ |
| 143 | 352 | super(threadGroup, name); |
| 144 | 352 | this.pipeline = pipeline; |
| 145 | 352 | setDaemon(true); |
| 146 | 352 | this.initialByteBufferSize = initialByteBufferSize; |
| 147 | 352 | } |
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
public WorkerThreadImpl(Pipeline<Callable> pipeline, String name, |
| 157 | |
Runnable runnable, int initialByteBufferSize){ |
| 158 | 0 | super(threadGroup, runnable, name); |
| 159 | 0 | target = runnable; |
| 160 | 0 | this.pipeline = pipeline; |
| 161 | 0 | setDaemon(true); |
| 162 | 0 | this.initialByteBufferSize = initialByteBufferSize; |
| 163 | 0 | } |
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
@Override |
| 169 | |
public void run(){ |
| 170 | 352 | if (byteBuffer == null){ |
| 171 | 352 | byteBuffer = ByteBufferFactory.allocate(byteBufferType, |
| 172 | |
initialByteBufferSize); |
| 173 | |
} |
| 174 | |
|
| 175 | 352 | if (target != null){ |
| 176 | 0 | target.run(); |
| 177 | 0 | return; |
| 178 | |
} |
| 179 | |
|
| 180 | 357393 | while (execute) { |
| 181 | |
try{ |
| 182 | |
|
| 183 | 357041 | Callable t = pipeline.waitForIoTask(); |
| 184 | 357041 | processTask(t); |
| 185 | 357041 | t = null; |
| 186 | 0 | } catch (Throwable t) { |
| 187 | 0 | if (execute) { |
| 188 | 0 | Controller.logger().log(Level.SEVERE, |
| 189 | |
"WorkerThreadImpl unexpected exception: ",t); |
| 190 | |
} else { |
| 191 | 0 | Controller.logger().log(Level.FINE, |
| 192 | |
"WorkerThreadImpl unexpected exception, when WorderThread supposed to be closed: ",t); |
| 193 | |
} |
| 194 | |
} finally { |
| 195 | 357041 | reset(); |
| 196 | 357041 | } |
| 197 | |
} |
| 198 | 351 | } |
| 199 | |
|
| 200 | |
|
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | |
|
| 205 | |
public void terminate(){ |
| 206 | 352 | execute = false; |
| 207 | 352 | } |
| 208 | |
|
| 209 | |
public ThreadAttachment updateAttachment(int mode) { |
| 210 | 159383 | ThreadAttachment currentAttachment = getAttachment(); |
| 211 | 159383 | currentAttachment.reset(); |
| 212 | |
|
| 213 | 159383 | if ((mode & Mode.BYTE_BUFFER) != 0) { |
| 214 | 12 | currentAttachment.setByteBuffer(byteBuffer); |
| 215 | |
} |
| 216 | |
|
| 217 | 159383 | if ((mode & Mode.SSL_ENGINE) != 0) { |
| 218 | 159380 | currentAttachment.setSSLEngine(sslEngine); |
| 219 | |
} |
| 220 | |
|
| 221 | 159383 | if ((mode & Mode.INPUT_BB) != 0) { |
| 222 | 159257 | currentAttachment.setInputBB(inputBB); |
| 223 | |
} |
| 224 | |
|
| 225 | 159383 | if ((mode & Mode.OUTPUT_BB) != 0) { |
| 226 | 311 | currentAttachment.setOutputBB(outputBB); |
| 227 | |
} |
| 228 | |
|
| 229 | 159383 | currentAttachment.setMode(mode); |
| 230 | |
|
| 231 | 159383 | return currentAttachment; |
| 232 | |
} |
| 233 | |
|
| 234 | |
public ThreadAttachment getAttachment() { |
| 235 | 265568 | if (threadAttachment == null) { |
| 236 | 132 | threadAttachment = new ThreadAttachment(); |
| 237 | |
} |
| 238 | |
|
| 239 | 265568 | return threadAttachment; |
| 240 | |
} |
| 241 | |
|
| 242 | |
public ThreadAttachment detach() { |
| 243 | 53093 | ThreadAttachment currentAttachment = getAttachment(); |
| 244 | 53093 | int mode = currentAttachment.getMode(); |
| 245 | 53093 | updateAttachment(mode); |
| 246 | |
|
| 247 | |
|
| 248 | 53093 | if ((mode & Mode.BYTE_BUFFER) != 0) { |
| 249 | 11 | byteBuffer = ByteBufferFactory.allocate(byteBufferType, |
| 250 | |
initialByteBufferSize); |
| 251 | |
} |
| 252 | |
|
| 253 | 53093 | if ((mode & Mode.SSL_ENGINE) != 0) { |
| 254 | 53091 | sslEngine = null; |
| 255 | |
} |
| 256 | |
|
| 257 | 53093 | if ((mode & Mode.INPUT_BB) != 0) { |
| 258 | 53091 | inputBB = null; |
| 259 | |
} |
| 260 | |
|
| 261 | 53093 | if ((mode & Mode.OUTPUT_BB) != 0) { |
| 262 | 110 | outputBB = null; |
| 263 | |
} |
| 264 | |
|
| 265 | |
|
| 266 | 53093 | this.threadAttachment = null; |
| 267 | |
|
| 268 | 53093 | currentAttachment.deassociate(); |
| 269 | 53093 | return currentAttachment; |
| 270 | |
} |
| 271 | |
|
| 272 | |
|
| 273 | |
public void attach(ThreadAttachment threadAttachment) { |
| 274 | 53083 | threadAttachment.associate(); |
| 275 | 53083 | int mode = threadAttachment.getMode(); |
| 276 | |
|
| 277 | 53083 | if ((mode & Mode.BYTE_BUFFER) != 0) { |
| 278 | 4 | byteBuffer = threadAttachment.getByteBuffer(); |
| 279 | |
} |
| 280 | |
|
| 281 | 53083 | if ((mode & Mode.SSL_ENGINE) != 0) { |
| 282 | 53081 | sslEngine = threadAttachment.getSSLEngine(); |
| 283 | |
} |
| 284 | |
|
| 285 | 53083 | if ((mode & Mode.INPUT_BB) != 0) { |
| 286 | 53081 | inputBB = threadAttachment.getInputBB(); |
| 287 | |
} |
| 288 | |
|
| 289 | 53083 | if ((mode & Mode.OUTPUT_BB) != 0) { |
| 290 | 103 | outputBB = threadAttachment.getOutputBB(); |
| 291 | |
} |
| 292 | |
|
| 293 | 53083 | this.threadAttachment = threadAttachment; |
| 294 | 53083 | } |
| 295 | |
|
| 296 | |
|
| 297 | |
|
| 298 | |
|
| 299 | |
|
| 300 | |
|
| 301 | |
|
| 302 | |
|
| 303 | |
public ByteBufferType getByteBufferType() { |
| 304 | 0 | return byteBufferType; |
| 305 | |
} |
| 306 | |
|
| 307 | |
|
| 308 | |
|
| 309 | |
|
| 310 | |
|
| 311 | |
|
| 312 | |
|
| 313 | |
public void setByteBufferType(ByteBufferType byteBufferType) { |
| 314 | 352 | this.byteBufferType = byteBufferType; |
| 315 | 352 | } |
| 316 | |
|
| 317 | |
|
| 318 | |
|
| 319 | |
|
| 320 | |
|
| 321 | |
|
| 322 | |
|
| 323 | |
protected void processTask(Callable t) throws Exception { |
| 324 | 357040 | if (t != null){ |
| 325 | 161370 | t.call(); |
| 326 | |
} |
| 327 | 357040 | } |
| 328 | |
|
| 329 | |
|
| 330 | |
@Override |
| 331 | |
protected void reset() { |
| 332 | 357040 | if (threadAttachment != null) { |
| 333 | |
|
| 334 | |
|
| 335 | |
|
| 336 | |
|
| 337 | |
|
| 338 | 122 | threadAttachment.deassociate(); |
| 339 | |
} |
| 340 | |
|
| 341 | 357041 | threadAttachment = null; |
| 342 | 357041 | super.reset(); |
| 343 | 357040 | } |
| 344 | |
} |
| 345 | |
|