Update wrt-plugins-common_0.3.53
[framework/web/wrt-plugins-common.git] / src / Commons / ThreadPool.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /*
17  * ControllersFactory.cpp
18  *
19  *  Created on: 2010-06-28
20  *      Author: kmajewski
21  */
22
23 #include <assert.h>
24
25 #include <dpl/log/log.h>
26 #include <dpl/thread.h>
27
28 #include "ThreadPool.h"
29 #include "Exception.h"
30
31 namespace WrtDeviceApis {
32 namespace Commons {
33
34 ThreadPool &ThreadPool::getInstance()
35 {
36     static ThreadPool theInstance;
37     return theInstance;
38 }
39
40 DPL::Event::AbstractEventDispatcher *ThreadPool::getDispatcher(
41         DPL::Event::ThreadEventDispatcher &callingThreadDispatcher)
42 {
43     DPL::Thread *currentThread = DPL::Thread::GetCurrentThread();
44     //if we are in main thread
45     if (NULL == currentThread) {
46         return &DPL::Event::GetMainEventDispatcherInstance();
47     }
48     callingThreadDispatcher.SetThread(currentThread);
49     return &callingThreadDispatcher;
50 }
51
52 ThreadPool::ThreadPool()
53 {
54 }
55
56 ThreadPool::~ThreadPool()
57 {
58     for (ThreadHandleMap::const_iterator it = m_threadHandlers.begin(); it
59          != m_threadHandlers.end(); ++it)
60     {
61         delete (*it).second;
62     }
63 }
64
65 DPL::Thread* ThreadPool::getThreadHandleCreateIfNotExists(
66         ThreadEnum::Enumeration type)
67 {
68     DPL::Mutex::ScopedLock lock(&m_threadHandlersMutex);
69     ThreadHandleMap::iterator element = m_threadHandlers.find(type);
70
71     //if element does not exists in the map
72     if (m_threadHandlers.end() == element) {
73         DPL::Thread* newThread = new DPL::Thread();
74         m_threadHandlers.insert(
75             m_threadHandlers.begin(),
76             std::pair<ThreadEnum::Enumeration,DPL::Thread*>(type, newThread));
77         newThread->Run();
78         return newThread;
79     }
80     return (*element).second;
81 }
82
83 DPL::Thread *ThreadPool::getThreadRef(ThreadEnum::Enumeration type)
84 {
85     DPL::Thread *thandle = NULL;
86     if (type < 0 || type >= ThreadEnum::size) {
87         Throw(InvalidArgumentException);
88     }
89     /* we could stay with
90      * getThreadHandleCreateIfNotExists(type);
91      * but by switch we can attach actually one thread to more than one ThreadEnums
92      */
93     switch (type) {
94     case ThreadEnum::NULL_THREAD:
95         thandle = NULL;
96         break;
97     case ThreadEnum::CAMERA_THREAD:
98         thandle = getThreadHandleCreateIfNotExists(ThreadEnum::CAMERA_THREAD);
99         break;
100     case ThreadEnum::CALENDAR_THREAD:
101         thandle = getThreadHandleCreateIfNotExists(ThreadEnum::CALENDAR_THREAD);
102         break;
103     case ThreadEnum::TELEPHONY_THREAD:
104         thandle = getThreadHandleCreateIfNotExists(ThreadEnum::TELEPHONY_THREAD);
105         break;
106     case ThreadEnum::APPLAUNCHER_THREAD:
107         thandle = getThreadHandleCreateIfNotExists(
108                 ThreadEnum::APPLAUNCHER_THREAD);
109         break;
110     case ThreadEnum::APPCONFIG_THREAD:
111         thandle = getThreadHandleCreateIfNotExists(ThreadEnum::APPCONFIG_THREAD);
112         break;
113     case ThreadEnum::MESSAGING_THREAD:
114         thandle = getThreadHandleCreateIfNotExists(type);
115         break;
116     case ThreadEnum::FILESYSTEM_THREAD:
117         thandle = getThreadHandleCreateIfNotExists(
118                 ThreadEnum::FILESYSTEM_THREAD);
119         break;
120     case ThreadEnum::GALLERY_THREAD:
121         thandle = getThreadHandleCreateIfNotExists(ThreadEnum::GALLERY_THREAD);
122         break;
123     case ThreadEnum::CONTACT_THREAD:
124         thandle = getThreadHandleCreateIfNotExists(ThreadEnum::CONTACT_THREAD);
125         break;
126     case ThreadEnum::MMPLAYER_THREAD:
127         thandle = getThreadHandleCreateIfNotExists(ThreadEnum::MMPLAYER_THREAD);
128         break;
129     case ThreadEnum::BONDI_THREAD:
130         thandle = getThreadHandleCreateIfNotExists(ThreadEnum::BONDI_THREAD);
131         break;
132     case ThreadEnum::GEOLOCATION_THREAD:
133         thandle = getThreadHandleCreateIfNotExists(
134                 ThreadEnum::GEOLOCATION_THREAD);
135         break;
136     case ThreadEnum::DEVICESTATUS_THREAD:
137         thandle = getThreadHandleCreateIfNotExists(
138                 ThreadEnum::DEVICESTATUS_THREAD);
139         break;
140     case ThreadEnum::PROFILE_THREAD:
141         thandle = getThreadHandleCreateIfNotExists(type);
142         break;
143     case ThreadEnum::HAPTICS_THREAD:
144         thandle = getThreadHandleCreateIfNotExists(type);
145         break;
146     case ThreadEnum::ACCELEROMETER_THREAD:
147         thandle = getThreadHandleCreateIfNotExists(
148                 ThreadEnum::ACCELEROMETER_THREAD);
149         break;
150     case ThreadEnum::ORIENTATION_THREAD:
151         thandle = getThreadHandleCreateIfNotExists(
152                 ThreadEnum::ORIENTATION_THREAD);
153         break;
154     case ThreadEnum::TASK_THREAD:
155         thandle = getThreadHandleCreateIfNotExists(ThreadEnum::TASK_THREAD);
156         break;
157     case ThreadEnum::POWER_THREAD:
158         thandle = getThreadHandleCreateIfNotExists(ThreadEnum::POWER_THREAD);
159         break;
160     case ThreadEnum::PLUGIN_TEMPLETE_THREAD:
161         thandle = getThreadHandleCreateIfNotExists(ThreadEnum::PLUGIN_TEMPLETE_THREAD);
162         break;
163     case ThreadEnum::BLUETOOTH_THREAD:
164         thandle = getThreadHandleCreateIfNotExists(ThreadEnum::BLUETOOTH_THREAD);
165         break;
166     case ThreadEnum::APPLICATION_THREAD:
167         thandle = getThreadHandleCreateIfNotExists(ThreadEnum::APPLICATION_THREAD);
168         break;
169     case ThreadEnum::GYROSCOPE_THREAD:
170         thandle = getThreadHandleCreateIfNotExists(ThreadEnum::GYROSCOPE_THREAD);
171         break;
172     case ThreadEnum::CLOCK_THREAD:
173         thandle = getThreadHandleCreateIfNotExists(ThreadEnum::CLOCK_THREAD);
174         break;
175     case ThreadEnum::SYSTEMINFO_THREAD:
176         thandle = getThreadHandleCreateIfNotExists(ThreadEnum::SYSTEMINFO_THREAD);
177         break;
178     case ThreadEnum::CALLHISTORY_THREAD:
179         thandle = getThreadHandleCreateIfNotExists(ThreadEnum::CALLHISTORY_THREAD);
180         break;
181     case ThreadEnum::ACCOUNT_THREAD:
182         thandle = getThreadHandleCreateIfNotExists(ThreadEnum::CALLHISTORY_THREAD);
183         break;
184     case ThreadEnum::NFC_THREAD:
185         thandle = getThreadHandleCreateIfNotExists(ThreadEnum::NFC_THREAD);
186         break;
187     case ThreadEnum::MEDIACONTENT_THREAD:
188         thandle = getThreadHandleCreateIfNotExists(ThreadEnum::MEDIACONTENT_THREAD);
189         break;
190     case ThreadEnum::SE_THREAD:
191         thandle = getThreadHandleCreateIfNotExists(ThreadEnum::SE_THREAD);
192         break;
193     // .....
194     default:
195         LogError("no case statement for ThreadEnum");
196         Throw(InvalidArgumentException);
197     }
198     return thandle;
199 }
200
201 }
202 } // WrtDeviceApisCommon