summaryrefslogtreecommitdiff
path: root/os_wrappers/Thread.cpp
blob: cc50f732f23ef631adfa2d5779762079a3dcfcd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
}

/* @} */