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 {
33 ThreadPool &ThreadPool::getInstance()
35 static ThreadPool theInstance;
39 DPL::Event::AbstractEventDispatcher *ThreadPool::getDispatcher(
40 DPL::Event::ThreadEventDispatcher &callingThreadDispatcher)
42 DPL::Thread *currentThread = DPL::Thread::GetCurrentThread();
43 //if we are in main thread
44 if (NULL == currentThread) {
45 return &DPL::Event::GetMainEventDispatcherInstance();
47 callingThreadDispatcher.SetThread(currentThread);
48 return &callingThreadDispatcher;
51 ThreadPool::ThreadPool()
54 ThreadPool::~ThreadPool()
56 for (ThreadHandleMap::const_iterator it = m_threadHandlers.begin(); it
57 != m_threadHandlers.end(); ++it)
63 DPL::Thread* ThreadPool::getThreadHandleCreateIfNotExists(
64 ThreadEnum::Enumeration type)
66 DPL::Mutex::ScopedLock lock(&m_threadHandlersMutex);
67 ThreadHandleMap::iterator element = m_threadHandlers.find(type);
69 //if element does not exists in the map
70 if (m_threadHandlers.end() == element) {
71 DPL::Thread* newThread = new DPL::Thread();
72 m_threadHandlers.insert(
73 m_threadHandlers.begin(),
74 std::pair<ThreadEnum::Enumeration, DPL::Thread*>(type, newThread));
78 return (*element).second;
81 DPL::Thread *ThreadPool::getThreadRef(ThreadEnum::Enumeration type)
83 DPL::Thread *thandle = NULL;
84 if (type < 0 || type >= ThreadEnum::size) {
85 Throw(InvalidArgumentException);
88 * getThreadHandleCreateIfNotExists(type);
89 * but by switch we can attach actually one thread to more than one
93 case ThreadEnum::NULL_THREAD:
96 case ThreadEnum::CAMERA_THREAD:
97 thandle = getThreadHandleCreateIfNotExists(ThreadEnum::CAMERA_THREAD);
99 case ThreadEnum::CALENDAR_THREAD:
100 thandle = getThreadHandleCreateIfNotExists(ThreadEnum::CALENDAR_THREAD);
102 case ThreadEnum::TELEPHONY_THREAD:
103 thandle = getThreadHandleCreateIfNotExists(ThreadEnum::TELEPHONY_THREAD);
105 case ThreadEnum::APPLAUNCHER_THREAD:
106 thandle = getThreadHandleCreateIfNotExists(
107 ThreadEnum::APPLAUNCHER_THREAD);
109 case ThreadEnum::APPCONFIG_THREAD:
110 thandle = getThreadHandleCreateIfNotExists(ThreadEnum::APPCONFIG_THREAD);
112 case ThreadEnum::MESSAGING_THREAD:
113 thandle = getThreadHandleCreateIfNotExists(type);
115 case ThreadEnum::FILESYSTEM_THREAD:
116 thandle = getThreadHandleCreateIfNotExists(
117 ThreadEnum::FILESYSTEM_THREAD);
119 case ThreadEnum::GALLERY_THREAD:
120 thandle = getThreadHandleCreateIfNotExists(ThreadEnum::GALLERY_THREAD);
122 case ThreadEnum::CONTACT_THREAD:
123 thandle = getThreadHandleCreateIfNotExists(ThreadEnum::CONTACT_THREAD);
125 case ThreadEnum::BONDI_THREAD:
126 thandle = getThreadHandleCreateIfNotExists(ThreadEnum::BONDI_THREAD);
128 case ThreadEnum::GEOLOCATION_THREAD:
129 thandle = getThreadHandleCreateIfNotExists(
130 ThreadEnum::GEOLOCATION_THREAD);
132 case ThreadEnum::DEVICESTATUS_THREAD:
133 thandle = getThreadHandleCreateIfNotExists(
134 ThreadEnum::DEVICESTATUS_THREAD);
136 case ThreadEnum::PROFILE_THREAD:
137 thandle = getThreadHandleCreateIfNotExists(type);
139 case ThreadEnum::HAPTICS_THREAD:
140 thandle = getThreadHandleCreateIfNotExists(type);
142 case ThreadEnum::ACCELEROMETER_THREAD:
143 thandle = getThreadHandleCreateIfNotExists(
144 ThreadEnum::ACCELEROMETER_THREAD);
146 case ThreadEnum::ORIENTATION_THREAD:
147 thandle = getThreadHandleCreateIfNotExists(
148 ThreadEnum::ORIENTATION_THREAD);
150 case ThreadEnum::TASK_THREAD:
151 thandle = getThreadHandleCreateIfNotExists(ThreadEnum::TASK_THREAD);
153 case ThreadEnum::POWER_THREAD:
154 thandle = getThreadHandleCreateIfNotExists(ThreadEnum::POWER_THREAD);
156 case ThreadEnum::PLUGIN_TEMPLETE_THREAD:
157 thandle = getThreadHandleCreateIfNotExists(
158 ThreadEnum::PLUGIN_TEMPLETE_THREAD);
160 case ThreadEnum::BLUETOOTH_THREAD:
161 thandle = getThreadHandleCreateIfNotExists(ThreadEnum::BLUETOOTH_THREAD);
163 case ThreadEnum::APPLICATION_THREAD:
164 thandle = getThreadHandleCreateIfNotExists(
165 ThreadEnum::APPLICATION_THREAD);
167 case ThreadEnum::GYROSCOPE_THREAD:
168 thandle = getThreadHandleCreateIfNotExists(ThreadEnum::GYROSCOPE_THREAD);
170 case ThreadEnum::CLOCK_THREAD:
171 thandle = getThreadHandleCreateIfNotExists(ThreadEnum::CLOCK_THREAD);
173 case ThreadEnum::SYSTEMINFO_THREAD:
174 thandle = getThreadHandleCreateIfNotExists(
175 ThreadEnum::SYSTEMINFO_THREAD);
177 case ThreadEnum::CALLHISTORY_THREAD:
178 thandle = getThreadHandleCreateIfNotExists(
179 ThreadEnum::CALLHISTORY_THREAD);
181 case ThreadEnum::ACCOUNT_THREAD:
182 thandle = getThreadHandleCreateIfNotExists(
183 ThreadEnum::CALLHISTORY_THREAD);
185 case ThreadEnum::NFC_THREAD:
186 thandle = getThreadHandleCreateIfNotExists(ThreadEnum::NFC_THREAD);
188 case ThreadEnum::MEDIACONTENT_THREAD:
189 thandle = getThreadHandleCreateIfNotExists(
190 ThreadEnum::MEDIACONTENT_THREAD);
192 case ThreadEnum::SE_THREAD:
193 thandle = getThreadHandleCreateIfNotExists(ThreadEnum::SE_THREAD);
195 case ThreadEnum::DOWNLOAD_THREAD:
196 thandle = getThreadHandleCreateIfNotExists(ThreadEnum::DOWNLOAD_THREAD);
198 case ThreadEnum::PUSH_THREAD:
199 thandle = getThreadHandleCreateIfNotExists(ThreadEnum::PUSH_THREAD);
201 case ThreadEnum::SYNC_THREAD:
202 thandle = getThreadHandleCreateIfNotExists(ThreadEnum::SYNC_THREAD);
206 LogError("no case statement for ThreadEnum");
207 Throw(InvalidArgumentException);
212 } // WrtDeviceApisCommon