cwIo.h/cpp : Added io::threadCreate()

This commit is contained in:
kevin 2022-06-12 16:50:01 -04:00
parent ca0a61d7f8
commit fac893d0b2
2 changed files with 98 additions and 14 deletions

View File

@ -38,6 +38,14 @@ namespace cw
struct io_str; struct io_str;
typedef struct thread_str
{
unsigned id;
void* arg;
struct io_str* p;
struct thread_str* link;
} thread_t;
typedef struct timer_str typedef struct timer_str
{ {
struct io_str* io; struct io_str* io;
@ -86,7 +94,6 @@ namespace cw
char* label; char* label;
unsigned sockA_index; unsigned sockA_index;
unsigned userId; unsigned userId;
} socket_t; } socket_t;
typedef struct io_str typedef struct io_str
@ -102,6 +109,8 @@ namespace cw
object_t* cfg; object_t* cfg;
thread_t* threadL;
timer_t* timerA; timer_t* timerA;
unsigned timerN; unsigned timerN;
@ -141,6 +150,36 @@ namespace cw
io_t* _handleToPtr( handle_t h ) io_t* _handleToPtr( handle_t h )
{ return handleToPtr<handle_t,io_t>(h); } { return handleToPtr<handle_t,io_t>(h); }
//----------------------------------------------------------------------------------------------------------
//
// Thread
//
bool _threadFunc( void* arg )
{
thread_t* t = (thread_t*)arg;
thread_msg_t tm = { .id=t->id, .arg=t->arg };
msg_t m;
m.tid = kThreadTId;
m.u.thread = &tm;
t->p->cbFunc( t->p->cbArg, &m );
return true;
}
void _threadRelease( io_t* p )
{
thread_t* t0 = p->threadL;
for(; t0!=nullptr; t0=t0->link)
{
thread_t* t1 = t0->link;
mem::release(t0);
t0 = t1;
}
}
//---------------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------------
// //
// Timer // Timer
@ -335,7 +374,10 @@ namespace cw
// get the serial port list node // get the serial port list node
if((cfg = c->find("serial")) == nullptr) if((cfg = c->find("serial")) == nullptr)
return cwLogError(kSyntaxErrorRC,"Unable to locate the 'serial' configuration."); {
cwLogWarning("No 'serial' configuration.");
return kOkRC;
}
// the serial header values // the serial header values
if((rc = cfg->getv("pollPeriodMs", pollPeriodMs, if((rc = cfg->getv("pollPeriodMs", pollPeriodMs,
@ -413,6 +455,7 @@ namespace cw
{ {
rc_t rc = kOkRC; rc_t rc = kOkRC;
if( p->serialPortSrvH.isValid() )
// the service is only started if at least one serial port is enabled // the service is only started if at least one serial port is enabled
if( serialPort::portCount( serialPortSrv::serialHandle(p->serialPortSrvH) ) > 0 ) if( serialPort::portCount( serialPortSrv::serialHandle(p->serialPortSrvH) ) > 0 )
if((rc =serialPortSrv::start( p->serialPortSrvH )) != kOkRC ) if((rc =serialPortSrv::start( p->serialPortSrvH )) != kOkRC )
@ -461,8 +504,10 @@ namespace cw
// get the MIDI port cfg // get the MIDI port cfg
if((cfg = c->find("midi")) == nullptr) if((cfg = c->find("midi")) == nullptr)
return cwLogError(kSyntaxErrorRC,"Unable to locate the 'MIDI' configuration."); {
cwLogWarning("No 'MIDI' configuration.");
return kOkRC;
}
if((rc = cfg->getv( if((rc = cfg->getv(
"parserBufByteN", parserBufByteN )) != kOkRC ) "parserBufByteN", parserBufByteN )) != kOkRC )
@ -586,8 +631,8 @@ namespace cw
// get the socket configuration node // get the socket configuration node
if((node = cfg->find("socket")) == nullptr ) if((node = cfg->find("socket")) == nullptr )
{ {
rc = cwLogError(kSyntaxErrorRC,"Unable to locate the 'socket' configuration node."); cwLogWarning("No 'socket' configuration node.");
goto errLabel; return kOkRC;
} }
// get the required socket arguments // get the required socket arguments
@ -1469,7 +1514,10 @@ namespace cw
// get the audio port node // get the audio port node
if((node = cfg->find("audio")) == nullptr ) if((node = cfg->find("audio")) == nullptr )
return cwLogError(kSyntaxErrorRC,"Unable to locate the 'audio' configuration node."); {
cwLogWarning("No 'audio' configuration node.");
return kOkRC;
}
// get the meterMs value // get the meterMs value
if((rc = node->getv("meterMs", p->audioMeterCbPeriodMs, "threadTimeOutMs", p->audioThreadTimeOutMs )) != kOkRC ) if((rc = node->getv("meterMs", p->audioMeterCbPeriodMs, "threadTimeOutMs", p->audioThreadTimeOutMs )) != kOkRC )
@ -1863,6 +1911,29 @@ void cw::io::report( handle_t h )
} }
//----------------------------------------------------------------------------------------------------------
//
// Thread
//
cw::rc_t cw::io::threadCreate( handle_t h, unsigned id, void* arg )
{
rc_t rc = kOkRC;
io_t* p = _handleToPtr(h);
thread_t* t = mem::allocZ<thread_t>(1);
t->id = id;
t->arg = arg;
t->p = p;
t->link = p->threadL;
p->threadL = t;
if((rc = thread_mach::add( p->threadMachH, _threadFunc, t )) != kOkRC )
rc = cwLogError(rc,"Thread create failed.");
return rc;
}
//---------------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------------
// //
// Timer // Timer

13
cwIo.h
View File

@ -27,6 +27,7 @@ namespace cw
enum enum
{ {
kThreadTId,
kTimerTId, kTimerTId,
kSerialTId, kSerialTId,
kMidiTId, kMidiTId,
@ -37,6 +38,12 @@ namespace cw
kUiTId kUiTId
}; };
typedef struct thread_msg_str
{
unsigned id;
void* arg;
} thread_msg_t;
typedef struct timer_msg_str typedef struct timer_msg_str
{ {
unsigned id; unsigned id;
@ -121,6 +128,7 @@ namespace cw
unsigned tid; unsigned tid;
union union
{ {
thread_msg_t* thread;
timer_msg_t* timer; timer_msg_t* timer;
serial_msg_t* serial; serial_msg_t* serial;
midi_msg_t* midi; midi_msg_t* midi;
@ -151,6 +159,11 @@ namespace cw
bool isShuttingDown( handle_t h ); bool isShuttingDown( handle_t h );
void report( handle_t h ); void report( handle_t h );
//----------------------------------------------------------------------------------------------------------
//
// Thread
//
rc_t threadCreate( handle_t h, unsigned id, void* arg );
//---------------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------------
// //