cwIo.cpp : Added use of atomic isStartedFl

This commit is contained in:
kevin 2023-03-21 08:27:32 -04:00
parent 6c97b62d6f
commit ab40711eee

View File

@ -111,6 +111,7 @@ namespace cw
typedef struct io_str
{
std::atomic<bool> quitFl;
std::atomic<bool> startedFl;
time::spec_t t0;
@ -180,6 +181,10 @@ namespace cw
rc_t rc = kOkRC;
bool unlockFl = false;
bool isSynchronousFl = !asyncFl;
bool isStartedFl = p->startedFl.load();
if( isStartedFl )
{
// if this is a synchronous callback then lock the mutex
if( isSynchronousFl )
@ -221,7 +226,7 @@ namespace cw
rc = cwLogError(rc,"io mutex callback mutex unlock failed.");
}
}
}
return rc;
}
@ -2233,6 +2238,7 @@ cw::rc_t cw::io::create(
p->quitFl.store(false);
p->startedFl.store(false);
time::get(p->t0);
h.set(p);
@ -2264,19 +2270,46 @@ cw::rc_t cw::io::destroy( handle_t& h )
cw::rc_t cw::io::start( handle_t h )
{
rc_t rc = kOkRC;
io_t* p = _handleToPtr(h);
_audioDeviceStartStop(p,true);
if((rc = _audioDeviceStartStop(p,true)) != kOkRC )
goto errLabel;
_serialPortStart(p);
if((rc = _serialPortStart(p)) != kOkRC )
goto errLabel;
return thread_mach::start( p->threadMachH );
if((rc = thread_mach::start( p->threadMachH )) != kOkRC )
{
cwLogError(rc,"Thread machine start failed.");
goto errLabel;
}
p->startedFl.store(true);
errLabel:
if( rc != kOkRC )
stop(h);
return rc;
}
cw::rc_t cw::io::pause( handle_t h )
{
io_t* p = _handleToPtr(h);
return thread_mach::stop( p->threadMachH );
rc_t rc;
if((rc = thread_mach::stop( p->threadMachH )) != kOkRC )
{
cwLogError(rc,"Thread machine stop failed.");
goto errLabel;
}
p->startedFl.store(false);
errLabel:
return rc;
}
cw::rc_t cw::io::stop( handle_t h )
@ -2284,16 +2317,21 @@ cw::rc_t cw::io::stop( handle_t h )
io_t* p = _handleToPtr(h);
p->quitFl.store(true);
thread_mach::stop(p->threadMachH );
rc_t rc0 = thread_mach::stop(p->threadMachH );
// stop the audio devices
_audioDeviceStartStop(p,false);
rc_t rc1 = _audioDeviceStartStop(p,false);
// clear the UI
//if( p->wsUiH.isValid() )
// uiDestroyElement(h,ui::kRootUuId);
return kOkRC;
rc_t rc = rcSelect(rc0,rc1);
if(rc == kOkRC )
p->startedFl.store(false);
return rc;
}
cw::rc_t cw::io::exec( handle_t h, void* execCbArg )