1 /*=============================================================================
2 
3   Library: CTK
4 
5   Copyright (c) German Cancer Research Center,
6     Division of Medical and Biological Informatics
7 
8   Licensed under the Apache License, Version 2.0 (the "License");
9   you may not use this file except in compliance with the License.
10   You may obtain a copy of the License at
11 
12     http://www.apache.org/licenses/LICENSE-2.0
13 
14   Unless required by applicable law or agreed to in writing, software
15   distributed under the License is distributed on an "AS IS" BASIS,
16   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   See the License for the specific language governing permissions and
18   limitations under the License.
19 
20 =============================================================================*/
21 
22 
23 #include "ctkEADefaultThreadPool_p.h"
24 
25 #include "ctkEALinkedQueue_p.h"
26 #include "ctkEAInterruptedException_p.h"
27 
28 #include <ctkEventAdminActivator_p.h>
29 #include <tasks/ctkEASyncThread_p.h>
30 
31 struct _SyncThreadFactory : public ctkEAThreadFactory
32 {
newThread_SyncThreadFactory33   ctkEAInterruptibleThread* newThread(ctkEARunnable* command)
34   {
35     ctkEAInterruptibleThread* thread = new ctkEASyncThread(command);
36     //thread->setPriority(QThread::NormalPriority);
37     //thread->setDaemon(true);
38 
39     return thread;
40   }
41 };
42 
43 struct _AsyncThreadFactory : public ctkEAThreadFactory
44 {
newThread_AsyncThreadFactory45   ctkEAInterruptibleThread* newThread(ctkEARunnable* command)
46   {
47     ctkEAInterruptibleThread* thread = new ctkEAInterruptibleThread(command);
48     //thread->setPriority(QThread::NormalPriority);
49     //thread->setDaemon(false);
50 
51     return thread;
52   }
53 };
54 
ctkEADefaultThreadPool(int poolSize,bool syncThreads)55 ctkEADefaultThreadPool::ctkEADefaultThreadPool(int poolSize, bool syncThreads)
56   : ctkEAPooledExecutor(new ctkEALinkedQueue())
57 {
58   if (syncThreads)
59   {
60     delete this->setThreadFactory(new _SyncThreadFactory());
61   }
62   else
63   {
64     delete this->setThreadFactory(new _AsyncThreadFactory());
65   }
66 
67   configure(poolSize);
68   setKeepAliveTime(60000);
69   runWhenBlocked();
70 }
71 
configure(int poolSize)72 void ctkEADefaultThreadPool::configure(int poolSize)
73 {
74   setMinimumPoolSize(poolSize);
75   setMaximumPoolSize(poolSize + 10);
76 }
77 
close()78 void ctkEADefaultThreadPool::close()
79 {
80   shutdownNow();
81 
82   try
83   {
84     awaitTerminationAfterShutdown();
85   }
86   catch (const ctkEAInterruptedException& )
87   {
88     // ignore this
89   }
90 }
91 
executeTask(ctkEARunnable * task)92 void ctkEADefaultThreadPool::executeTask(ctkEARunnable* task)
93 {
94   try
95   {
96     this->execute(task);
97   }
98   catch (const std::exception& e)
99   {
100     CTK_WARN_EXC(ctkEventAdminActivator::getLogService(), &e)
101         << "Exception: " << e.what();
102     // ignore this
103   }
104 }
105