diff options
author | Karin Hedlund <karin.hedlund@stericsson.com> | 2011-05-20 16:56:58 +0200 |
---|---|---|
committer | Karin Hedlund <karin.hedlund@stericsson.com> | 2011-05-20 16:56:58 +0200 |
commit | b76c2437f1017bda4879a3a8ee6ddd00fdb5c281 (patch) | |
tree | fc3873776155e1d5ff021ae9370264d226893c69 /os_wrappers/Thread.cpp |
Initial commit
Diffstat (limited to 'os_wrappers/Thread.cpp')
-rwxr-xr-x | os_wrappers/Thread.cpp | 68 |
1 files changed, 68 insertions, 0 deletions
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 +} + +/* @} */ |