1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
20 #ifndef _ThreadManager_H_
21 #define _ThreadManager_H_
23 #include "PlatformLayer.h"
25 static const OID OID_IThreadClient = { 0x6f379191, 0xd9f1, 0x480c, { 0xac, 0x23, 0xb2, 0x88, 0x9e, 0x69, 0x28, 0x7b } };
27 * @class IThreadClient
28 * @brief IThreadClient Interface
29 * This class represents thread client that called from worker thread.\n
30 * The application who wants to use worker thread, inherit this interface
34 class IThreadClient : public IBase
39 * @brief Called when worker thread is ready execute message
41 * @param [in] void *pArg - Optional argument
48 virtual void onExecute(void *pArg) = 0;
52 * @brief Called when worker thread has finished last executed message
54 * @param [in] void *pArg - Optional argument
61 virtual void onTerminate(void *pArg) = 0;
62 virtual ~IThreadClient() {};
66 * @class CObjectMultiThreadModel
67 * @brief CObjectMultiThreadModel Interface
68 * This class represents declared CObject's threading model
72 class CObjectMultiThreadModel
77 * @brief Called when reference counter need to increase
79 * @param [in] volatile unsigned long *p - Reference counter's pointer
81 * @return unsigned long
86 static unsigned long increment(volatile unsigned long *p)
89 return InterlockedIncrement(p);
91 return __sync_add_and_fetch(p, 1);
93 #error WIN32 or LINUX tag must be defined
99 * @brief Called when reference counter need to decrease
101 * @param [in] volatile unsigned long *p - Reference counter's pointer
103 * @return unsigned long
108 static unsigned long decrement(volatile unsigned long *p)
111 return InterlockedDecrement(p);
113 return __sync_add_and_fetch(p, -1);
115 #error WIN32 or LINUX tag must be defined
122 * @brief CNullMutex Interface
123 * This class represents mutex strategy
132 * @brief Called when current thread need to locked
145 * @brief Called when current thread need to unlocked
159 * @brief CAutoMutex Interface
160 * This class represents automated mutex class
183 * @class CSimpleMutex
184 * @brief CSimpleMutex Interface
185 * This class represents basic mutex class.\n
186 * This mutex must support recursive locking mechanism
194 CRITICAL_SECTION m_criticalSection;
196 pthread_mutex_t m_mutex;
197 pthread_mutexattr_t m_mutexAttribute;
199 #error WIN32 or LINUX tag must be defined
209 * @brief Lock current thread
222 * @brief Unlock current thread
242 #error WIN32 or LINUX tag must be defined
250 SSMRESULT initialize();
260 static const OID OID_IWorkerThread = { 0x12a67457, 0xce97, 0x41c4, { 0x90, 0xb8, 0xcc, 0x72, 0x5f, 0xc4, 0x40, 0x27 } };
262 * @class IWorkerThread
263 * @brief IWorkerThread Interface
264 * This class represents worker thread utility
268 class IWorkerThread : public IBase
273 * @brief Initialize current worker thread
282 virtual SSMRESULT initialize() = 0;
286 * @brief Terminate current worker thread.\n
287 * All remained tasks are destroyed after IThreadClient::OnTerminate called
296 virtual SSMRESULT terminate() = 0;
300 * @brief Add task to current worker thread.\n
301 * Each task called only once
303 * @param [in] IThreadClient *pThreadClient - Implemented thread client class instance that called from worker thread.
304 * @param [in] void *param - Optional parameter that IThreadClient::OnExecute/OnTerminate can receive
311 virtual SSMRESULT addTask(IThreadClient *pThreadClient, void *param) = 0;
315 * @class CWorkerThread
316 * @brief CWorkerThread Interface
317 * This class represents worker thread utility that holds one thread
321 class CWorkerThread :
322 public CObjectRoot<CObjectMultiThreadModel>
323 , public IWorkerThread
328 IThreadClient *pClient;
336 #error WIN32 or LINUX tag must be defined
338 CSemaphore m_semInit;
339 CSemaphore m_semTerm;
341 CSimpleMutex m_mtxClientEntry;
342 CSimpleMutex m_mtxThreadTerm;
343 CSemaphore m_semTask;
347 std::list<ClientEntry> m_ClientEntry;
349 bool getTask(ClientEntry *clientEntry);
353 static void _worker(void *pArg)
355 CWorkerThread *pThread = (CWorkerThread *)pArg;
356 return pThread->worker();
360 SSMRESULT finalConstruct();
363 SSMRESULT queryInterface(const OID &objectID, IBase **ppObject)
365 if (ppObject == NULL)
366 return SSM_E_POINTER;
368 if (IsEqualOID(objectID, OID_IWorkerThread))
370 IBase *pBase = (IWorkerThread *)this;
376 return SSM_E_NOINTERFACE;
381 * @brief Initialize current worker thread
390 SSMRESULT initialize();
394 * @brief Terminate current worker thread.\n
395 * All remained tasks are destroyed after IThreadClient::OnTerminate called
404 SSMRESULT terminate();
408 * @brief Add task to current worker thread.\n
409 * Each task called only once
411 * @param [in] IThreadClient *pThreadClient - Implemented thread client class instance that called from worker thread.
412 * @param [in] void *param - Optional parameter that IThreadClient::OnExecute/OnTerminate can receive
419 SSMRESULT addTask(IThreadClient *pThreadClient, void *param);
423 static const OID OID_IThreadPool = { 0x53855605, 0xb2a0, 0x4a31, { 0xa2, 0x60, 0x36, 0x4d, 0x43, 0xc1, 0x2f, 0x96 } };
426 * @brief IThreadPool Interface
427 * This class represents worker thread utility
431 class IThreadPool : public IBase
434 virtual SSMRESULT createWorkerThread(IWorkerThread **ppWorkerThread) = 0;
435 virtual SSMRESULT destroyThreadPool() = 0;
440 * @brief CThreadPool Interface
441 * This class represents worker thread pool utility that can holds multiple thread
446 public CObjectRoot<CObjectMultiThreadModel>
450 std::vector<IWorkerThread *> m_lstWorkerThread;
453 SSMRESULT finalConstruct();
456 SSMRESULT queryInterface(const OID &objectID, IBase **ppObject)
458 if (ppObject == NULL)
459 return SSM_E_POINTER;
461 if (IsEqualOID(objectID, OID_IThreadPool))
469 return SSM_E_NOINTERFACE;
472 SSMRESULT createWorkerThread(IWorkerThread **ppWorkerThread);
474 SSMRESULT destroyThreadPool();
477 static const OID OID_ITasker = { 0x8f2b0b4f, 0xaab8, 0x4cbc, { 0x99, 0x1, 0x65, 0xde, 0x89, 0x82, 0x9f, 0x7a } };
480 * @brief ITasker Interface
481 * This class represents worker thread utility
485 class ITasker : public IBase
488 virtual SSMRESULT addTask(IThreadClient *pThreadClient, void *param) = 0;
492 public CObjectRoot<CObjectMultiThreadModel>
496 CObjectPtr<IThreadPool> m_pThreadPool;
497 CObjectPtr<IWorkerThread> m_pWorkerThread;
500 SSMRESULT finalConstruct();
503 SSMRESULT queryInterface(const OID &objectID, IBase **ppObject)
505 if (ppObject == NULL)
506 return SSM_E_POINTER;
508 if (IsEqualOID(objectID, OID_ITasker))
516 return SSM_E_NOINTERFACE;
519 SSMRESULT addTask(IThreadClient *pThreadClient, void *param);