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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
/*
* Licensed under CLIFE license. See LICENCE.TXT
*
* Produced by: Jeff Lait
*
* CLIFE Development
*
* NAME: thread_linux.h ( CLIFE, C++ )
*
* COMMENTS:
* Attempt at a threading class.
*/
#include "thread_linux.h"
THREAD_LINUX::THREAD_LINUX()
{
myExists = false;
myRunning = false;
}
THREAD_LINUX::~THREAD_LINUX()
{
if (isActive())
join();
if (myExists)
pthread_cancel(mySelf);
}
void
THREAD_LINUX::start(THREADmainFunc func, void *data)
{
// If we are still running, block until the current task completes.
if (isActive())
join();
myCB = func;
myCBData = data;
if (!myExists)
{
pthread_attr_t attr;
pthread_cond_init(&myStateChangeEvent, 0);
pthread_mutex_init(&myLock, 0);
pthread_attr_init(&attr);
myExists = true;
pthread_create(&mySelf, &attr, THREAD::wrapper, this);
pthread_attr_destroy(&attr);
}
jobsready();
}
void
THREAD_LINUX::jobsready()
{
pthread_mutex_lock(&myLock);
myRunning = true;
pthread_cond_broadcast(&myStateChangeEvent);
pthread_mutex_unlock(&myLock);
}
void
THREAD_LINUX::iamdonenow()
{
pthread_mutex_lock(&myLock);
myRunning = false;
pthread_cond_broadcast(&myStateChangeEvent);
pthread_mutex_unlock(&myLock);
}
void
THREAD_LINUX::waittillimready()
{
pthread_mutex_lock(&myLock);
while (!myRunning)
pthread_cond_wait(&myStateChangeEvent, &myLock);
pthread_mutex_unlock(&myLock);
}
void
THREAD_LINUX::join()
{
pthread_mutex_lock(&myLock);
while (myRunning)
pthread_cond_wait(&myStateChangeEvent, &myLock);
pthread_mutex_unlock(&myLock);
}
void
THREAD_LINUX::kill()
{
if (myExists)
{
pthread_cancel(mySelf);
myExists = false;
}
}
|