summaryrefslogtreecommitdiff
path: root/os_wrappers
diff options
context:
space:
mode:
authorKarin Hedlund <karin.hedlund@stericsson.com>2011-05-20 16:56:58 +0200
committerKarin Hedlund <karin.hedlund@stericsson.com>2011-05-20 16:56:58 +0200
commitb76c2437f1017bda4879a3a8ee6ddd00fdb5c281 (patch)
treefc3873776155e1d5ff021ae9370264d226893c69 /os_wrappers
Initial commit
Diffstat (limited to 'os_wrappers')
-rwxr-xr-xos_wrappers/CriticalSection.cpp66
-rwxr-xr-xos_wrappers/CriticalSection.h82
-rwxr-xr-xos_wrappers/Event.cpp56
-rwxr-xr-xos_wrappers/Event.h66
-rwxr-xr-xos_wrappers/Thread.cpp68
-rwxr-xr-xos_wrappers/Thread.h73
-rwxr-xr-xos_wrappers/Utilities.h57
7 files changed, 468 insertions, 0 deletions
diff --git a/os_wrappers/CriticalSection.cpp b/os_wrappers/CriticalSection.cpp
new file mode 100755
index 0000000..2374565
--- /dev/null
+++ b/os_wrappers/CriticalSection.cpp
@@ -0,0 +1,66 @@
+/*
+ * CriticalSection.cpp
+ *
+ * Copyright (C) ST-Ericsson SA 2011
+ * Authors: Srimanta Panda <srimanta.panda@stericsson.com>,
+ * Ola Borgelin <ola.borgelin@stericsson.com>,
+ * Karin Hedlund <karin.hedlund@stericsson.com>,
+ * Markus Andersson <markus.m.andersson@stericsson.com> for ST-Ericsson.
+ * License terms: 3-clause BSD license
+ *
+ */
+
+/*
+ * @addtogroup OS Wrappers
+ * @{
+ */
+
+#include "CriticalSection.h"
+
+CriticalSection::CriticalSection()
+{
+#ifdef _WIN32
+ InitializeCriticalSection(&mutex_);
+#else
+ pthread_mutex_init(&mutex_, NULL);
+#endif
+}
+
+CriticalSection::~CriticalSection()
+{
+#ifdef _WIN32
+ DeleteCriticalSection(&mutex_);
+#else
+ pthread_mutex_destroy(&mutex_);
+#endif
+}
+
+void CriticalSection::enter()
+{
+#ifdef _WIN32
+ EnterCriticalSection(&mutex_);
+#else
+ pthread_mutex_lock(&mutex_);
+#endif
+}
+
+void CriticalSection::leave()
+{
+#ifdef _WIN32
+ LeaveCriticalSection(&mutex_);
+#else
+ pthread_mutex_unlock(&mutex_);
+#endif
+}
+
+CSLock::CSLock(CriticalSection* section): section_(section)
+{
+ section_->enter();
+}
+
+CSLock::~CSLock()
+{
+ section_->leave();
+}
+
+/* @} */
diff --git a/os_wrappers/CriticalSection.h b/os_wrappers/CriticalSection.h
new file mode 100755
index 0000000..f67f42a
--- /dev/null
+++ b/os_wrappers/CriticalSection.h
@@ -0,0 +1,82 @@
+/*
+ * CriticalSection.h
+ *
+ * Copyright (C) ST-Ericsson SA 2011
+ * Authors: Srimanta Panda <srimanta.panda@stericsson.com>,
+ * Ola Borgelin <ola.borgelin@stericsson.com>,
+ * Karin Hedlund <karin.hedlund@stericsson.com>,
+ * Markus Andersson <markus.m.andersson@stericsson.com> for ST-Ericsson.
+ * License terms: 3-clause BSD license
+ *
+ */
+
+/*
+ * @addtogroup OS Wrappers
+ * @{
+ */
+
+#pragma once
+
+#ifndef _WIN32
+#include <pthread.h>
+#endif
+
+/**
+ * @brief Simple wrapper for OS synchronization primitive.
+ *
+ * This class wraps Win32 critical section and POSIX threads mutex.
+ */
+class CriticalSection
+{
+public:
+ /**
+ * @brief Constructor creating new critical section.
+ */
+ CriticalSection();
+
+ /**
+ * @brief Destructor that destroys the critical section.
+ */
+ virtual ~CriticalSection();
+
+ /**
+ * @brief This method enters/locks the critical section.
+ */
+ void enter();
+
+ /**
+ * @brief This method leaves/unlocks the critical section.
+ */
+ void leave();
+private:
+#ifdef _WIN32
+ CRITICAL_SECTION mutex_;
+#else
+ pthread_mutex_t mutex_;
+#endif
+};
+
+/**
+ * @brief RAII class for critical section locking/unlocking.
+ */
+class CSLock
+{
+public:
+ /**
+ * @brief Constructor that enters/locks the passed critical section.
+ *
+ * @param[in] section - critical section to lock.
+ */
+ CSLock(CriticalSection* section);
+
+ /**
+ * @brief Destructor that leaves/unlocks the associated critical section.
+ */
+ virtual ~CSLock();
+private:
+ CriticalSection* section_;
+ CSLock(const CSLock&);
+ CSLock& operator=(const CSLock&);
+};
+
+/* @} */
diff --git a/os_wrappers/Event.cpp b/os_wrappers/Event.cpp
new file mode 100755
index 0000000..0390735
--- /dev/null
+++ b/os_wrappers/Event.cpp
@@ -0,0 +1,56 @@
+/*
+ * Event.cpp
+ *
+ * Copyright (C) ST-Ericsson SA 2011
+ * Authors: Srimanta Panda <srimanta.panda@stericsson.com>,
+ * Ola Borgelin <ola.borgelin@stericsson.com>,
+ * Karin Hedlund <karin.hedlund@stericsson.com>,
+ * Markus Andersson <markus.m.andersson@stericsson.com> for ST-Ericsson.
+ * License terms: 3-clause BSD license
+ *
+ */
+
+/*
+ * @addtogroup OS Wrappers
+ * @{
+ */
+
+#include "Event.h"
+
+Event::Event()
+{
+#ifdef _WIN32
+ semaphore_ = CreateSemaphore(NULL, 0, 256, NULL);
+#else
+ sem_init(&semaphore_, 0, 0);
+#endif
+}
+
+Event::~Event()
+{
+#ifdef _WIN32
+ CloseHandle(semaphore_);
+#else
+ sem_destroy(&semaphore_);
+#endif
+}
+
+void Event::signal()
+{
+#ifdef _WIN32
+ ReleaseSemaphore(semaphore_, 1, NULL);
+#else
+ sem_post(&semaphore_);
+#endif
+}
+
+void Event::wait(unsigned long timeout __attribute__((unused)))
+{
+#ifdef _WIN32
+ WaitForSingleObject(semaphore_, timeout);
+#else
+ sem_wait(&semaphore_);
+#endif
+}
+
+/* @} */
diff --git a/os_wrappers/Event.h b/os_wrappers/Event.h
new file mode 100755
index 0000000..9bd46f6
--- /dev/null
+++ b/os_wrappers/Event.h
@@ -0,0 +1,66 @@
+/*
+ * Event.h
+ *
+ * Copyright (C) ST-Ericsson SA 2011
+ * Authors: Srimanta Panda <srimanta.panda@stericsson.com>,
+ * Ola Borgelin <ola.borgelin@stericsson.com>,
+ * Karin Hedlund <karin.hedlund@stericsson.com>,
+ * Markus Andersson <markus.m.andersson@stericsson.com> for ST-Ericsson.
+ * License terms: 3-clause BSD license
+ *
+ */
+
+/*
+ * @addtogroup OS Wrappers
+ * @{
+ */
+
+#pragma once
+
+#ifdef _WIN32
+#include <windows.h>
+#else
+#include <semaphore.h>
+#endif
+
+/**
+ * @brief Simple wrapper for OS synchronization primitive.
+ *
+ * This class wraps Win32 and POSIX semaphore.
+ */
+class Event
+{
+public:
+ /**
+ * @brief Constructor creating new event.
+ */
+ Event();
+
+ /**
+ * @brief Destructor that destroys the event.
+ */
+ virtual ~Event();
+
+ /**
+ * @brief Wait on the event for specified time.
+ *
+ * By default it waits for infinite time and blocks the thread until
+ * the event is signaled.
+ */
+ void wait(unsigned long timeout = -1);
+
+ /**
+ * @brief Signal the event.
+ *
+ * When event is signaled the waiting thread is released.
+ */
+ void signal();
+private:
+#ifdef _WIN32
+ HANDLE semaphore_;
+#else
+ sem_t semaphore_;
+#endif
+};
+
+/* @} */
diff --git a/os_wrappers/Thread.cpp b/os_wrappers/Thread.cpp
new file mode 100755
index 0000000..cc50f73
--- /dev/null
+++ b/os_wrappers/Thread.cpp
@@ -0,0 +1,68 @@
+/*
+ * Thread.cpp
+ *
+ * Copyright (C) ST-Ericsson SA 2011
+ * Authors: Srimanta Panda <srimanta.panda@stericsson.com>,
+ * Ola Borgelin <ola.borgelin@stericsson.com>,
+ * Karin Hedlund <karin.hedlund@stericsson.com>,
+ * Markus Andersson <markus.m.andersson@stericsson.com> for ST-Ericsson.
+ * License terms: 3-clause BSD license
+ *
+ */
+
+/*
+ * @addtogroup OS Wrappers
+ * @{
+ */
+
+#include "Thread.h"
+#ifdef _WIN32
+#include <process.h>
+#else
+#include <pthread.h>
+#endif
+
+Thread::Thread(ThreadFunction_t function, void* argument): function_(function), argument_(argument), end_(new Event())
+{
+#ifdef _WIN32
+ _beginthread(ThreadFunction, 0, this);
+#else
+ pthread_attr_init(&attributes_);
+ pthread_attr_setdetachstate(&attributes_, PTHREAD_CREATE_DETACHED);
+ pthread_create(&id_, &attributes_, ThreadFunction, this);
+#endif
+}
+
+Thread::~Thread()
+{
+#ifndef _WIN32
+ pthread_cancel(id_);
+ pthread_attr_destroy(&attributes_);
+#endif
+ delete end_;
+}
+
+void Thread::wait(unsigned long timeout)
+{
+ end_->wait(timeout);
+ // signal the event again to avoid locking infinitely on following calls
+ end_->signal();
+}
+
+#ifdef _WIN32
+void Thread::ThreadFunction(void* arg)
+#else
+void* Thread::ThreadFunction(void* arg)
+#endif
+{
+ Thread* pthis = static_cast<Thread*>(arg);
+ pthis->function_(pthis->argument_);
+ pthis->end_->signal();
+#ifdef _WIN32
+ _endthread();
+#else
+ return 0;
+#endif
+}
+
+/* @} */
diff --git a/os_wrappers/Thread.h b/os_wrappers/Thread.h
new file mode 100755
index 0000000..a5e65b8
--- /dev/null
+++ b/os_wrappers/Thread.h
@@ -0,0 +1,73 @@
+/*
+ * Thread.h
+ *
+ * Copyright (C) ST-Ericsson SA 2011
+ * Authors: Srimanta Panda <srimanta.panda@stericsson.com>,
+ * Ola Borgelin <ola.borgelin@stericsson.com>,
+ * Karin Hedlund <karin.hedlund@stericsson.com>,
+ * Markus Andersson <markus.m.andersson@stericsson.com> for ST-Ericsson.
+ * License terms: 3-clause BSD license
+ *
+ */
+
+/*
+ * @addtogroup OS Wrappers
+ * @{
+ */
+
+#pragma once
+
+#include "Event.h"
+#ifndef _WIN32
+#include <pthread.h>
+#endif
+
+/**
+ * @brief Simple wrapper for OS thread.
+ *
+ * This class wraps Win32 and POSIX thread.
+ */
+class Thread
+{
+public:
+ /**
+ * @brief Type for thread's start function.
+ */
+ typedef void*(*ThreadFunction_t)(void*);
+
+ /**
+ * @brief Constructor creating new thread
+ *
+ * It creates new thread and starts execution of the function.
+ *
+ * @param[in] function - thread's start function.
+ * @param[in] arg - argument to be passed to the function.
+ */
+ Thread(ThreadFunction_t function, void* arg);
+
+ /**
+ * @brief Destructor.
+ */
+ virtual ~Thread();
+
+ /**
+ * @brief Wait for the thread to die for specified timeout.
+ *
+ * @param[in] timeout - Time in ms to wait. By default timeout is infinite.
+ */
+ void wait(unsigned long timeout = -1);
+private:
+#ifdef _WIN32
+ static void ThreadFunction(void* arg);
+#else
+ pthread_t id_;
+ pthread_attr_t attributes_;
+ static void* ThreadFunction(void* arg);
+#endif
+ ThreadFunction_t function_;
+ void* argument_;
+
+ Event* end_;
+};
+
+/* @} */
diff --git a/os_wrappers/Utilities.h b/os_wrappers/Utilities.h
new file mode 100755
index 0000000..ca7eac9
--- /dev/null
+++ b/os_wrappers/Utilities.h
@@ -0,0 +1,57 @@
+/*
+ * Utilities.h
+ *
+ * Copyright (C) ST-Ericsson SA 2011
+ * Authors: Srimanta Panda <srimanta.panda@stericsson.com>,
+ * Ola Borgelin <ola.borgelin@stericsson.com>,
+ * Karin Hedlund <karin.hedlund@stericsson.com>,
+ * Markus Andersson <markus.m.andersson@stericsson.com> for ST-Ericsson.
+ * License terms: 3-clause BSD license
+ *
+ */
+
+/*
+ * @addtogroup Utilities
+ * @{
+ */
+
+#pragma once
+#include <sstream>
+#include <string>
+
+using namespace std;
+class Utilities
+{
+public:
+ static void sleep(int ms) {
+ timespec delay;
+ delay.tv_sec = 0;
+ delay.tv_nsec = ms * 1000000;
+ nanosleep(&delay, 0);
+ }
+
+ template<typename T, typename F>
+ static T convert(const F& fromValue) {
+ stringstream stream;
+ stream << fromValue;
+
+ T toValue;
+ stream >> toValue;
+ return toValue;
+ }
+
+ static void trim(std::string& s) {
+ static const std::string whitespace(" \t\r\n");
+ size_t start = s.find_first_not_of(whitespace);
+
+ if (std::string::npos != start) {
+ s.erase(0, start);
+ size_t end = s.find_last_not_of(whitespace);
+ s.erase(end + 1);
+ } else {
+ s.clear();
+ }
+ }
+};
+
+/* @} */