Initialize Tizen 2.3
[framework/web/wrt-plugins-common.git] / src_wearable / 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 ThreadPool &ThreadPool::getInstance()
34 {
35     static ThreadPool theInstance;
36     return theInstance;
37 }
38
39 DPL::Event::AbstractEventDispatcher *ThreadPool::getDispatcher(
40     DPL::Event::ThreadEventDispatcher &callingThreadDispatcher)
41 {
42     DPL::Thread *currentThread = DPL::Thread::GetCurrentThread();
43     //if we are in main thread
44     if (NULL == currentThread) {
45         return &DPL::Event::GetMainEventDispatcherInstance();
46     }
47     callingThreadDispatcher.SetThread(currentThread);
48     return &callingThreadDispatcher;
49 }
50
51 ThreadPool::ThreadPool()
52 {}
53
54 ThreadPool::~ThreadPool()
55 {
56     for (ThreadHandleMap::const_iterator it = m_threadHandlers.begin(); it
57          != m_threadHandlers.end(); ++it)
58     {
59         delete (*it).second;
60     }
61 }
62
63 DPL::Thread* ThreadPool::getThreadHandleCreateIfNotExists(
64     ThreadEnum::Enumeration type)
65 {
66     DPL::Mutex::ScopedLock lock(&m_threadHandlersMutex);
67     ThreadHandleMap::iterator element = m_threadHandlers.find(type);
68
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));
75         newThread->Run();
76         return newThread;
77     }
78     return (*element).second;
79 }
80
81 DPL::Thread *ThreadPool::getThreadRef(ThreadEnum::Enumeration type)
82 {
83     DPL::Thread *thandle = NULL;
84     if (type < 0 || type >= ThreadEnum::size) {
85         Throw(InvalidArgumentException);
86     }
87     /* we could stay with
88      * getThreadHandleCreateIfNotExists(type);
89      * but by switch we can attach actually one thread to more than one
90      * ThreadEnums
91      */
92     switch (type) {
93     case ThreadEnum::NULL_THREAD:
94         thandle = NULL;
95         break;
96     case ThreadEnum::CAMERA_THREAD:
97         thandle = getThreadHandleCreateIfNotExists(ThreadEnum::CAMERA_THREAD);
98         break;
99     case ThreadEnum::CALENDAR_THREAD:
100         thandle = getThreadHandleCreateIfNotExists(ThreadEnum::CALENDAR_THREAD);
101         break;
102     case ThreadEnum::TELEPHONY_THREAD:
103         thandle = getThreadHandleCreateIfNotExists(ThreadEnum::TELEPHONY_THREAD);
104         break;
105     case ThreadEnum::APPLAUNCHER_THREAD:
106         thandle = getThreadHandleCreateIfNotExists(
107                 ThreadEnum::APPLAUNCHER_THREAD);
108         break;
109     case ThreadEnum::APPCONFIG_THREAD:
110         thandle = getThreadHandleCreateIfNotExists(ThreadEnum::APPCONFIG_THREAD);
111         break;
112     case ThreadEnum::MESSAGING_THREAD:
113         thandle = getThreadHandleCreateIfNotExists(type);
114         break;
115     case ThreadEnum::FILESYSTEM_THREAD:
116         thandle = getThreadHandleCreateIfNotExists(
117                 ThreadEnum::FILESYSTEM_THREAD);
118         break;
119     case ThreadEnum::GALLERY_THREAD:
120         thandle = getThreadHandleCreateIfNotExists(ThreadEnum::GALLERY_THREAD);
121         break;
122     case ThreadEnum::CONTACT_THREAD:
123         thandle = getThreadHandleCreateIfNotExists(ThreadEnum::CONTACT_THREAD);
124         break;
125     case ThreadEnum::BONDI_THREAD:
126         thandle = getThreadHandleCreateIfNotExists(ThreadEnum::BONDI_THREAD);
127         break;
128     case ThreadEnum::GEOLOCATION_THREAD:
129         thandle = getThreadHandleCreateIfNotExists(
130                 ThreadEnum::GEOLOCATION_THREAD);
131         break;
132     case ThreadEnum::DEVICESTATUS_THREAD:
133         thandle = getThreadHandleCreateIfNotExists(
134                 ThreadEnum::DEVICESTATUS_THREAD);
135         break;
136     case ThreadEnum::PROFILE_THREAD:
137         thandle = getThreadHandleCreateIfNotExists(type);
138         break;
139     case ThreadEnum::HAPTICS_THREAD:
140         thandle = getThreadHandleCreateIfNotExists(type);
141         break;
142     case ThreadEnum::ACCELEROMETER_THREAD:
143         thandle = getThreadHandleCreateIfNotExists(
144                 ThreadEnum::ACCELEROMETER_THREAD);
145         break;
146     case ThreadEnum::ORIENTATION_THREAD:
147         thandle = getThreadHandleCreateIfNotExists(
148                 ThreadEnum::ORIENTATION_THREAD);
149         break;
150     case ThreadEnum::TASK_THREAD:
151         thandle = getThreadHandleCreateIfNotExists(ThreadEnum::TASK_THREAD);
152         break;
153     case ThreadEnum::POWER_THREAD:
154         thandle = getThreadHandleCreateIfNotExists(ThreadEnum::POWER_THREAD);
155         break;
156     case ThreadEnum::PLUGIN_TEMPLETE_THREAD:
157         thandle = getThreadHandleCreateIfNotExists(
158                 ThreadEnum::PLUGIN_TEMPLETE_THREAD);
159         break;
160     case ThreadEnum::BLUETOOTH_THREAD:
161         thandle = getThreadHandleCreateIfNotExists(ThreadEnum::BLUETOOTH_THREAD);
162         break;
163     case ThreadEnum::APPLICATION_THREAD:
164         thandle = getThreadHandleCreateIfNotExists(
165                 ThreadEnum::APPLICATION_THREAD);
166         break;
167     case ThreadEnum::GYROSCOPE_THREAD:
168         thandle = getThreadHandleCreateIfNotExists(ThreadEnum::GYROSCOPE_THREAD);
169         break;
170     case ThreadEnum::CLOCK_THREAD:
171         thandle = getThreadHandleCreateIfNotExists(ThreadEnum::CLOCK_THREAD);
172         break;
173     case ThreadEnum::SYSTEMINFO_THREAD:
174         thandle = getThreadHandleCreateIfNotExists(
175                 ThreadEnum::SYSTEMINFO_THREAD);
176         break;
177     case ThreadEnum::CALLHISTORY_THREAD:
178         thandle = getThreadHandleCreateIfNotExists(
179                 ThreadEnum::CALLHISTORY_THREAD);
180         break;
181     case ThreadEnum::ACCOUNT_THREAD:
182         thandle = getThreadHandleCreateIfNotExists(
183                 ThreadEnum::CALLHISTORY_THREAD);
184         break;
185     case ThreadEnum::NFC_THREAD:
186         thandle = getThreadHandleCreateIfNotExists(ThreadEnum::NFC_THREAD);
187         break;
188     case ThreadEnum::MEDIACONTENT_THREAD:
189         thandle = getThreadHandleCreateIfNotExists(
190                 ThreadEnum::MEDIACONTENT_THREAD);
191         break;
192     case ThreadEnum::SE_THREAD:
193         thandle = getThreadHandleCreateIfNotExists(ThreadEnum::SE_THREAD);
194         break;
195     case ThreadEnum::DOWNLOAD_THREAD:
196         thandle = getThreadHandleCreateIfNotExists(ThreadEnum::DOWNLOAD_THREAD);
197         break;
198     case ThreadEnum::PUSH_THREAD:
199         thandle = getThreadHandleCreateIfNotExists(ThreadEnum::PUSH_THREAD);
200         break;
201     case ThreadEnum::SYNC_THREAD:
202         thandle = getThreadHandleCreateIfNotExists(ThreadEnum::SYNC_THREAD);
203         break;
204     // .....
205     default:
206         LogError("no case statement for ThreadEnum");
207         Throw(InvalidArgumentException);
208     }
209     return thandle;
210 }
211 }
212 } // WrtDeviceApisCommon