summaryrefslogtreecommitdiff
path: root/cs/src/Ice/ThreadPool.cs
blob: 7c4be1a72fe089d33fa65029e04b9e17a6702dac (plain)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
// **********************************************************************
//
// Copyright (c) 2003-2008 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

//
// Uncomment these definitions as needed. (We use #define here instead
// of constants to stop the compiler from complaining about unreachable code.)
//
//#define TRACE_REGISTRATION
//#define TRACE_SHUTDOWN
//#define TRACE_MESSAGE
//#define TRACE_EXCEPTION
//#define TRACE_THREAD
//#define TRACE_STACK_TRACE

namespace IceInternal
{

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Threading;

    public delegate void ThreadPoolWorkItem(bool safeThread);

    public sealed class ThreadPool
    {
        public ThreadPool(Instance instance, string prefix, int timeout)
        {
            _instance = instance;
            _destroyed = false;
            _prefix = prefix;
            _timeout = timeout;
            _size = 0;
            _sizeMax = 0;
            _sizeWarn = 0;
            _threadIndex = 0;
            _running = 0;
            _inUse = 0;
            _load = 1.0;
            _serialize = _instance.initializationData().properties.getPropertyAsInt(_prefix + ".Serialize") > 0;

            string programName = _instance.initializationData().properties.getProperty("Ice.ProgramName");
            if(programName.Length > 0)
            {
                _programNamePrefix = programName + "-";
            }
            else
            {
                _programNamePrefix = "";
            }

            //
            // We use just one thread as the default. This is the fastest
            // possible setting, still allows one level of nesting, and
            // doesn't require to make the servants thread safe.
            //
            int size = _instance.initializationData().properties.getPropertyAsIntWithDefault(_prefix + ".Size", 1);
            if(size < 1)
            {
                string s = _prefix + ".Size < 1; Size adjusted to 1";
                _instance.initializationData().logger.warning(s);
                size = 1;
            }

            int sizeMax =
                _instance.initializationData().properties.getPropertyAsIntWithDefault(_prefix + ".SizeMax", size);
            if(sizeMax < size)
            {
                string s = _prefix + ".SizeMax < " + _prefix + ".Size; SizeMax adjusted to Size (" + size + ")";
                _instance.initializationData().logger.warning(s);
                sizeMax = size;
            }

            int sizeWarn = _instance.initializationData().properties.getPropertyAsIntWithDefault(_prefix + ".SizeWarn",
                                                                              sizeMax * 80 / 100);
            if(sizeWarn > sizeMax)
            {
                string s = _prefix + ".SizeWarn > " + _prefix + ".SizeMax; adjusted SizeWarn to SizeMax ("
                    + sizeMax + ")";
                _instance.initializationData().logger.warning(s);
                sizeWarn = sizeMax;
            }

            _size = size;
            _sizeMax = sizeMax;
            _sizeWarn = sizeWarn;

            try
            {
                _threads = new List<WorkerThread>();
                for(int i = 0; i < _size; ++i)
                {
                    WorkerThread thread = new WorkerThread(this, _programNamePrefix + _prefix + "-" + _threadIndex++);
                    _threads.Add(thread);
                    thread.Start();
                    ++_running;
                }
            }
            catch(System.Exception ex)
            {
                string s = "cannot create thread for `" + _prefix + "':\n" + ex;
                _instance.initializationData().logger.error(s);

                destroy();
                joinWithAllThreads();
                throw;
            }
        }

        public void destroy()
        {
            lock(this)
            {
                #if TRACE_SHUTDOWN
                    trace("destroy");
                #endif

                Debug.Assert(!_destroyed);
                _destroyed = true;
                Monitor.PulseAll(this);
            }
        }

        public void execute(ThreadPoolWorkItem workItem)
        {
            lock(this)
            {
                #if TRACE_REGISTRATION
                    trace("adding work item");
                    #if TRACE_STACK_TRACE
                        Console.Error.WriteLine(Environment.StackTrace);
                    #endif
                #endif

                if(_destroyed)
                {
                    throw new Ice.CommunicatorDestroyedException();
                }
                _workItems.AddLast(workItem);
                Monitor.Pulse(this);
            }
        }

        public void finish(Ice.ConnectionI connection)
        {
            lock(this)
            {
                if(_destroyed)
                {
                    throw new Ice.CommunicatorDestroyedException();
                }
                _finished.AddLast(connection);
                Monitor.Pulse(this);
            }
        }

        public void joinWithAllThreads()
        {
            //
            // _threads is immutable after destroy() has been called,
            // therefore no synchronization is needed. (Synchronization
            // wouldn't be possible here anyway, because otherwise the
            // other threads would never terminate.)
            //
            Debug.Assert(_destroyed);
            foreach(WorkerThread thread in _threads)
            {
                thread.Join();
            }
        }

        public string prefix()
        {
            return _prefix;
        }

        public bool
        serialize()
        {
            return _serialize;
        }

        private void run()
        {
            while(true)
            {
                ThreadPoolWorkItem workItem = null;
                Ice.ConnectionI handler = null;
                lock(this)
                {
                    while(_finished.Count == 0 && _workItems.Count == 0 && !_destroyed)
                    {
                        if(!Monitor.Wait(this, _timeout > 0 ? _timeout * 1000 : Timeout.Infinite))
                        {
                            //
                            // Initiate server shutdown.
                            //
                            try
                            {
                                _instance.objectAdapterFactory().shutdown();
                            }
                            catch(Ice.CommunicatorDestroyedException)
                            {
                            }
                            continue;
                        }
                    }

                    Debug.Assert(_finished.Count > 0 || _workItems.Count > 0 || _destroyed);

                    //
                    // There are two possibilities for a notification:
                    //
                    // 1. The thread pool has been destroyed.
                    //
                    // 2. A work item has been scheduled.
                    //

                    if(_finished.Count > 0)
                    {
                        handler = _finished.First.Value;
                        _finished.RemoveFirst();
                    }
                    else if(_workItems.Count > 0)
                    {
                        //
                        // Work items must be executed first even if the thread pool is destroyed.
                        //
                        workItem = _workItems.First.Value;
                        _workItems.RemoveFirst();
                    }
                    else
                    {
                        Debug.Assert(_destroyed);
                        return;
                    }

                    if(_sizeMax > 1)
                    {
                        Debug.Assert(_inUse >= 0);
                        ++_inUse;

                        if(_inUse == _sizeWarn)
                        {
                            string s = "thread pool `" + _prefix + "' is running low on threads\n"
                                       + "Size=" + _size + ", " + "SizeMax=" + _sizeMax + ", "
                                       + "SizeWarn=" + _sizeWarn;
                            _instance.initializationData().logger.warning(s);
                        }

                        Debug.Assert(_inUse <= _running);
                        if(_inUse < _sizeMax && _inUse == _running)
                        {
                            try
                            {
                                WorkerThread thread = new WorkerThread(this, _programNamePrefix + _prefix + "-" +
                                                                       _threadIndex++);
                                thread.Start();
                                _threads.Add(thread);
                                ++_running;
                            }
                            catch(System.Exception ex)
                            {
                                string s = "cannot create thread for `" + _prefix + "':\n" + ex;
                                _instance.initializationData().logger.error(s);
                            }
                        }
                    }
                }

                //
                // Now we are outside the thread synchronization.
                //
                if(workItem != null)
                {
                    try
                    {
                        //
                        // Execute the work item and indicate whether this thread is safe (i.e., will not be reaped).
                        //
                        workItem(_size == _sizeMax);
                    }
                    catch(Ice.LocalException ex)
                    {
                        string s = "exception in `" + _prefix + "' while calling execute():\n" + ex;
                        _instance.initializationData().logger.error(s);
                    }
                }
                else 
                {
                    Debug.Assert(handler != null);
                    try
                    {
                        //
                        // Execute the work item and indicate whether this thread is safe (i.e., will not be reaped).
                        //
                        handler.finished(this);
                    }
                    catch(Ice.LocalException ex)
                    {
                        string s = "exception in `" + _prefix + "' while calling finished():\n" + ex;
                        _instance.initializationData().logger.error(s);
                    }
                }
                
                if(_sizeMax > 1)
                {
                    lock(this)
                    {
                        if(!_destroyed)
                        {
                            //
                            // First we reap threads that have been destroyed before.
                            //
                            int sz = _threads.Count;
                            Debug.Assert(_running <= sz);
                            if(_running < sz)
                            {
                                List<WorkerThread> liveThreads = new List<WorkerThread>();
                                foreach(WorkerThread thread in _threads)
                                {
                                    if(!thread.IsAlive())
                                    {
                                        thread.Join();
                                    }
                                    else
                                    {
                                        liveThreads.Add(thread);
                                    }
                                }
                                _threads = liveThreads;
                            }

                            //
                            // Now we check if this thread can be destroyed, based
                            // on a load factor.
                            //

                            //
                            // The load factor jumps immediately to the number of
                            // threads that are currently in use, but decays
                            // exponentially if the number of threads in use is
                            // smaller than the load factor. This reflects that we
                            // create threads immediately when they are needed,
                            // but want the number of threads to slowly decline to
                            // the configured minimum.
                            //
                            double inUse = (double)_inUse;
                            if(_load < inUse)
                            {
                                _load = inUse;
                            }
                            else
                            {
                                double loadFactor = 0.05; // TODO: Configurable?
                                double oneMinusLoadFactor = 1 - loadFactor;
                                _load = _load * oneMinusLoadFactor + inUse * loadFactor;
                            }

                            if(_running > _size)
                            {
                                int load = (int)(_load + 0.5);
                                if(load + 1 < _running)
                                {
                                    Debug.Assert(_inUse > 0);
                                    --_inUse;

                                    Debug.Assert(_running > 0);
                                    --_running;
                                    return;
                                }
                            }

                            Debug.Assert(_inUse > 0);
                            --_inUse;
                        }
                    }

                    #if TRACE_THREAD
                        trace("thread " + Thread.CurrentThread.Name + " is active");
                    #endif
                }
            }
        }

/*
 * Commented out because it is unused.
 *
        private void trace(string msg)
        {
            System.Console.Error.WriteLine(_prefix + "(" + Thread.CurrentThread.Name + "): " + msg);
        }
*/

        private Instance _instance;
        private bool _destroyed;
        private readonly string _prefix;
        private readonly string _programNamePrefix;

        private LinkedList<ThreadPoolWorkItem> _workItems = new LinkedList<ThreadPoolWorkItem>();
        private LinkedList<Ice.ConnectionI> _finished = new LinkedList<Ice.ConnectionI>();

        private int _timeout;

        private sealed class WorkerThread
        {
            private ThreadPool _threadPool;

            internal WorkerThread(ThreadPool threadPool, string name)
                : base()
            {
                _threadPool = threadPool;
                _name = name;
            }

            public bool IsAlive()
            {
                return _thread.IsAlive;
            }

            public void Join()
            {
                _thread.Join();
            }

            public void Start()
            {
                _thread = new Thread(new ThreadStart(Run));
                _thread.IsBackground = true;
                _thread.Name = _name;
                _thread.Start();
            }

            public void Run()
            {
                if(_threadPool._instance.initializationData().threadHook != null)
                {
                    _threadPool._instance.initializationData().threadHook.start();
                }

                try
                {
                    _threadPool.run();
                }
                catch(Ice.LocalException ex)
                {
                    string s = "exception in `" + _threadPool._prefix + "' thread " + _thread.Name + ":\n" + ex;
                    _threadPool._instance.initializationData().logger.error(s);
                }
                catch(System.Exception ex)
                {
                    string s = "unknown exception in `" + _threadPool._prefix + "' thread " + _thread.Name + ":\n" + ex;
                    _threadPool._instance.initializationData().logger.error(s);
                }

                if(_threadPool._instance.initializationData().threadHook != null)
                {
                    _threadPool._instance.initializationData().threadHook.stop();
                }

                #if TRACE_THREAD
                    _threadPool.trace("run() terminated");
                #endif
            }

            private string _name;
            private Thread _thread;
        }

        private readonly int _size; // Number of threads that are pre-created.
        private readonly int _sizeMax; // Maximum number of threads.
        private readonly int _sizeWarn; // If _inUse reaches _sizeWarn, a "low on threads" warning will be printed.
        private readonly bool _serialize; // True if requests need to be serialized over the connection.

        private List<WorkerThread> _threads; // All threads, running or not.
        private int _threadIndex; // For assigning thread names.
        private int _running; // Number of running threads.
        private int _inUse; // Number of threads that are currently in use.
        private double _load; // Current load in number of threads.
    }

}