2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * ControllersFactory.cpp
19 * Created on: 2010-06-28
25 #include <dpl/log/log.h>
26 #include <dpl/thread.h>
28 #include "ThreadPool.h"
29 #include "Exception.h"
31 namespace WrtDeviceApis {
34 ThreadPool &ThreadPool::getInstance()
36 static ThreadPool theInstance;
40 DPL::Event::AbstractEventDispatcher *ThreadPool::getDispatcher(
41 DPL::Event::ThreadEventDispatcher &callingThreadDispatcher)
43 DPL::Thread *currentThread = DPL::Thread::GetCurrentThread();
44 //if we are in main thread
45 if (NULL == currentThread) {
46 return &DPL::Event::GetMainEventDispatcherInstance();
48 callingThreadDispatcher.SetThread(currentThread);
49 return &callingThreadDispatcher;
52 ThreadPool::ThreadPool()
56 ThreadPool::~ThreadPool()
58 for (ThreadHandleMap::const_iterator it = m_threadHandlers.begin(); it
59 != m_threadHandlers.end(); ++it)
65 DPL::Thread* ThreadPool::getThreadHandleCreateIfNotExists(
66 ThreadEnum::Enumeration type)
68 DPL::Mutex::ScopedLock lock(&m_threadHandlersMutex);
69 ThreadHandleMap::iterator element = m_threadHandlers.find(type);
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));
80 return (*element).second;
83 DPL::Thread *ThreadPool::getThreadRef(ThreadEnum::Enumeration type)
85 DPL::Thread *thandle = NULL;
86 if (type < 0 || type >= ThreadEnum::size) {
87 Throw(InvalidArgumentException);
90 * getThreadHandleCreateIfNotExists(type);
91 * but by switch we can attach actually one thread to more than one ThreadEnums
94 case ThreadEnum::NULL_THREAD:
97 case ThreadEnum::CAMERA_THREAD:
98 thandle = getThreadHandleCreateIfNotExists(ThreadEnum::CAMERA_THREAD);
100 case ThreadEnum::CALENDAR_THREAD:
101 thandle = getThreadHandleCreateIfNotExists(ThreadEnum::CALENDAR_THREAD);
103 case ThreadEnum::TELEPHONY_THREAD:
104 thandle = getThreadHandleCreateIfNotExists(ThreadEnum::TELEPHONY_THREAD);
106 case ThreadEnum::APPLAUNCHER_THREAD:
107 thandle = getThreadHandleCreateIfNotExists(
108 ThreadEnum::APPLAUNCHER_THREAD);
110 case ThreadEnum::APPCONFIG_THREAD:
111 thandle = getThreadHandleCreateIfNotExists(ThreadEnum::APPCONFIG_THREAD);
113 case ThreadEnum::MESSAGING_THREAD:
114 thandle = getThreadHandleCreateIfNotExists(type);
116 case ThreadEnum::FILESYSTEM_THREAD:
117 thandle = getThreadHandleCreateIfNotExists(
118 ThreadEnum::FILESYSTEM_THREAD);
120 case ThreadEnum::GALLERY_THREAD:
121 thandle = getThreadHandleCreateIfNotExists(ThreadEnum::GALLERY_THREAD);
123 case ThreadEnum::CONTACT_THREAD:
124 thandle = getThreadHandleCreateIfNotExists(ThreadEnum::CONTACT_THREAD);
126 case ThreadEnum::MMPLAYER_THREAD:
127 thandle = getThreadHandleCreateIfNotExists(ThreadEnum::MMPLAYER_THREAD);
129 case ThreadEnum::BONDI_THREAD:
130 thandle = getThreadHandleCreateIfNotExists(ThreadEnum::BONDI_THREAD);
132 case ThreadEnum::GEOLOCATION_THREAD:
133 thandle = getThreadHandleCreateIfNotExists(
134 ThreadEnum::GEOLOCATION_THREAD);
136 case ThreadEnum::DEVICESTATUS_THREAD:
137 thandle = getThreadHandleCreateIfNotExists(
138 ThreadEnum::DEVICESTATUS_THREAD);
140 case ThreadEnum::PROFILE_THREAD:
141 thandle = getThreadHandleCreateIfNotExists(type);
143 case ThreadEnum::HAPTICS_THREAD:
144 thandle = getThreadHandleCreateIfNotExists(type);
146 case ThreadEnum::ACCELEROMETER_THREAD:
147 thandle = getThreadHandleCreateIfNotExists(
148 ThreadEnum::ACCELEROMETER_THREAD);
150 case ThreadEnum::ORIENTATION_THREAD:
151 thandle = getThreadHandleCreateIfNotExists(
152 ThreadEnum::ORIENTATION_THREAD);
154 case ThreadEnum::TASK_THREAD:
155 thandle = getThreadHandleCreateIfNotExists(ThreadEnum::TASK_THREAD);
157 case ThreadEnum::POWER_THREAD:
158 thandle = getThreadHandleCreateIfNotExists(ThreadEnum::POWER_THREAD);
160 case ThreadEnum::PLUGIN_TEMPLETE_THREAD:
161 thandle = getThreadHandleCreateIfNotExists(ThreadEnum::PLUGIN_TEMPLETE_THREAD);
163 case ThreadEnum::BLUETOOTH_THREAD:
164 thandle = getThreadHandleCreateIfNotExists(ThreadEnum::BLUETOOTH_THREAD);
166 case ThreadEnum::APPLICATION_THREAD:
167 thandle = getThreadHandleCreateIfNotExists(ThreadEnum::APPLICATION_THREAD);
169 case ThreadEnum::GYROSCOPE_THREAD:
170 thandle = getThreadHandleCreateIfNotExists(ThreadEnum::GYROSCOPE_THREAD);
172 case ThreadEnum::CLOCK_THREAD:
173 thandle = getThreadHandleCreateIfNotExists(ThreadEnum::CLOCK_THREAD);
175 case ThreadEnum::SYSTEMINFO_THREAD:
176 thandle = getThreadHandleCreateIfNotExists(ThreadEnum::SYSTEMINFO_THREAD);
178 case ThreadEnum::CALLHISTORY_THREAD:
179 thandle = getThreadHandleCreateIfNotExists(ThreadEnum::CALLHISTORY_THREAD);
181 case ThreadEnum::ACCOUNT_THREAD:
182 thandle = getThreadHandleCreateIfNotExists(ThreadEnum::CALLHISTORY_THREAD);
184 case ThreadEnum::NFC_THREAD:
185 thandle = getThreadHandleCreateIfNotExists(ThreadEnum::NFC_THREAD);
187 case ThreadEnum::MEDIACONTENT_THREAD:
188 thandle = getThreadHandleCreateIfNotExists(ThreadEnum::MEDIACONTENT_THREAD);
190 case ThreadEnum::SE_THREAD:
191 thandle = getThreadHandleCreateIfNotExists(ThreadEnum::SE_THREAD);
195 LogError("no case statement for ThreadEnum");
196 Throw(InvalidArgumentException);
202 } // WrtDeviceApisCommon