diff options
Diffstat (limited to 'source/api_wrappers/linux')
-rw-r--r-- | source/api_wrappers/linux/CCriticalSection.h | 52 | ||||
-rw-r--r-- | source/api_wrappers/linux/CEventObject.cpp | 66 | ||||
-rw-r--r-- | source/api_wrappers/linux/CEventObject.h | 40 | ||||
-rw-r--r-- | source/api_wrappers/linux/CSemaphore.cpp | 55 | ||||
-rw-r--r-- | source/api_wrappers/linux/CSemaphore.h | 26 | ||||
-rw-r--r-- | source/api_wrappers/linux/CSemaphoreQueue.cpp | 121 | ||||
-rw-r--r-- | source/api_wrappers/linux/CSemaphoreQueue.h | 67 | ||||
-rw-r--r-- | source/api_wrappers/linux/CSimpleQueue.h | 26 | ||||
-rw-r--r-- | source/api_wrappers/linux/CThreadWrapper.cpp | 119 | ||||
-rw-r--r-- | source/api_wrappers/linux/CThreadWrapper.h | 36 | ||||
-rw-r--r-- | source/api_wrappers/linux/CWaitableObject.cpp | 17 | ||||
-rw-r--r-- | source/api_wrappers/linux/CWaitableObject.h | 24 | ||||
-rw-r--r-- | source/api_wrappers/linux/CWaitableObjectCollection.cpp | 67 | ||||
-rw-r--r-- | source/api_wrappers/linux/CWaitableObjectCollection.h | 26 | ||||
-rw-r--r-- | source/api_wrappers/linux/LinuxApiWrappers.h | 20 | ||||
-rw-r--r-- | source/api_wrappers/linux/OS.cpp | 88 | ||||
-rw-r--r-- | source/api_wrappers/linux/OS.h | 107 | ||||
-rw-r--r-- | source/api_wrappers/linux/Types.h | 21 |
18 files changed, 978 insertions, 0 deletions
diff --git a/source/api_wrappers/linux/CCriticalSection.h b/source/api_wrappers/linux/CCriticalSection.h new file mode 100644 index 0000000..bbe95c4 --- /dev/null +++ b/source/api_wrappers/linux/CCriticalSection.h @@ -0,0 +1,52 @@ +/******************************************************************************* + * Copyright (C) ST-Ericsson SA 2011 + * License terms: 3-clause BSD license + ******************************************************************************/ + +#ifndef _CCRITICALSECTION_H +#define _CCRITICALSECTION_H + +#include "pthread.h" +class CCriticalSectionObject +{ + friend class CLockCS; +public: + CCriticalSectionObject() { + pthread_mutexattr_init(&recursivem); + pthread_mutexattr_settype(&recursivem, PTHREAD_MUTEX_RECURSIVE); + pthread_mutex_init(&m_CriticalSection, &recursivem); + } + ~CCriticalSectionObject() { + pthread_mutexattr_destroy(&recursivem); + pthread_mutex_destroy(&m_CriticalSection); + } + inline void Enter() { + pthread_mutex_lock(&m_CriticalSection); + } + inline void Leave() { + pthread_mutex_unlock(&m_CriticalSection); + } +private: + pthread_mutexattr_t recursivem; + mutable pthread_mutex_t m_CriticalSection; +}; + +// Basic lock class used to enter and leave the CRITICAL_SECTION private member of a +// CCriticalSection object. Create a CLock object in scope that needs to be +// synchronized and pass the shared CCriticalSection object used to synchronize the +// resource to protect. The destructor calls the Leave method when leaving scope. +class CLockCS +{ +public: + CLockCS(CCriticalSectionObject &cs) : m_CriticalSectionObject(cs) { + m_CriticalSectionObject.Enter(); + } + ~CLockCS() { + m_CriticalSectionObject.Leave(); + } +private: + CCriticalSectionObject &m_CriticalSectionObject; +}; + +#endif /* _CCRITICALSECTION_H */ + diff --git a/source/api_wrappers/linux/CEventObject.cpp b/source/api_wrappers/linux/CEventObject.cpp new file mode 100644 index 0000000..5f5acda --- /dev/null +++ b/source/api_wrappers/linux/CEventObject.cpp @@ -0,0 +1,66 @@ +/******************************************************************************* + * Copyright (C) ST-Ericsson SA 2011 + * License terms: 3-clause BSD license + ******************************************************************************/ + +#include <errno.h> +#include <sys/time.h> + +#include "Types.h" +#include "CEventObject.h" +#include "OS.h" + +// ****************************************************************************** +// Name: CEventObject() +// Desc: CEventObject constructor which initializes the m_sem member +// Ret: +// ****************************************************************************** +CEventObject::CEventObject() +{ + sem_init(&m_sem, 0, 0); +} + +// ****************************************************************************** +// Name: ~CEventObject() +// Desc: CEventObject destructor +// Ret: +// ****************************************************************************** +CEventObject::~CEventObject() +{ + sem_destroy(&m_sem); +} + +// ****************************************************************************** +// Name: SetEvent() +// Desc: Sets an event by post-ing the member semaphore m_sem +// Ret: +// ****************************************************************************** +void CEventObject::SetEvent() +{ + sem_post(&m_sem); +} + +// ****************************************************************************** +// Name: Wait() +// Desc: implementation of the pure virtual base class member Wait() +// Ret: WAIT_OBJECT_0 when event occurs, or WAIT_TIMEOUT on timeout +// ****************************************************************************** +DWORD CEventObject::Wait(DWORD dwTimeout) +{ + if (INFINITE == dwTimeout) { + sem_wait(&m_sem); + return WAIT_OBJECT_0; + } else { + timespec absolute_time = OS::GetAbsoluteTime(dwTimeout); + int ret; + + /* coverity[returned_null] */ + while (-1 == (ret = sem_timedwait(&m_sem, &absolute_time)) && errno == EINTR); + + if (0 == ret) { + return WAIT_OBJECT_0; + } else { + return WAIT_TIMEOUT; + } + } +} diff --git a/source/api_wrappers/linux/CEventObject.h b/source/api_wrappers/linux/CEventObject.h new file mode 100644 index 0000000..9c86087 --- /dev/null +++ b/source/api_wrappers/linux/CEventObject.h @@ -0,0 +1,40 @@ +/******************************************************************************* +* +* File name: CEventObjectObject.h +* Language: Visual C++ +* Description: CEventObjectObject class declarations +* +* +* Copyright (C) ST-Ericsson SA 2011 +* License terms: 3-clause BSD license +* +*******************************************************************************/ + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// + +#ifndef _CEVENTOBJECT_H +#define _CEVENTOBJECT_H + +#include <semaphore.h> + +#include "Types.h" +#include "CWaitableObject.h" + +//class used to wrap the OS methods for even signaling +//implements Wait() method used when waiting for event to occur + +class CEventObject : public CWaitableObject +{ +public: + CEventObject(); + virtual ~CEventObject(); + void SetEvent(); + void UnsetEvent(); + DWORD Wait(DWORD dwTimeout = INFINITE); +private: + sem_t m_sem; +}; + +#endif /* _CEVENTOBJECT_H */ + diff --git a/source/api_wrappers/linux/CSemaphore.cpp b/source/api_wrappers/linux/CSemaphore.cpp new file mode 100644 index 0000000..0246091 --- /dev/null +++ b/source/api_wrappers/linux/CSemaphore.cpp @@ -0,0 +1,55 @@ +/******************************************************************************* + * Copyright (C) ST-Ericsson SA 2011 + * License terms: 3-clause BSD license + ******************************************************************************/ + +#include <errno.h> +#include <semaphore.h> +#include <sys/time.h> +#include "Types.h" +#include "CSemaphore.h" +#include "OS.h" + +CSemaphore::CSemaphore(unsigned int initial_count) +{ + sem_init(&m_semaphore, 0, initial_count); +} + +CSemaphore::~CSemaphore() +{ + sem_destroy(&m_semaphore); +} + +bool CSemaphore::Release(unsigned int count) +{ + if (!count) { + return false; + } + + for (unsigned int i = 0; i < count; ++i) { + if (sem_post(&m_semaphore)) { + return false; + } + } + + return true; +} + +DWORD CSemaphore::Wait(DWORD timeout) +{ + if (INFINITE == timeout) { + sem_wait(&m_semaphore); + } else { + timespec absoulute_time = OS::GetAbsoluteTime(timeout); + int ret; + + /* coverity[returned_null] */ + while (-1 == (ret = sem_timedwait(&m_semaphore, &absoulute_time)) && errno == EINTR); + + if (0 != ret) { + return WAIT_TIMEOUT; + } + } + + return WAIT_OBJECT_0; +} diff --git a/source/api_wrappers/linux/CSemaphore.h b/source/api_wrappers/linux/CSemaphore.h new file mode 100644 index 0000000..24af4ad --- /dev/null +++ b/source/api_wrappers/linux/CSemaphore.h @@ -0,0 +1,26 @@ +/******************************************************************************* + * Copyright (C) ST-Ericsson SA 2011 + * License terms: 3-clause BSD license + ******************************************************************************/ + +#ifndef _CSEMAPHORE_H +#define _CSEMAPHORE_H + +#include <pthread.h> +#include <semaphore.h> +#include "CWaitableObject.h" + +class CSemaphore : public CWaitableObject +{ +public: + CSemaphore(unsigned int initial_count = 0); + virtual ~CSemaphore(); + bool Release(unsigned int count = 1); + DWORD Wait(DWORD timeout = INFINITE); + +private: + sem_t m_semaphore; +}; + +#endif /* _CSEMAPHORE_H */ + diff --git a/source/api_wrappers/linux/CSemaphoreQueue.cpp b/source/api_wrappers/linux/CSemaphoreQueue.cpp new file mode 100644 index 0000000..0a2f573 --- /dev/null +++ b/source/api_wrappers/linux/CSemaphoreQueue.cpp @@ -0,0 +1,121 @@ +/******************************************************************************* + * Copyright (C) ST-Ericsson SA 2011 + * License terms: 3-clause BSD license + ******************************************************************************/ + +#include <pthread.h> +#include <assert.h> +#include "Types.h" +#include "CEventObject.h" +#include "CSemaphore.h" +#include "CSemaphoreQueue.h" + +CSemaphoreQueue::CSemaphoreQueue(unsigned int MaxCount) : m_MaximumCount(MaxCount) +{ + m_pEventObject = new CEventObject(); + m_pSemaphore = new CSemaphore(); + + m_ObjectCollection.Add(m_pEventObject); + m_ObjectCollection.Add(m_pSemaphore); + + m_Queue = new void*[MaxCount]; + m_CurrentHead = 0; + m_CurrentTail = 0; + m_CurrentCount = 0; +} + +CSemaphoreQueue::~CSemaphoreQueue() +{ + delete m_pEventObject; + delete m_pSemaphore; + delete[] m_Queue; +} + +bool CSemaphoreQueue::AddTail(void *pObject) +{ + bool result = false; + CLockCS LocalCSLock(m_CSLock); + AddToQueueTail(pObject); + result = m_pSemaphore->Release(1); + + if (!result) { + // Error : use ::GetLastError for cause of error + RemoveFromQueueTail(); // Not really necessary but keep, + // everything's gone pear-shaped anyway + } + + return result; +} + +RemoveResult CSemaphoreQueue::RemoveHead(void **ppObject, size_t mSecTimeout) +{ + CWaitableObject *pWaitableObject = m_ObjectCollection.Wait(mSecTimeout); + + if (pWaitableObject == m_pEventObject) { + return REMOVE_CANCEL; + } else if (pWaitableObject == m_pSemaphore) { + CLockCS LocalCSLock(m_CSLock); + *ppObject = RemoveFromQueueHead(); // Remove pObject from pObjectQueue head + return REMOVE_SUCCESS; + } else if (NULL == pWaitableObject) { + return REMOVE_TIMEOUT;; + } else { + // Should never occur + assert(false); + return REMOVE_CANCEL;; + } +} + +void CSemaphoreQueue::SignalEvent() +{ + CEventObject *pEvent = m_pEventObject; + pEvent->SetEvent(); +} + +void CSemaphoreQueue::IncrementHead() +{ + ++m_CurrentHead; + + if (m_CurrentHead == m_MaximumCount) { + m_CurrentHead = 0; + } +} + +void CSemaphoreQueue::IncrementTail() +{ + ++m_CurrentTail; + + if (m_CurrentTail == m_MaximumCount) { + m_CurrentTail = 0; + } +} + +void CSemaphoreQueue::AddToQueueTail(void *pObject) +{ + m_Queue[m_CurrentTail] = pObject; + IncrementTail(); +} + +void *CSemaphoreQueue::RemoveFromQueueHead() +{ + void *Object; + Object = m_Queue[m_CurrentHead]; + IncrementHead(); + return Object; +} + +// Next 2 functions not really necessary - for error case only +void CSemaphoreQueue::DecrementTail() +{ + if (m_CurrentTail == 0) { + m_CurrentTail = m_MaximumCount - 1; + } else { + --m_CurrentTail; + } +} + +void CSemaphoreQueue::RemoveFromQueueTail() +{ + DecrementTail(); + m_Queue[m_CurrentTail] = 0; +} diff --git a/source/api_wrappers/linux/CSemaphoreQueue.h b/source/api_wrappers/linux/CSemaphoreQueue.h new file mode 100644 index 0000000..712d697 --- /dev/null +++ b/source/api_wrappers/linux/CSemaphoreQueue.h @@ -0,0 +1,67 @@ +/******************************************************************************* + * Copyright (C) ST-Ericsson SA 2011 + * License terms: 3-clause BSD license + ******************************************************************************/ + +#ifndef _CSEMAPHOREQUEUE_H +#define _CSEMAPHOREQUEUE_H +#include "Types.h" +#include "CEventObject.h" +#include "CSemaphore.h" +#include "pthread.h" +#include "CCriticalSection.h" +#include "CWaitableObject.h" +#include "CWaitableObjectCollection.h" + +class CSemaphore; + +enum RemoveResult { + REMOVE_SUCCESS, + REMOVE_TIMEOUT, + REMOVE_CANCEL +}; + +// An object queue implementation using WinAPI's Semaphore. +// For multi-threaded multi-processor applications with multiple producers and multiple consumers +class CSemaphoreQueue +{ +public: + CSemaphoreQueue(unsigned int MaxCount); + ~CSemaphoreQueue(); + bool AddTail(void *pObject); + RemoveResult RemoveHead(void **ppRequest, size_t mSecTimeout); + void SignalEvent(); +private: + CEventObject *m_pEventObject; + CSemaphore *m_pSemaphore; + CCriticalSectionObject m_CSLock; + void **m_Queue; + const unsigned int m_MaximumCount; + unsigned int m_CurrentCount; + unsigned int m_CurrentHead; + unsigned int m_CurrentTail; + void IncrementHead(); + void IncrementTail(); + void AddToQueueTail(void *pObject); + void *RemoveFromQueueHead(); + unsigned int GetIndex(CWaitableObject *Object) const; + // Next 2 functions not really necessary - for error case only + void DecrementTail(); + void RemoveFromQueueTail(); + CWaitableObjectCollection m_ObjectCollection; +}; + +class CPollQueue : public CSemaphoreQueue +{ +public: + CPollQueue() : CSemaphoreQueue(256) {} + RemoveResult RemovePollRequest(void **pTO, int mSecTimeout = INFINITE) { + return RemoveHead(pTO, mSecTimeout); + } + bool AddPollRequest(void *pObject) { + return AddTail(pObject); + } +}; + +#endif /* _CSEMAPHOREQUEUE_H */ + diff --git a/source/api_wrappers/linux/CSimpleQueue.h b/source/api_wrappers/linux/CSimpleQueue.h new file mode 100644 index 0000000..18a0478 --- /dev/null +++ b/source/api_wrappers/linux/CSimpleQueue.h @@ -0,0 +1,26 @@ +/****************************************************************************** +* +* Copyright (C) ST-Ericsson SA 2011 +* License terms: 3-clause BSD license +* +******************************************************************************/ + +#ifndef _CSIMPLEQUEUE_H_ +#define _CSIMPLEQUEUE_H_ + +#include "Types.h" +#include "CSemaphoreQueue.h" + +class CSimpleQueue : public CSemaphoreQueue +{ +public: + CSimpleQueue(): CSemaphoreQueue(256) {} + RemoveResult RemoveRequest(void **ppRequest, int mSecTimeout = INFINITE) { + return RemoveHead(ppRequest, mSecTimeout); + } + bool AddRequest(void *pRequest) { + return AddTail(pRequest); + } +}; + +#endif // _CSIMPLEQUEUE_H_ diff --git a/source/api_wrappers/linux/CThreadWrapper.cpp b/source/api_wrappers/linux/CThreadWrapper.cpp new file mode 100644 index 0000000..3e95da8 --- /dev/null +++ b/source/api_wrappers/linux/CThreadWrapper.cpp @@ -0,0 +1,119 @@ +/******************************************************************************* +* +* File name: CThreadWrapper.cpp +* Language: Visual C++ +* Description: CThreadWrapper class definitions +* The class implements OS independent thread wrapper. +* The class contains methods for suspending resuming and waiting for threads. +* The implementations of the methods is linux dependent, +* but the interface of the functions is same as the appropriate +* WIN32 implementation. +* +* +* Copyright (C) ST-Ericsson SA 2011 +* License terms: 3-clause BSD license +* +*******************************************************************************/ + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// + +#include <string> +#include <signal.h> + +#include "Types.h" +#include "CThreadWrapper.h" +#include "CCriticalSection.h" + +// ****************************************************************************** +// Name: CThreadWrapper() +// Desc: CEventObject constructor which initializes the class members +// Ret: +// ****************************************************************************** +CThreadWrapper::CThreadWrapper(void*(*pStartAddress)(void *), void *pArgument) +{ + m_pStartAddress = pStartAddress; + m_pArgument = pArgument; + m_tid = (pthread_t)0; +} + +// ****************************************************************************** +// Name: ~CThreadWrapper() +// Desc: CEventObject destructor +// Ret: +// ****************************************************************************** +CThreadWrapper::~CThreadWrapper() +{ +} + +// ****************************************************************************** +// Name: Wait() +// Desc: Waits for the thread to finish its job +// Ret: WAIT_OBJECT_0 on success and EAIT_TIMEOUT otherwise +// ****************************************************************************** +DWORD CThreadWrapper::Wait(DWORD dwMilliseconds) +{ + if ((int)m_tid != 0) { + return m_ThreadEndedEvt.Wait(dwMilliseconds); + } else { + return WAIT_OBJECT_0; + } +} + +// ****************************************************************************** +// Name: ResumeThread() +// Desc: Resumes the thread. Should be called after the thread is created +// Ret: +// ****************************************************************************** +void CThreadWrapper::ResumeThread() +{ + pthread_create(&m_tid, NULL, CThreadWrapper::ThreadFunc, this); +} + +// ****************************************************************************** +// Name: ResumeThread() +// Desc: Suspends the running thread. +// Ret: +// ****************************************************************************** +void CThreadWrapper::SuspendThread() +{ + // this will stop all active threads + pthread_kill(m_tid, SIGSTOP); +} + +// ****************************************************************************** +// Name: WaitToDie() +// Desc: Waits 1000ms for thread to die +// Ret: +// ****************************************************************************** +void CThreadWrapper::WaitToDie(DWORD dwMilliseconds) +{ + CThreadWrapper::Wait(dwMilliseconds); +} +// ****************************************************************************** +// Name: ThreadFunc() +// Desc: Sets event when the thread finishes its job +// Ret: +// ****************************************************************************** +void *CThreadWrapper::ThreadFunc(void *arg) +{ + CThreadWrapper *pthis = (CThreadWrapper *)arg; + + pthis->m_pStartAddress(pthis->m_pArgument); + + // Thread has finished, set appropriate event here + pthis->m_ThreadEndedEvt.SetEvent(); + pthread_detach(pthread_self()); + pthis->m_tid = (pthread_t)0; + return NULL; +} + +// ****************************************************************************** +// Name: GetThreadId() +// Desc: Returns the thread ID +// Ret: Thread ID +// ****************************************************************************** +DWORD CThreadWrapper::GetThreadId() +{ + return (DWORD)m_tid; +} diff --git a/source/api_wrappers/linux/CThreadWrapper.h b/source/api_wrappers/linux/CThreadWrapper.h new file mode 100644 index 0000000..aabadcc --- /dev/null +++ b/source/api_wrappers/linux/CThreadWrapper.h @@ -0,0 +1,36 @@ +/******************************************************************************* + * Copyright (C) ST-Ericsson SA 2011 + * License terms: 3-clause BSD license + ******************************************************************************/ + +#ifndef _CTHREADWRAPPER_H +#define _CTHREADWRAPPER_H + +#include "CWaitableObject.h" +#include "CEventObject.h" +#include <pthread.h> + +typedef void*(*StartAddress_t)(void *); + +class CThreadWrapper : public CWaitableObject +{ +public: + CThreadWrapper(void*(* pStartAddress)(void *), void *pArgument); + ~CThreadWrapper(); + void ResumeThread(); + void SuspendThread(); + void WaitToDie(DWORD dwMilliseconds = 1000); + DWORD GetThreadId(); + DWORD Wait(DWORD dwMilliseconds = INFINITE); +private: + pthread_t m_tid; + StartAddress_t m_pStartAddress; + void *m_pArgument; + CEventObject m_ThreadEndedEvt; + +protected: + static void *ThreadFunc(void *Arg); +}; + +#endif /* _CTHREADWRAPPER_H */ + diff --git a/source/api_wrappers/linux/CWaitableObject.cpp b/source/api_wrappers/linux/CWaitableObject.cpp new file mode 100644 index 0000000..918dd6f --- /dev/null +++ b/source/api_wrappers/linux/CWaitableObject.cpp @@ -0,0 +1,17 @@ +/******************************************************************************* + * Copyright (C) ST-Ericsson SA 2011 + * License terms: 3-clause BSD license + ******************************************************************************/ + +#include "Types.h" +#include "CWaitableObject.h" + +CWaitableObject::CWaitableObject() +{ +} + + +CWaitableObject::~CWaitableObject() +{ +} + diff --git a/source/api_wrappers/linux/CWaitableObject.h b/source/api_wrappers/linux/CWaitableObject.h new file mode 100644 index 0000000..93cd638 --- /dev/null +++ b/source/api_wrappers/linux/CWaitableObject.h @@ -0,0 +1,24 @@ +/******************************************************************************* + * Copyright (C) ST-Ericsson SA 2011 + * License terms: 3-clause BSD license + ******************************************************************************/ + +#ifndef _CWAITABLEOBJECT_H +#define _CWAITABLEOBJECT_H + +#include "Types.h" + +class CWaitableObject +{ +public: + CWaitableObject(); + virtual ~CWaitableObject(); + virtual DWORD Wait(DWORD dwTimeout = INFINITE) = 0; +private: + +}; + +typedef CWaitableObject *HANDLE; + +#endif /* _CWAITABLEOBJECT_H */ + diff --git a/source/api_wrappers/linux/CWaitableObjectCollection.cpp b/source/api_wrappers/linux/CWaitableObjectCollection.cpp new file mode 100644 index 0000000..e48fd1c --- /dev/null +++ b/source/api_wrappers/linux/CWaitableObjectCollection.cpp @@ -0,0 +1,67 @@ +/******************************************************************************* + * Copyright (C) ST-Ericsson SA 2011 + * License terms: 3-clause BSD license + ******************************************************************************/ + +#include <time.h> +#include <errno.h> + +#include "Types.h" +#include "CWaitableObjectCollection.h" +#include <assert.h> + +CWaitableObjectCollection::CWaitableObjectCollection() +{ + m_objs.clear(); +} + +CWaitableObjectCollection::~CWaitableObjectCollection() +{ + m_objs.clear(); +} + +void CWaitableObjectCollection::Add(CWaitableObject *obj) +{ + m_objs.push_back(obj); +} + +CWaitableObject *CWaitableObjectCollection::Wait(DWORD dwTimeout) +{ + vector<CWaitableObject *>::iterator it; + DWORD dwTimePassed = 0; + struct timespec ts; + struct timespec curr_time, start_time; + + if (-1 == clock_gettime(CLOCK_REALTIME, &start_time)) { + return NULL; + } + + do { + for (it = m_objs.begin(); it != m_objs.end(); ++it) { + assert(*it); + + if (WAIT_OBJECT_0 == (*it)->Wait(0)) { + return (*it); + } + } + + ts.tv_sec = 0; + ts.tv_nsec = 10000000L; // 10 milliseconds + + // There isn't any possiblity of errno returning NULL, mean is not defined. Even if + // errno is not defined this is the least thing that we should care for. + + // coverity[returned_null] + while (-1 == nanosleep(&ts, &ts) && EINTR == errno); + + if (-1 == clock_gettime(CLOCK_REALTIME, &curr_time)) { + return NULL; + } + + dwTimePassed = 1000 * (curr_time.tv_sec - start_time.tv_sec) + \ + (curr_time.tv_nsec - start_time.tv_nsec) / 1000000; + + } while (dwTimePassed < dwTimeout); + + return NULL; +} diff --git a/source/api_wrappers/linux/CWaitableObjectCollection.h b/source/api_wrappers/linux/CWaitableObjectCollection.h new file mode 100644 index 0000000..f5edfa7 --- /dev/null +++ b/source/api_wrappers/linux/CWaitableObjectCollection.h @@ -0,0 +1,26 @@ +/******************************************************************************* + * Copyright (C) ST-Ericsson SA 2011 + * License terms: 3-clause BSD license + ******************************************************************************/ + +#ifndef _CWAITABLEOBJECTCOLLECTION_H +#define _CWAITABLEOBJECTCOLLECTION_H + +#include <vector> +#include "CWaitableObject.h" + +using namespace std; + +class CWaitableObjectCollection +{ +public: + CWaitableObjectCollection(); + virtual ~CWaitableObjectCollection(); + void Add(CWaitableObject *obj); + CWaitableObject *Wait(DWORD dwTimeout = INFINITE); +private: + vector<CWaitableObject *>m_objs; +}; + +#endif /* _CWAITABLEOBJECTCOLLECTION_H */ + diff --git a/source/api_wrappers/linux/LinuxApiWrappers.h b/source/api_wrappers/linux/LinuxApiWrappers.h new file mode 100644 index 0000000..b63b840 --- /dev/null +++ b/source/api_wrappers/linux/LinuxApiWrappers.h @@ -0,0 +1,20 @@ +/******************************************************************************* + * Copyright (C) ST-Ericsson SA 2011 + * License terms: 3-clause BSD license + ******************************************************************************/ + +#ifndef _LINUXAPIWRAPPERS_H +#define _LINUXAPIWRAPPERS_H + +#include "Types.h" +#include "CCriticalSection.h" +#include "CEventObject.h" +#include "CSemaphore.h" +#include "CSemaphoreQueue.h" +#include "CThreadWrapper.h" +#include "CWaitableObject.h" +#include "CWaitableObjectCollection.h" +#include "OS.h" + +#endif /* _LINUXAPIWRAPPERS_H */ + diff --git a/source/api_wrappers/linux/OS.cpp b/source/api_wrappers/linux/OS.cpp new file mode 100644 index 0000000..55c82bd --- /dev/null +++ b/source/api_wrappers/linux/OS.cpp @@ -0,0 +1,88 @@ +/******************************************************************************* + * Copyright (C) ST-Ericsson SA 2011 + * License terms: 3-clause BSD license + ******************************************************************************/ + +#include <time.h> +#include <errno.h> +#include "Types.h" +#include "OS.h" + +DWORD OS::ErrorCode = 0; + +OS::OS() +{ +} + +OS::~OS() +{ +} + +void OS::Sleep(DWORD dwMilliseconds) +{ + struct timespec ts; + + ts.tv_sec = dwMilliseconds / 1000; + ts.tv_nsec = (dwMilliseconds % 1000) * 1000000L; + + /* coverity[returned_null] */ + while (-1 == nanosleep(&ts, &ts) && errno == EINTR); +} + +time_t OS::GetSystemTimeInMs() +{ + timespec systemTime; + clock_gettime(CLOCK_REALTIME, &systemTime); + return (systemTime.tv_sec * 1000) + (systemTime.tv_nsec / 1000000); +} + +timespec OS::GetAbsoluteTime(DWORD dwTimeout) +{ + timespec absolute_time; + timespec current_time; + + long timeout_nsec; + + clock_gettime(CLOCK_REALTIME, ¤t_time); + + absolute_time.tv_sec = current_time.tv_sec + (dwTimeout / 1000); + timeout_nsec = (dwTimeout % 1000) * 1000000L; + + if ((1000000000 - current_time.tv_nsec) < timeout_nsec) { + // overflow will occur! + absolute_time.tv_sec++; + } + + absolute_time.tv_nsec = (current_time.tv_nsec + timeout_nsec) % 1000000000; + + return absolute_time; +} + + +char *strcpy_s(char *dst, size_t _Size, const char *src) +{ + return strncpy(dst, src, _Size); +} + +int sprintf_s(char *dst, size_t _Size, const char *format, ...) +{ + va_list l; + int ReturnValue; + + va_start(l, format); + ReturnValue = vsnprintf(dst, _Size, format, l); + va_end(l); + + return ReturnValue; +} + +char *strncpy_s(char *dst, const char *src, size_t _Size) +{ + return strncpy(dst, src, _Size); +} + +int _stricmp(const char *s1, const char *s2) +{ + return strcasecmp(s1, s2); +} + diff --git a/source/api_wrappers/linux/OS.h b/source/api_wrappers/linux/OS.h new file mode 100644 index 0000000..cd219b9 --- /dev/null +++ b/source/api_wrappers/linux/OS.h @@ -0,0 +1,107 @@ +/******************************************************************************* + * Copyright (C) ST-Ericsson SA 2011 + * License terms: 3-clause BSD license + ******************************************************************************/ + +#ifndef _OS_H +#define _OS_H +#include <sys/time.h> +#include <stdio.h> +#include <stdarg.h> +#include <string.h> +#include <strings.h> +#include <stdlib.h> +#include "Types.h" + +#include "CWaitableObject.h" + +class OS +{ +public: + OS(); + virtual ~OS(); + + static void Sleep(DWORD dwMilliseconds); + static time_t GetSystemTimeInMs(); + + static DWORD GetErrorCode() { + return OS::ErrorCode; + } + static void SetErrorCode(DWORD dwErrorCode) { + OS::ErrorCode = dwErrorCode; + } + + static timespec GetAbsoluteTime(DWORD dwTimeout); +private: + + static DWORD ErrorCode; +}; + +#define MAX(a, b) ((a) > (b) ? (a) : (b)) + +/* + * str manipulation functions used in windows build + */ + +char *strcpy_s(char *dst, size_t _Size, const char *src); +char *strncpy_s(char *dst, const char *src, size_t _Size); +int _stricmp(const char *s1, const char *s2); +int sprintf_s(char *dst, size_t _Size, const char *format, ...); + +#define _snprintf snprintf + +template <size_t _Size> +char *strcpy_s(char(&dst)[_Size], const char src[]) +{ + return strncpy(dst, src, _Size); +} + +template<size_t _Size> +int sprintf_s(char(&dst)[_Size], const char *format, ...) +{ + int ReturnValue; + va_list l; + va_start(l, format); + ReturnValue = vsnprintf(dst, _Size, format, l); + va_end(l); + return ReturnValue; +} + +template <size_t _Size> +char *strcat_s(char(&dst)[_Size], const char src[]) +{ + return strncat(dst, src, _Size); +} + +template <size_t _Size> +int _ultoa_s(unsigned long value, char(&str)[_Size], int radix) +{ + switch (radix) { + case 10: + return sprintf_s(str, "%ul", value); + case 16: + return sprintf_s(str, "%ulX", value); + default: + return -1; + } +} + +template<size_t _Size> +int _snprintf_s(char(&dst)[_Size], size_t _MaxCount, const char *format, ...) +{ + int ReturnValue; + va_list l; + va_start(l, format); + ReturnValue = vsnprintf(dst, MAX(_MaxCount, _Size), format, l); + va_end(l); + return ReturnValue; +} + +template<size_t _Size> +int vsprintf_s(char(&dst)[_Size], const char *format, va_list l) +{ + return vsnprintf(dst, _Size, format, l); +} + +#endif /* _OS_H */ + diff --git a/source/api_wrappers/linux/Types.h b/source/api_wrappers/linux/Types.h new file mode 100644 index 0000000..14929b4 --- /dev/null +++ b/source/api_wrappers/linux/Types.h @@ -0,0 +1,21 @@ +/******************************************************************************* + * Copyright (C) ST-Ericsson SA 2011 + * License terms: 3-clause BSD license + ******************************************************************************/ + +#ifndef _TYPES_H +#define _TYPES_H + +const unsigned int os_minus_one = ~0; +#define INFINITE os_minus_one + +#define WAIT_OBJECT_0 0 +#define WAIT_TIMEOUT 0x00000102L +#define WAIT_FAILED (DWORD)0xFFFFFFFF + +typedef unsigned int DWORD; + +#define WINAPI + +#endif /* _TYPES_H */ + |