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