| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | using System.Diagnostics; |
| | 4 | |
|
| | 5 | | namespace Ice.Internal; |
| | 6 | |
|
| | 7 | | public delegate void ThreadPoolWorkItem(ThreadPoolCurrent current); |
| | 8 | |
|
| | 9 | | public delegate void AsyncCallback(object state); |
| | 10 | |
|
| | 11 | | internal class ThreadPoolMessage : IDisposable |
| | 12 | | { |
| | 13 | | public ThreadPoolMessage(ThreadPoolCurrent current, object mutex) |
| | 14 | | { |
| | 15 | | _current = current; |
| | 16 | | _mutex = mutex; |
| | 17 | | _finish = false; |
| | 18 | | _finishWithIO = false; |
| | 19 | | } |
| | 20 | |
|
| | 21 | | public bool startIOScope() |
| | 22 | | { |
| | 23 | | // This must be called with the handler locked. |
| | 24 | | _finishWithIO = _current.startMessage(); |
| | 25 | | return _finishWithIO; |
| | 26 | | } |
| | 27 | |
|
| | 28 | | public void finishIOScope() |
| | 29 | | { |
| | 30 | | if (_finishWithIO) |
| | 31 | | { |
| | 32 | | // This must be called with the handler locked. |
| | 33 | | _current.finishMessage(); |
| | 34 | | } |
| | 35 | | } |
| | 36 | |
|
| | 37 | | public void ioCompleted() |
| | 38 | | { |
| | 39 | | // |
| | 40 | | // Call finishMessage once IO is completed only if serialization is not enabled. |
| | 41 | | // Otherwise, finishMessage will be called when the event handler is done with |
| | 42 | | // the message (it will be called from Dispose below). |
| | 43 | | // |
| | 44 | | Debug.Assert(_finishWithIO); |
| | 45 | | if (_current.ioCompleted()) |
| | 46 | | { |
| | 47 | | _finishWithIO = false; |
| | 48 | | _finish = true; |
| | 49 | | } |
| | 50 | | } |
| | 51 | |
|
| | 52 | | public void Dispose() |
| | 53 | | { |
| | 54 | | if (_finish) |
| | 55 | | { |
| | 56 | | // |
| | 57 | | // A ThreadPoolMessage instance must be created outside the synchronization of the event handler. We |
| | 58 | | // need to lock the event handler here to call finishMessage. |
| | 59 | | // |
| | 60 | | lock (_mutex) |
| | 61 | | { |
| | 62 | | _current.finishMessage(); |
| | 63 | | } |
| | 64 | | } |
| | 65 | | } |
| | 66 | |
|
| | 67 | | private readonly ThreadPoolCurrent _current; |
| | 68 | | private readonly object _mutex; |
| | 69 | | private bool _finish; |
| | 70 | | private bool _finishWithIO; |
| | 71 | | } |
| | 72 | |
|
| | 73 | | public class ThreadPoolCurrent |
| | 74 | | { |
| | 75 | | internal ThreadPoolCurrent(ThreadPool threadPool, ThreadPool.WorkerThread thread) |
| | 76 | | { |
| | 77 | | _threadPool = threadPool; |
| | 78 | | _thread = thread; |
| | 79 | | } |
| | 80 | |
|
| | 81 | | public int operation; |
| | 82 | |
|
| | 83 | | public bool ioCompleted() => _threadPool.ioCompleted(this); |
| | 84 | |
|
| | 85 | | public bool startMessage() => _threadPool.startMessage(this); |
| | 86 | |
|
| | 87 | | public void finishMessage() => _threadPool.finishMessage(this); |
| | 88 | |
|
| | 89 | | internal readonly ThreadPool _threadPool; |
| | 90 | | internal readonly ThreadPool.WorkerThread _thread; |
| | 91 | | internal bool _ioCompleted; |
| | 92 | | internal EventHandler _handler; |
| | 93 | | } |
| | 94 | |
|
| | 95 | | public sealed class ThreadPool : System.Threading.Tasks.TaskScheduler |
| | 96 | | { |
| 1 | 97 | | public ThreadPool(Instance instance, string prefix, int timeout) |
| | 98 | | { |
| 1 | 99 | | Ice.Properties properties = instance.initializationData().properties; |
| | 100 | |
|
| 1 | 101 | | _instance = instance; |
| 1 | 102 | | _executor = instance.initializationData().executor; |
| 1 | 103 | | _destroyed = false; |
| 1 | 104 | | _prefix = prefix; |
| 1 | 105 | | _threadIndex = 0; |
| 1 | 106 | | _inUse = 0; |
| 1 | 107 | | _serialize = properties.getPropertyAsInt(_prefix + ".Serialize") > 0; |
| 1 | 108 | | _serverIdleTime = timeout <= 0 ? Timeout.InfiniteTimeSpan : TimeSpan.FromSeconds(timeout); |
| | 109 | |
|
| 1 | 110 | | string programName = properties.getIceProperty("Ice.ProgramName"); |
| 1 | 111 | | if (programName.Length > 0) |
| | 112 | | { |
| 1 | 113 | | _threadPrefix = programName + "-" + _prefix; |
| | 114 | | } |
| | 115 | | else |
| | 116 | | { |
| 1 | 117 | | _threadPrefix = _prefix; |
| | 118 | | } |
| | 119 | |
|
| | 120 | | // |
| | 121 | | // We use just one thread as the default. This is the fastest |
| | 122 | | // possible setting, still allows one level of nesting, and |
| | 123 | | // doesn't require to make the servants thread safe. |
| | 124 | | // |
| 1 | 125 | | int size = properties.getPropertyAsIntWithDefault(_prefix + ".Size", 1); |
| 1 | 126 | | if (size < 1) |
| | 127 | | { |
| 0 | 128 | | string s = _prefix + ".Size < 1; Size adjusted to 1"; |
| 0 | 129 | | _instance.initializationData().logger.warning(s); |
| 0 | 130 | | size = 1; |
| | 131 | | } |
| | 132 | |
|
| 1 | 133 | | int sizeMax = properties.getPropertyAsIntWithDefault(_prefix + ".SizeMax", size); |
| 1 | 134 | | if (sizeMax < size) |
| | 135 | | { |
| 1 | 136 | | string s = _prefix + ".SizeMax < " + _prefix + ".Size; SizeMax adjusted to Size (" + size + ")"; |
| 1 | 137 | | _instance.initializationData().logger.warning(s); |
| 1 | 138 | | sizeMax = size; |
| | 139 | | } |
| | 140 | |
|
| 1 | 141 | | int sizeWarn = properties.getPropertyAsInt(_prefix + ".SizeWarn"); |
| 1 | 142 | | if (sizeWarn != 0 && sizeWarn < size) |
| | 143 | | { |
| 0 | 144 | | string s = _prefix + ".SizeWarn < " + _prefix + ".Size; adjusted SizeWarn to Size (" + size + ")"; |
| 0 | 145 | | _instance.initializationData().logger.warning(s); |
| 0 | 146 | | sizeWarn = size; |
| | 147 | | } |
| 1 | 148 | | else if (sizeWarn > sizeMax) |
| | 149 | | { |
| 0 | 150 | | string s = _prefix + ".SizeWarn > " + _prefix + ".SizeMax; adjusted SizeWarn to SizeMax (" |
| 0 | 151 | | + sizeMax + ")"; |
| 0 | 152 | | _instance.initializationData().logger.warning(s); |
| 0 | 153 | | sizeWarn = sizeMax; |
| | 154 | | } |
| | 155 | |
|
| 1 | 156 | | int threadIdleTime = properties.getPropertyAsIntWithDefault(_prefix + ".ThreadIdleTime", 60); |
| 1 | 157 | | if (threadIdleTime < 0) |
| | 158 | | { |
| 0 | 159 | | string s = _prefix + ".ThreadIdleTime < 0; ThreadIdleTime adjusted to 0"; |
| 0 | 160 | | _instance.initializationData().logger.warning(s); |
| 0 | 161 | | threadIdleTime = 0; |
| | 162 | | } |
| | 163 | |
|
| 1 | 164 | | _size = size; |
| 1 | 165 | | _sizeMax = sizeMax; |
| 1 | 166 | | _sizeWarn = sizeWarn; |
| 1 | 167 | | _threadIdleTime = threadIdleTime <= 0 ? Timeout.InfiniteTimeSpan : TimeSpan.FromSeconds(threadIdleTime); |
| | 168 | |
|
| 1 | 169 | | int stackSize = properties.getPropertyAsInt(_prefix + ".StackSize"); |
| 1 | 170 | | if (stackSize < 0) |
| | 171 | | { |
| 0 | 172 | | string s = _prefix + ".StackSize < 0; Size adjusted to OS default"; |
| 0 | 173 | | _instance.initializationData().logger.warning(s); |
| 0 | 174 | | stackSize = 0; |
| | 175 | | } |
| 1 | 176 | | _stackSize = stackSize; |
| | 177 | |
|
| 1 | 178 | | _priority = properties.getProperty(_prefix + ".ThreadPriority").Length > 0 ? |
| 1 | 179 | | Util.stringToThreadPriority(properties.getProperty(_prefix + ".ThreadPriority")) : |
| 1 | 180 | | Util.stringToThreadPriority(properties.getIceProperty("Ice.ThreadPriority")); |
| | 181 | |
|
| 1 | 182 | | if (_instance.traceLevels().threadPool >= 1) |
| | 183 | | { |
| 0 | 184 | | string s = "creating " + _prefix + ": Size = " + _size + ", SizeMax = " + _sizeMax + ", SizeWarn = " + |
| 0 | 185 | | _sizeWarn; |
| 0 | 186 | | _instance.initializationData().logger.trace(_instance.traceLevels().threadPoolCat, s); |
| | 187 | | } |
| | 188 | |
|
| 1 | 189 | | _workItems = new Queue<ThreadPoolWorkItem>(); |
| | 190 | |
|
| | 191 | | try |
| | 192 | | { |
| 1 | 193 | | _threads = new List<WorkerThread>(); |
| 1 | 194 | | for (int i = 0; i < _size; ++i) |
| | 195 | | { |
| 1 | 196 | | var thread = new WorkerThread(this, _threadPrefix + "-" + _threadIndex++); |
| 1 | 197 | | thread.start(_priority); |
| 1 | 198 | | _threads.Add(thread); |
| | 199 | | } |
| 1 | 200 | | } |
| 0 | 201 | | catch (System.Exception ex) |
| | 202 | | { |
| 0 | 203 | | string s = "cannot create thread for `" + _prefix + "':\n" + ex; |
| 0 | 204 | | _instance.initializationData().logger.error(s); |
| | 205 | |
|
| 0 | 206 | | destroy(); |
| 0 | 207 | | joinWithAllThreads(); |
| 0 | 208 | | throw; |
| | 209 | | } |
| 1 | 210 | | } |
| | 211 | |
|
| | 212 | | public void destroy() |
| | 213 | | { |
| 1 | 214 | | lock (_mutex) |
| | 215 | | { |
| 1 | 216 | | if (_destroyed) |
| | 217 | | { |
| 0 | 218 | | return; |
| | 219 | | } |
| 1 | 220 | | _destroyed = true; |
| 1 | 221 | | Monitor.PulseAll(_mutex); |
| 1 | 222 | | } |
| 1 | 223 | | } |
| | 224 | |
|
| | 225 | | public void updateObservers() |
| | 226 | | { |
| 1 | 227 | | lock (_mutex) |
| | 228 | | { |
| 1 | 229 | | foreach (WorkerThread t in _threads) |
| | 230 | | { |
| 1 | 231 | | t.updateObserver(); |
| | 232 | | } |
| | 233 | | } |
| 1 | 234 | | } |
| | 235 | |
|
| | 236 | | public void initialize(EventHandler handler) |
| | 237 | | { |
| 1 | 238 | | handler._ready = 0; |
| 1 | 239 | | handler._pending = 0; |
| 1 | 240 | | handler._started = 0; |
| 1 | 241 | | handler._finish = false; |
| 1 | 242 | | handler._hasMoreData = false; |
| 1 | 243 | | handler._registered = 0; |
| 1 | 244 | | } |
| | 245 | |
|
| 1 | 246 | | public void register(EventHandler handler, int op) => update(handler, SocketOperation.None, op); |
| | 247 | |
|
| | 248 | | public void update(EventHandler handler, int remove, int add) |
| | 249 | | { |
| 1 | 250 | | lock (_mutex) |
| | 251 | | { |
| | 252 | | Debug.Assert(!_destroyed); |
| | 253 | |
|
| | 254 | | // Don't remove what needs to be added |
| 1 | 255 | | remove &= ~add; |
| | 256 | |
|
| | 257 | | // Don't remove/add if already un-registered or registered |
| 1 | 258 | | remove &= handler._registered; |
| 1 | 259 | | add &= ~handler._registered; |
| 1 | 260 | | if (remove == add) |
| | 261 | | { |
| 1 | 262 | | return; |
| | 263 | | } |
| | 264 | |
|
| 1 | 265 | | handler._registered &= ~remove; |
| 1 | 266 | | handler._registered |= add; |
| | 267 | |
|
| 1 | 268 | | if ((add & SocketOperation.Read) != 0 && (handler._pending & SocketOperation.Read) == 0) |
| | 269 | | { |
| 1 | 270 | | handler._pending |= SocketOperation.Read; |
| 1 | 271 | | queueReadyForIOHandler(handler, SocketOperation.Read); |
| | 272 | | } |
| 1 | 273 | | else if ((add & SocketOperation.Write) != 0 && (handler._pending & SocketOperation.Write) == 0) |
| | 274 | | { |
| 1 | 275 | | handler._pending |= SocketOperation.Write; |
| 1 | 276 | | queueReadyForIOHandler(handler, SocketOperation.Write); |
| | 277 | | } |
| 1 | 278 | | } |
| 1 | 279 | | } |
| | 280 | |
|
| 1 | 281 | | public void unregister(EventHandler handler, int op) => update(handler, op, SocketOperation.None); |
| | 282 | |
|
| | 283 | | public void finish(EventHandler handler) |
| | 284 | | { |
| 1 | 285 | | lock (_mutex) |
| | 286 | | { |
| | 287 | | Debug.Assert(!_destroyed); |
| | 288 | |
|
| 1 | 289 | | handler._registered = SocketOperation.None; |
| | 290 | |
|
| | 291 | | // |
| | 292 | | // If there are no pending asynchronous operations, we can call finish on the handler now. |
| | 293 | | // |
| 1 | 294 | | if (handler._pending == 0) |
| | 295 | | { |
| 1 | 296 | | _workItems.Enqueue(current => |
| 1 | 297 | | { |
| 1 | 298 | | current.operation = SocketOperation.None; |
| 1 | 299 | | current._handler = handler; |
| 1 | 300 | | handler.finished(current); |
| 1 | 301 | | }); |
| 1 | 302 | | Monitor.Pulse(_mutex); |
| | 303 | | } |
| | 304 | | else |
| | 305 | | { |
| 1 | 306 | | handler._finish = true; |
| | 307 | | } |
| 1 | 308 | | } |
| 1 | 309 | | } |
| | 310 | |
|
| | 311 | | public void executeFromThisThread(System.Action call, Ice.Connection connection) |
| | 312 | | { |
| 1 | 313 | | if (_executor is not null) |
| | 314 | | { |
| | 315 | | try |
| | 316 | | { |
| 1 | 317 | | _executor(call, connection); |
| 1 | 318 | | } |
| 0 | 319 | | catch (System.Exception ex) |
| | 320 | | { |
| 0 | 321 | | if (_instance.initializationData().properties.getIcePropertyAsInt("Ice.Warn.Executor") > 0) |
| | 322 | | { |
| 0 | 323 | | _instance.initializationData().logger.warning($"executor exception:\n{ex}"); |
| | 324 | | } |
| 0 | 325 | | } |
| | 326 | | } |
| | 327 | | else |
| | 328 | | { |
| 1 | 329 | | call(); |
| | 330 | | } |
| 1 | 331 | | } |
| | 332 | |
|
| | 333 | | public void execute(Action workItem, Ice.Connection connection) |
| | 334 | | { |
| 1 | 335 | | lock (_mutex) |
| | 336 | | { |
| 1 | 337 | | if (_destroyed) |
| | 338 | | { |
| 0 | 339 | | throw new Ice.CommunicatorDestroyedException(); |
| | 340 | | } |
| 1 | 341 | | _workItems.Enqueue(current => |
| 1 | 342 | | { |
| 1 | 343 | | current.ioCompleted(); |
| 1 | 344 | | executeFromThisThread(workItem, connection); |
| 1 | 345 | | }); |
| 1 | 346 | | Monitor.Pulse(_mutex); |
| 1 | 347 | | } |
| 1 | 348 | | } |
| | 349 | |
|
| | 350 | | public void joinWithAllThreads() |
| | 351 | | { |
| | 352 | | // |
| | 353 | | // _threads is immutable after destroy() has been called, therefore no synchronization is needed. |
| | 354 | | // (Synchronization wouldn't be possible here anyway, because otherwise the other threads would never |
| | 355 | | // terminate.) |
| | 356 | | // |
| | 357 | | Debug.Assert(_destroyed); |
| 1 | 358 | | foreach (WorkerThread thread in _threads) |
| | 359 | | { |
| 1 | 360 | | thread.join(); |
| | 361 | | } |
| 1 | 362 | | } |
| | 363 | |
|
| 0 | 364 | | public string prefix() => _prefix; |
| | 365 | |
|
| 0 | 366 | | public bool serialize() => _serialize; |
| | 367 | |
|
| | 368 | | protected sealed override void QueueTask( |
| 1 | 369 | | System.Threading.Tasks.Task task) => execute( |
| 1 | 370 | | () => TryExecuteTask(task), |
| 1 | 371 | | null); |
| | 372 | |
|
| | 373 | | protected sealed override bool TryExecuteTaskInline(System.Threading.Tasks.Task task, bool taskWasPreviouslyQueued) |
| | 374 | | { |
| 1 | 375 | | if (!taskWasPreviouslyQueued) |
| | 376 | | { |
| 1 | 377 | | executeFromThisThread(() => TryExecuteTask(task), null); |
| 1 | 378 | | return true; |
| | 379 | | } |
| 1 | 380 | | return false; |
| | 381 | | } |
| | 382 | |
|
| 0 | 383 | | protected sealed override bool TryDequeue(System.Threading.Tasks.Task task) => false; |
| | 384 | |
|
| | 385 | | protected sealed override IEnumerable<System.Threading.Tasks.Task> GetScheduledTasks() => |
| 0 | 386 | | Array.Empty<System.Threading.Tasks.Task>(); |
| | 387 | |
|
| | 388 | | private void queueReadyForIOHandler(EventHandler handler, int operation) |
| | 389 | | { |
| 1 | 390 | | lock (_mutex) |
| | 391 | | { |
| | 392 | | Debug.Assert(!_destroyed); |
| 1 | 393 | | _workItems.Enqueue(current => |
| 1 | 394 | | { |
| 1 | 395 | | current._handler = handler; |
| 1 | 396 | | current.operation = operation; |
| 1 | 397 | | try |
| 1 | 398 | | { |
| 1 | 399 | | current._handler.message(current); |
| 1 | 400 | | } |
| 0 | 401 | | catch (System.Exception ex) |
| 1 | 402 | | { |
| 0 | 403 | | string s = "exception in `" + _prefix + "':\n" + ex + "\nevent handler: " + |
| 0 | 404 | | current._handler.ToString(); |
| 0 | 405 | | _instance.initializationData().logger.error(s); |
| 0 | 406 | | } |
| 1 | 407 | | }); |
| 1 | 408 | | Monitor.Pulse(_mutex); |
| 1 | 409 | | } |
| 1 | 410 | | } |
| | 411 | |
|
| | 412 | | private void run(WorkerThread thread) |
| | 413 | | { |
| 1 | 414 | | var current = new ThreadPoolCurrent(this, thread); |
| | 415 | | while (true) |
| | 416 | | { |
| 1 | 417 | | ThreadPoolWorkItem workItem = null; |
| 1 | 418 | | lock (_mutex) |
| | 419 | | { |
| 1 | 420 | | while (_workItems.Count == 0) |
| | 421 | | { |
| 1 | 422 | | if (_destroyed) |
| | 423 | | { |
| 1 | 424 | | return; |
| | 425 | | } |
| | 426 | |
|
| 1 | 427 | | if (_threadIdleTime != Timeout.InfiniteTimeSpan) |
| | 428 | | { |
| 1 | 429 | | if (!Monitor.Wait(_mutex, _threadIdleTime) && _workItems.Count == 0) // If timeout |
| | 430 | | { |
| 1 | 431 | | if (_destroyed) |
| | 432 | | { |
| 0 | 433 | | return; |
| | 434 | | } |
| 1 | 435 | | else if (_inUse < _threads.Count - 1) |
| | 436 | | { |
| 0 | 437 | | if (_instance.traceLevels().threadPool >= 1) |
| | 438 | | { |
| 0 | 439 | | string s = "shrinking " + _prefix + ": Size=" + (_threads.Count - 1); |
| 0 | 440 | | _instance.initializationData().logger.trace( |
| 0 | 441 | | _instance.traceLevels().threadPoolCat, s); |
| | 442 | | } |
| | 443 | |
|
| 0 | 444 | | _threads.Remove(thread); |
| 0 | 445 | | _workItems.Enqueue(c => |
| 0 | 446 | | // No call to ioCompleted, this shouldn't block (and we don't want to cause |
| 0 | 447 | | // a new thread to be started). |
| 0 | 448 | | thread.join()); |
| 0 | 449 | | Monitor.Pulse(_mutex); |
| 0 | 450 | | return; |
| | 451 | | } |
| 1 | 452 | | else if (_inUse > 0) |
| | 453 | | { |
| | 454 | | // |
| | 455 | | // If this is the last idle thread but there are still other threads |
| | 456 | | // busy dispatching, we go back waiting with _threadIdleTime. We only |
| | 457 | | // wait with _serverIdleTime when there's only one thread left. |
| | 458 | | // |
| | 459 | | continue; |
| | 460 | | } |
| | 461 | |
|
| | 462 | | Debug.Assert(_threads.Count == 1); |
| 1 | 463 | | if (!Monitor.Wait(_mutex, _serverIdleTime) && !_destroyed) |
| | 464 | | { |
| 1 | 465 | | _workItems.Enqueue(c => |
| 1 | 466 | | { |
| 1 | 467 | | c.ioCompleted(); |
| 1 | 468 | | try |
| 1 | 469 | | { |
| 1 | 470 | | _instance.objectAdapterFactory().shutdown(); |
| 1 | 471 | | } |
| 0 | 472 | | catch (Ice.CommunicatorDestroyedException) |
| 1 | 473 | | { |
| 0 | 474 | | } |
| 1 | 475 | | }); |
| | 476 | | } |
| | 477 | | } |
| | 478 | | } |
| | 479 | | else |
| | 480 | | { |
| 0 | 481 | | Monitor.Wait(_mutex); |
| | 482 | | } |
| | 483 | | } |
| | 484 | |
|
| | 485 | | Debug.Assert(_workItems.Count > 0); |
| 1 | 486 | | workItem = _workItems.Dequeue(); |
| | 487 | |
|
| 1 | 488 | | current._thread.setState(Ice.Instrumentation.ThreadState.ThreadStateInUseForIO); |
| 1 | 489 | | current._ioCompleted = false; |
| 1 | 490 | | } |
| | 491 | |
|
| | 492 | | try |
| | 493 | | { |
| 1 | 494 | | workItem(current); |
| 1 | 495 | | } |
| 0 | 496 | | catch (System.Exception ex) |
| | 497 | | { |
| 0 | 498 | | string s = "exception in `" + _prefix + "' while calling on work item:\n" + ex + '\n'; |
| 0 | 499 | | _instance.initializationData().logger.error(s); |
| 0 | 500 | | } |
| | 501 | |
|
| 1 | 502 | | lock (_mutex) |
| | 503 | | { |
| 1 | 504 | | if (_sizeMax > 1 && current._ioCompleted) |
| | 505 | | { |
| | 506 | | Debug.Assert(_inUse > 0); |
| 1 | 507 | | --_inUse; |
| | 508 | | } |
| 1 | 509 | | thread.setState(Ice.Instrumentation.ThreadState.ThreadStateIdle); |
| 1 | 510 | | } |
| | 511 | | } |
| 1 | 512 | | } |
| | 513 | |
|
| | 514 | | public bool ioCompleted(ThreadPoolCurrent current) |
| | 515 | | { |
| 1 | 516 | | lock (_mutex) |
| | 517 | | { |
| 1 | 518 | | current._ioCompleted = true; // Set the IO completed flag to specify that ioCompleted() has been called. |
| | 519 | |
|
| 1 | 520 | | current._thread.setState(Ice.Instrumentation.ThreadState.ThreadStateInUseForUser); |
| | 521 | |
|
| 1 | 522 | | if (_sizeMax > 1) |
| | 523 | | { |
| | 524 | | Debug.Assert(_inUse >= 0); |
| 1 | 525 | | ++_inUse; |
| | 526 | |
|
| 1 | 527 | | if (_sizeMax > 1 && _inUse == _sizeWarn) |
| | 528 | | { |
| 0 | 529 | | string s = "thread pool `" + _prefix + "' is running low on threads\n" |
| 0 | 530 | | + "Size=" + _size + ", " + "SizeMax=" + _sizeMax + ", " + "SizeWarn=" + _sizeWarn; |
| 0 | 531 | | _instance.initializationData().logger.warning(s); |
| | 532 | | } |
| | 533 | |
|
| 1 | 534 | | if (!_destroyed && _inUse < _sizeMax && _inUse == _threads.Count) |
| | 535 | | { |
| 1 | 536 | | if (_instance.traceLevels().threadPool >= 1) |
| | 537 | | { |
| 0 | 538 | | string s = "growing " + _prefix + ": Size = " + (_threads.Count + 1); |
| 0 | 539 | | _instance.initializationData().logger.trace(_instance.traceLevels().threadPoolCat, s); |
| | 540 | | } |
| | 541 | |
|
| | 542 | | try |
| | 543 | | { |
| 1 | 544 | | var t = new WorkerThread(this, _threadPrefix + "-" + _threadIndex++); |
| 1 | 545 | | t.start(_priority); |
| 1 | 546 | | _threads.Add(t); |
| 1 | 547 | | } |
| 0 | 548 | | catch (System.Exception ex) |
| | 549 | | { |
| 0 | 550 | | string s = "cannot create thread for `" + _prefix + "':\n" + ex; |
| 0 | 551 | | _instance.initializationData().logger.error(s); |
| 0 | 552 | | } |
| | 553 | | } |
| | 554 | | } |
| 1 | 555 | | } |
| 1 | 556 | | return _serialize; |
| | 557 | | } |
| | 558 | |
|
| | 559 | | public bool startMessage(ThreadPoolCurrent current) |
| | 560 | | { |
| | 561 | | Debug.Assert((current._handler._pending & current.operation) != 0); |
| | 562 | |
|
| 1 | 563 | | if ((current._handler._started & current.operation) != 0) |
| | 564 | | { |
| | 565 | | Debug.Assert((current._handler._ready & current.operation) == 0); |
| 1 | 566 | | current._handler._ready |= current.operation; |
| 1 | 567 | | current._handler._started &= ~current.operation; |
| 1 | 568 | | if (!current._handler.finishAsync(current.operation)) // Returns false if the handler is finished. |
| | 569 | | { |
| 1 | 570 | | current._handler._pending &= ~current.operation; |
| 1 | 571 | | if (current._handler._pending == 0 && current._handler._finish) |
| | 572 | | { |
| 1 | 573 | | finish(current._handler); |
| | 574 | | } |
| 1 | 575 | | return false; |
| | 576 | | } |
| | 577 | | } |
| 1 | 578 | | else if ((current._handler._ready & current.operation) == 0 && |
| 1 | 579 | | (current._handler._registered & current.operation) != 0) |
| | 580 | | { |
| | 581 | | Debug.Assert((current._handler._started & current.operation) == 0); |
| 1 | 582 | | if (!current._handler.startAsync(current.operation, getCallback(current.operation))) |
| | 583 | | { |
| 0 | 584 | | current._handler._pending &= ~current.operation; |
| 0 | 585 | | if (current._handler._pending == 0 && current._handler._finish) |
| | 586 | | { |
| 0 | 587 | | finish(current._handler); |
| | 588 | | } |
| 0 | 589 | | return false; |
| | 590 | | } |
| | 591 | | else |
| | 592 | | { |
| 1 | 593 | | current._handler._started |= current.operation; |
| 1 | 594 | | return false; |
| | 595 | | } |
| | 596 | | } |
| | 597 | |
|
| 1 | 598 | | if ((current._handler._registered & current.operation) != 0) |
| | 599 | | { |
| | 600 | | Debug.Assert((current._handler._ready & current.operation) != 0); |
| 1 | 601 | | current._handler._ready &= ~current.operation; |
| 1 | 602 | | return true; |
| | 603 | | } |
| | 604 | | else |
| | 605 | | { |
| 1 | 606 | | current._handler._pending &= ~current.operation; |
| 1 | 607 | | if (current._handler._pending == 0 && current._handler._finish) |
| | 608 | | { |
| 1 | 609 | | finish(current._handler); |
| | 610 | | } |
| 1 | 611 | | return false; |
| | 612 | | } |
| | 613 | | } |
| | 614 | |
|
| | 615 | | public void finishMessage(ThreadPoolCurrent current) |
| | 616 | | { |
| 1 | 617 | | if ((current._handler._registered & current.operation) != 0) |
| | 618 | | { |
| | 619 | | Debug.Assert((current._handler._ready & current.operation) == 0); |
| 1 | 620 | | if (!current._handler.startAsync(current.operation, getCallback(current.operation))) |
| | 621 | | { |
| 0 | 622 | | current._handler._pending &= ~current.operation; |
| | 623 | | } |
| | 624 | | else |
| | 625 | | { |
| | 626 | | Debug.Assert((current._handler._pending & current.operation) != 0); |
| 1 | 627 | | current._handler._started |= current.operation; |
| | 628 | | } |
| | 629 | | } |
| | 630 | | else |
| | 631 | | { |
| 1 | 632 | | current._handler._pending &= ~current.operation; |
| | 633 | | } |
| | 634 | |
|
| 1 | 635 | | if (current._handler._pending == 0 && current._handler._finish) |
| | 636 | | { |
| | 637 | | // There are no more pending async operations, it's time to call finish. |
| 1 | 638 | | finish(current._handler); |
| | 639 | | } |
| 1 | 640 | | } |
| | 641 | |
|
| | 642 | | private AsyncCallback getCallback(int operation) |
| | 643 | | { |
| | 644 | | Debug.Assert(operation == SocketOperation.Read || operation == SocketOperation.Write); |
| 1 | 645 | | return state => queueReadyForIOHandler((EventHandler)state, operation); |
| | 646 | | } |
| | 647 | |
|
| | 648 | | private readonly Instance _instance; |
| | 649 | | private readonly System.Action<System.Action, Ice.Connection> _executor; |
| | 650 | | private bool _destroyed; |
| | 651 | | private readonly string _prefix; |
| | 652 | | private readonly string _threadPrefix; |
| | 653 | |
|
| 1 | 654 | | private readonly object _mutex = new object(); |
| | 655 | |
|
| | 656 | | internal sealed class WorkerThread |
| | 657 | | { |
| | 658 | | private readonly ThreadPool _threadPool; |
| | 659 | | private Ice.Instrumentation.ThreadObserver _observer; |
| | 660 | | private Ice.Instrumentation.ThreadState _state; |
| | 661 | |
|
| 1 | 662 | | internal WorkerThread(ThreadPool threadPool, string name) |
| | 663 | | { |
| 1 | 664 | | _threadPool = threadPool; |
| 1 | 665 | | _name = name; |
| 1 | 666 | | _state = Ice.Instrumentation.ThreadState.ThreadStateIdle; |
| 1 | 667 | | updateObserver(); |
| 1 | 668 | | } |
| | 669 | |
|
| | 670 | | public void updateObserver() |
| | 671 | | { |
| | 672 | | // Must be called with the thread pool mutex locked |
| 1 | 673 | | Ice.Instrumentation.CommunicatorObserver obsv = _threadPool._instance.initializationData().observer; |
| 1 | 674 | | if (obsv is not null) |
| | 675 | | { |
| 1 | 676 | | _observer = obsv.getThreadObserver(_threadPool._prefix, _name, _state, _observer); |
| 1 | 677 | | _observer?.attach(); |
| | 678 | | } |
| 1 | 679 | | } |
| | 680 | |
|
| | 681 | | public void setState(Ice.Instrumentation.ThreadState s) |
| | 682 | | { |
| | 683 | | // Must be called with the thread pool mutex locked |
| 1 | 684 | | if (_observer is not null) |
| | 685 | | { |
| 1 | 686 | | if (_state != s) |
| | 687 | | { |
| 1 | 688 | | _observer.stateChanged(_state, s); |
| | 689 | | } |
| | 690 | | } |
| 1 | 691 | | _state = s; |
| 1 | 692 | | } |
| | 693 | |
|
| 0 | 694 | | public Thread getThread() => _thread; |
| | 695 | |
|
| 1 | 696 | | public void join() => _thread.Join(); |
| | 697 | |
|
| | 698 | | public void start(ThreadPriority priority) |
| | 699 | | { |
| 1 | 700 | | if (_threadPool._stackSize == 0) |
| | 701 | | { |
| 1 | 702 | | _thread = new Thread(new ThreadStart(Run)); |
| | 703 | | } |
| | 704 | | else |
| | 705 | | { |
| 0 | 706 | | _thread = new Thread(new ThreadStart(Run), _threadPool._stackSize); |
| | 707 | | } |
| 1 | 708 | | _thread.IsBackground = true; |
| 1 | 709 | | _thread.Name = _name; |
| 1 | 710 | | _thread.Priority = priority; |
| 1 | 711 | | _thread.Start(); |
| 1 | 712 | | } |
| | 713 | |
|
| | 714 | | public void Run() |
| | 715 | | { |
| 1 | 716 | | if (_threadPool._instance.initializationData().threadStart is not null) |
| | 717 | | { |
| | 718 | | try |
| | 719 | | { |
| 0 | 720 | | _threadPool._instance.initializationData().threadStart(); |
| 0 | 721 | | } |
| 0 | 722 | | catch (System.Exception ex) |
| | 723 | | { |
| 0 | 724 | | string s = "thread hook start() method raised an unexpected exception in `"; |
| 0 | 725 | | s += _threadPool._prefix + "' thread " + _thread.Name + ":\n" + ex; |
| 0 | 726 | | _threadPool._instance.initializationData().logger.error(s); |
| 0 | 727 | | } |
| | 728 | | } |
| | 729 | |
|
| | 730 | | try |
| | 731 | | { |
| 1 | 732 | | _threadPool.run(this); |
| 1 | 733 | | } |
| 0 | 734 | | catch (System.Exception ex) |
| | 735 | | { |
| 0 | 736 | | string s = "exception in `" + _threadPool._prefix + "' thread " + _thread.Name + ":\n" + ex; |
| 0 | 737 | | _threadPool._instance.initializationData().logger.error(s); |
| 0 | 738 | | } |
| | 739 | |
|
| 1 | 740 | | _observer?.detach(); |
| | 741 | |
|
| 1 | 742 | | if (_threadPool._instance.initializationData().threadStop is not null) |
| | 743 | | { |
| | 744 | | try |
| | 745 | | { |
| 0 | 746 | | _threadPool._instance.initializationData().threadStop(); |
| 0 | 747 | | } |
| 0 | 748 | | catch (System.Exception ex) |
| | 749 | | { |
| 0 | 750 | | string s = "thread hook stop() method raised an unexpected exception in `"; |
| 0 | 751 | | s += _threadPool._prefix + "' thread " + _thread.Name + ":\n" + ex; |
| 0 | 752 | | _threadPool._instance.initializationData().logger.error(s); |
| 0 | 753 | | } |
| | 754 | | } |
| 1 | 755 | | } |
| | 756 | |
|
| | 757 | | private readonly string _name; |
| | 758 | | private Thread _thread; |
| | 759 | | } |
| | 760 | |
|
| | 761 | | private readonly int _size; // Number of threads that are pre-created. |
| | 762 | | private readonly int _sizeMax; // Maximum number of threads. |
| | 763 | | private readonly int _sizeWarn; // If _inUse reaches _sizeWarn, a "low on threads" warning will be printed. |
| | 764 | | private readonly bool _serialize; // True if requests need to be serialized over the connection. |
| | 765 | | private readonly ThreadPriority _priority; |
| | 766 | | private readonly TimeSpan _serverIdleTime; |
| | 767 | | private readonly TimeSpan _threadIdleTime; |
| | 768 | | private readonly int _stackSize; |
| | 769 | |
|
| | 770 | | private readonly List<WorkerThread> _threads; // All threads, running or not. |
| | 771 | | private int _threadIndex; // For assigning thread names. |
| | 772 | | private int _inUse; // Number of threads that are currently in use. |
| | 773 | |
|
| | 774 | | private readonly Queue<ThreadPoolWorkItem> _workItems; |
| | 775 | | } |