libcw/cwThread.cpp

284 lines
6.0 KiB
C++
Raw Normal View History

2019-12-19 03:24:12 +00:00
#include "cwCommon.h"
#include "cwLog.h"
#include "cwCommonImpl.h"
#include "cwMem.h"
#include "cwThread.h"
#include <pthread.h>
namespace cw
{
namespace thread
2019-12-19 03:24:12 +00:00
{
enum
2019-12-19 03:24:12 +00:00
{
kDoExitThFl = 0x01,
kDoPauseThFl = 0x02,
kDoRunThFl = 0x04
};
2019-12-19 03:24:12 +00:00
typedef struct thread_str
{
pthread_t pThreadH;
stateId_t stateId;
cbFunc_t func;
void* funcArg;
unsigned doFlags;
unsigned stateMicros;
unsigned pauseMicros;
unsigned sleepMicros = 15000;
} thread_t;
inline thread_t* _handleToPtr(handle_t h) { return handleToPtr<handle_t,thread_t>(h); }
rc_t _waitForState( thread_t* p, unsigned stateId )
{
unsigned waitTimeMicroSecs = 0;
2019-12-19 03:24:12 +00:00
while( p->stateId != stateId && waitTimeMicroSecs < p->stateMicros )
{
sleepUs( p->sleepMicros );
waitTimeMicroSecs += p->sleepMicros;
}
2019-12-19 03:24:12 +00:00
return p->stateId==stateId ? kOkRC : kTimeOutRC;
}
2019-12-19 03:24:12 +00:00
void _threadCleanUpCallback(void* p)
2019-12-19 03:24:12 +00:00
{
((thread_t*)p)->stateId = kExitedThId;
}
2019-12-19 03:24:12 +00:00
void* _threadCallback(void* param)
{
thread_t* p = (thread_t*)param;
// set a clean up handler - this will be called when the
// thread terminates unexpectedly or pthread_cleanup_pop() is called.
pthread_cleanup_push(_threadCleanUpCallback,p);
while( cwIsFlag(p->doFlags,kDoExitThFl) == false )
2019-12-19 03:24:12 +00:00
{
// if we are in the pause state
if( p->stateId == kPausedThId )
2019-12-19 03:24:12 +00:00
{
sleepUs( p->pauseMicros );
2019-12-19 03:24:12 +00:00
// check if we have been requested to leave the pause state
if( cwIsFlag(p->doFlags,kDoRunThFl) )
{
p->doFlags = cwClrFlag(p->doFlags,kDoRunThFl);
p->stateId = kRunningThId;
}
}
else
2019-12-19 03:24:12 +00:00
{
// call the user-defined function
if( p->func(p->funcArg)==false )
break;
// check if we have been requested to enter the pause state
if( cwIsFlag(p->doFlags,kDoPauseThFl) )
{
p->doFlags = cwClrFlag(p->doFlags,kDoPauseThFl);
p->stateId = kPausedThId;
}
2019-12-19 03:24:12 +00:00
}
}
pthread_cleanup_pop(1);
2019-12-19 03:24:12 +00:00
pthread_exit(NULL);
2019-12-19 03:24:12 +00:00
return p;
}
}
2019-12-19 03:24:12 +00:00
}
cw::rc_t cw::thread::create( handle_t& hRef, cbFunc_t func, void* funcArg, int stateMicros, int pauseMicros )
2019-12-19 03:24:12 +00:00
{
rc_t rc;
int sysRC;
pthread_attr_t attr;
if((rc = destroy(hRef)) != kOkRC )
2019-12-19 03:24:12 +00:00
return rc;
thread_t* p = memAllocZ<thread_t>();
p->func = func;
p->funcArg = funcArg;
p->stateMicros = stateMicros;
p->pauseMicros = pauseMicros;
p->stateId = kPausedThId;
if((sysRC = pthread_attr_init(&attr)) != 0)
2019-12-19 03:24:12 +00:00
{
p->stateId = kNotInitThId;
rc = cwLogSysError(kOpFailRC,sysRC,"Thread attribute init failed.");
2019-12-19 03:24:12 +00:00
}
else
if ((sysRC = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)) != 0)
{
p->stateId = kNotInitThId;
rc = cwLogSysError(kOpFailRC,sysRC,"Thread set detach attribute failed.");
}
else
if((sysRC = pthread_create(&p->pThreadH, &attr, _threadCallback, (void*)p )) != 0 )
{
p->stateId = kNotInitThId;
rc = cwLogSysError(kOpFailRC,sysRC,"Thread create failed.");
}
2019-12-19 03:24:12 +00:00
hRef.set(p);
return rc;
}
cw::rc_t cw::thread::destroy( handle_t& hRef )
2019-12-19 03:24:12 +00:00
{
rc_t rc = kOkRC;
if( !hRef.isValid() )
return rc;
thread_t* p = _handleToPtr(hRef);
2019-12-19 03:24:12 +00:00
// tell the thread to exit
p->doFlags = cwSetFlag(p->doFlags,kDoExitThFl);
// wait for the thread to exit and then deallocate the thread object
if((rc = _waitForState(p,kExitedThId)) != kOkRC )
return cwLogError(rc,"Thread timed out waiting for destroy.");
memRelease(p);
hRef.clear();
2019-12-19 03:24:12 +00:00
return rc;
}
cw::rc_t cw::thread::pause( handle_t h, unsigned cmdFlags )
2019-12-19 03:24:12 +00:00
{
rc_t rc = kOkRC;
bool pauseFl = cwIsFlag(cmdFlags,kPauseFl);
bool waitFl = cwIsFlag(cmdFlags,kWaitFl);
thread_t* p = _handleToPtr(h);
2019-12-19 03:24:12 +00:00
bool isPausedFl = p->stateId == kPausedThId;
unsigned waitId;
if( isPausedFl == pauseFl )
return kOkRC;
if( pauseFl )
{
p->doFlags = cwSetFlag(p->doFlags,kDoPauseThFl);
waitId = kPausedThId;
}
else
{
p->doFlags = cwSetFlag(p->doFlags,kDoRunThFl);
waitId = kRunningThId;
}
if( waitFl )
rc = _waitForState(p,waitId);
if( rc != kOkRC )
cwLogError(rc,"Thread timed out waiting for '%s'.", pauseFl ? "pause" : "un-pause");
return rc;
}
cw::rc_t cw::thread::unpause( handle_t h )
{ return pause( h, kWaitFl); }
cw::thread::stateId_t cw::thread::state( handle_t h )
2019-12-19 03:24:12 +00:00
{
thread_t* p = _handleToPtr(h);
2019-12-19 03:24:12 +00:00
return p->stateId;
}
unsigned cw::thread::id()
{ return static_cast<unsigned>(pthread_self()); }
2019-12-19 03:24:12 +00:00
namespace cw
{
bool _threadTestCb( void* p )
{
unsigned* ip = (unsigned*)p;
ip[0]++;
return true;
}
}
cw::rc_t cw::threadTest()
{
thread::handle_t h;
unsigned val = 0;
rc_t rc;
char c = 0;
2019-12-19 03:24:12 +00:00
if((rc = thread::create(h,_threadTestCb,&val)) != kOkRC )
2019-12-19 03:24:12 +00:00
return rc;
if((rc = thread::pause(h,0)) != kOkRC )
2019-12-19 03:24:12 +00:00
goto errLabel;
cwLogInfo("o=print p=pause s=state q=quit\n");
while( c != 'q' )
{
c = (char)fgetc(stdin);
fflush(stdin);
switch(c)
{
case 'o':
cwLogInfo("val: 0x%x\n",val);
break;
case 's':
cwLogInfo("state=%i\n",thread::state(h));
2019-12-19 03:24:12 +00:00
break;
case 'p':
{
if( thread::state(h) == thread::kPausedThId )
rc = thread::pause(h,thread::kWaitFl);
2019-12-19 03:24:12 +00:00
else
rc = thread::pause(h,thread::kPauseFl|thread::kWaitFl);
2019-12-19 03:24:12 +00:00
if( rc == kOkRC )
cwLogInfo("new state:%i\n", thread::state(h));
2019-12-19 03:24:12 +00:00
else
{
cwLogError(rc,"threadPause() test failed.");
goto errLabel;
}
}
break;
case 'q':
break;
//default:
//cwLogInfo("Unknown:%c\n",c);
}
}
errLabel:
rc_t rc0 = rc = thread::destroy(h);
2019-12-19 03:24:12 +00:00
return rc == kOkRC ? rc0 : rc;
}