| 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 | 353 | 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 | 353 | 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 | 353 | super(threadGroup, name); |
| 144 | 353 | this.pipeline = pipeline; |
| 145 | 353 | setDaemon(true); |
| 146 | 353 | this.initialByteBufferSize = initialByteBufferSize; |
| 147 | 353 | } |
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
@Override |
| 153 | |
public void run(){ |
| 154 | 353 | if (byteBuffer == null){ |
| 155 | 353 | byteBuffer = ByteBufferFactory.allocate(byteBufferType, |
| 156 | |
initialByteBufferSize); |
| 157 | |
} |
| 158 | |
|
| 159 | 353 | if (target != null){ |
| 160 | 0 | target.run(); |
| 161 | 0 | return; |
| 162 | |
} |
| 163 | |
|
| 164 | 389238 | while (execute) { |
| 165 | |
try{ |
| 166 | |
|
| 167 | 388885 | Callable t = pipeline.waitForIoTask(); |
| 168 | 388885 | if (t != null){ |
| 169 | 156189 | t.call(); |
| 170 | 156189 | t = null; |
| 171 | |
} |
| 172 | 0 | } catch (Throwable t) { |
| 173 | 0 | if (execute) { |
| 174 | 0 | Controller.logger().log(Level.SEVERE, |
| 175 | |
"WorkerThreadImpl unexpected exception: ",t); |
| 176 | |
} else { |
| 177 | 0 | Controller.logger().log(Level.FINE, |
| 178 | |
"WorkerThreadImpl unexpected exception, when WorderThread supposed to be closed: ",t); |
| 179 | |
} |
| 180 | |
} finally { |
| 181 | 388885 | reset(); |
| 182 | 388882 | } |
| 183 | |
} |
| 184 | 352 | } |
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
public void terminate(){ |
| 192 | 353 | execute = false; |
| 193 | 353 | } |
| 194 | |
|
| 195 | |
public ThreadAttachment updateAttachment(int mode) { |
| 196 | 161777 | ThreadAttachment currentAttachment = getAttachment(); |
| 197 | 161777 | currentAttachment.reset(); |
| 198 | |
|
| 199 | 161777 | if ((mode & Mode.BYTE_BUFFER) != 0) { |
| 200 | 12 | currentAttachment.setByteBuffer(byteBuffer); |
| 201 | |
} |
| 202 | |
|
| 203 | 161777 | if ((mode & Mode.SSL_ENGINE) != 0) { |
| 204 | 161774 | currentAttachment.setSSLEngine(sslEngine); |
| 205 | |
} |
| 206 | |
|
| 207 | 161777 | if ((mode & Mode.INPUT_BB) != 0) { |
| 208 | 161658 | currentAttachment.setInputBB(inputBB); |
| 209 | |
} |
| 210 | |
|
| 211 | 161777 | if ((mode & Mode.OUTPUT_BB) != 0) { |
| 212 | 582 | currentAttachment.setOutputBB(outputBB); |
| 213 | |
} |
| 214 | |
|
| 215 | 161777 | currentAttachment.setMode(mode); |
| 216 | |
|
| 217 | 161777 | return currentAttachment; |
| 218 | |
} |
| 219 | |
|
| 220 | |
public ThreadAttachment getAttachment() { |
| 221 | 269563 | if (threadAttachment == null) { |
| 222 | 124 | threadAttachment = new ThreadAttachment(); |
| 223 | |
} |
| 224 | |
|
| 225 | 269563 | return threadAttachment; |
| 226 | |
} |
| 227 | |
|
| 228 | |
public ThreadAttachment detach() { |
| 229 | 53894 | ThreadAttachment currentAttachment = getAttachment(); |
| 230 | 53894 | int mode = currentAttachment.getMode(); |
| 231 | 53894 | updateAttachment(mode); |
| 232 | |
|
| 233 | |
|
| 234 | 53894 | if ((mode & Mode.BYTE_BUFFER) != 0) { |
| 235 | 11 | byteBuffer = ByteBufferFactory.allocate(byteBufferType, |
| 236 | |
initialByteBufferSize); |
| 237 | |
} |
| 238 | |
|
| 239 | 53894 | if ((mode & Mode.SSL_ENGINE) != 0) { |
| 240 | 53892 | sslEngine = null; |
| 241 | |
} |
| 242 | |
|
| 243 | 53894 | if ((mode & Mode.INPUT_BB) != 0) { |
| 244 | 53892 | inputBB = null; |
| 245 | |
} |
| 246 | |
|
| 247 | 53894 | if ((mode & Mode.OUTPUT_BB) != 0) { |
| 248 | 200 | outputBB = null; |
| 249 | |
} |
| 250 | |
|
| 251 | |
|
| 252 | 53894 | this.threadAttachment = null; |
| 253 | |
|
| 254 | 53894 | currentAttachment.deassociate(); |
| 255 | 53894 | return currentAttachment; |
| 256 | |
} |
| 257 | |
|
| 258 | |
|
| 259 | |
public void attach(ThreadAttachment threadAttachment) { |
| 260 | 53884 | threadAttachment.associate(); |
| 261 | 53884 | int mode = threadAttachment.getMode(); |
| 262 | |
|
| 263 | 53884 | if ((mode & Mode.BYTE_BUFFER) != 0) { |
| 264 | 4 | byteBuffer = threadAttachment.getByteBuffer(); |
| 265 | |
} |
| 266 | |
|
| 267 | 53884 | if ((mode & Mode.SSL_ENGINE) != 0) { |
| 268 | 53881 | sslEngine = threadAttachment.getSSLEngine(); |
| 269 | |
} |
| 270 | |
|
| 271 | 53884 | if ((mode & Mode.INPUT_BB) != 0) { |
| 272 | 53881 | inputBB = threadAttachment.getInputBB(); |
| 273 | |
} |
| 274 | |
|
| 275 | 53884 | if ((mode & Mode.OUTPUT_BB) != 0) { |
| 276 | 194 | outputBB = threadAttachment.getOutputBB(); |
| 277 | |
} |
| 278 | |
|
| 279 | 53884 | this.threadAttachment = threadAttachment; |
| 280 | 53884 | } |
| 281 | |
|
| 282 | |
|
| 283 | |
|
| 284 | |
|
| 285 | |
|
| 286 | |
|
| 287 | |
|
| 288 | |
|
| 289 | |
public ByteBufferType getByteBufferType() { |
| 290 | 0 | return byteBufferType; |
| 291 | |
} |
| 292 | |
|
| 293 | |
|
| 294 | |
|
| 295 | |
|
| 296 | |
|
| 297 | |
|
| 298 | |
|
| 299 | |
public void setByteBufferType(ByteBufferType byteBufferType) { |
| 300 | 353 | this.byteBufferType = byteBufferType; |
| 301 | 353 | } |
| 302 | |
|
| 303 | |
@Override |
| 304 | |
protected void reset() { |
| 305 | 388885 | if (threadAttachment != null) { |
| 306 | |
|
| 307 | |
|
| 308 | |
|
| 309 | |
|
| 310 | |
|
| 311 | 114 | threadAttachment.deassociate(); |
| 312 | |
} |
| 313 | |
|
| 314 | 388885 | threadAttachment = null; |
| 315 | 388880 | super.reset(); |
| 316 | 388885 | } |
| 317 | |
} |
| 318 | |
|