Add shared memory synchronization to make access safe

This commit is contained in:
Bill Somerville 2020-03-15 00:25:35 +00:00
parent 649a79dfc1
commit 46638f6f07
No known key found for this signature in database
GPG Key ID: D864B06D1E81618F
3 changed files with 17 additions and 4 deletions

View File

@ -16,7 +16,6 @@ subroutine jt9a()
integer*2 id2a(180000) integer*2 id2a(180000)
integer*1 attach_jt9 integer*1 attach_jt9
! integer*1 lock_jt9,unlock_jt9
integer size_jt9 integer size_jt9
! Multiple instances: ! Multiple instances:
character*80 mykey character*80 mykey
@ -41,11 +40,14 @@ subroutine jt9a()
call c_f_pointer(address_jt9(),shared_data) call c_f_pointer(address_jt9(),shared_data)
! Wait here until GUI has set ss(2,1) to 1.0 ! Wait here until GUI has set ss(2,1) to 1.0
10 if(shared_data%ss(2,1).eq.999.0) then 10 call lock_jt9()
if(shared_data%ss(2,1).eq.999.0) then
call unlock_jt9()
i1=detach_jt9() i1=detach_jt9()
go to 999 go to 999
endif endif
if(shared_data%ss(2,1).ne.1.0) then if(shared_data%ss(2,1).ne.1.0) then
call unlock_jt9()
call sleep_msec(msdelay) call sleep_msec(msdelay)
go to 10 go to 10
endif endif
@ -53,11 +55,13 @@ subroutine jt9a()
nbytes=size_jt9() nbytes=size_jt9()
if(nbytes.le.0) then if(nbytes.le.0) then
call unlock_jt9()
print*,'jt9a: Shared memory mem_jt9 does not exist.' print*,'jt9a: Shared memory mem_jt9 does not exist.'
print*,"Must start 'jt9 -s <thekey>' from within WSJT-X." print*,"Must start 'jt9 -s <thekey>' from within WSJT-X."
go to 999 go to 999
endif endif
local_params=shared_data%params !save a copy because wsjtx carries on accessing local_params=shared_data%params !save a copy because wsjtx carries on accessing
call unlock_jt9()
call flush(6) call flush(6)
call timer('decoder ',0) call timer('decoder ',0)
if(local_params%nmode.eq.8 .and. local_params%ndiskdat) then if(local_params%nmode.eq.8 .and. local_params%ndiskdat) then
@ -80,11 +84,14 @@ subroutine jt9a()
! Wait here until GUI routine decodeDone() has set ss(3,1) to 1.0 ! Wait here until GUI routine decodeDone() has set ss(3,1) to 1.0
100 if(shared_data%ss(3,1).ne.1.0) then 100 call lock_jt9()
if(shared_data%ss(3,1).ne.1.0) then
call unlock_jt9()
call sleep_msec(msdelay) call sleep_msec(msdelay)
go to 100 go to 100
endif endif
shared_data%ss(3,1)=0. shared_data%ss(3,1)=0.
call unlock_jt9()
go to 10 go to 10
999 call timer('decoder ',101) 999 call timer('decoder ',101)

View File

@ -364,7 +364,9 @@ int main(int argc, char *argv[])
throw std::runtime_error {"Shared memory error"}; throw std::runtime_error {"Shared memory error"};
} }
} }
mem_jt9.lock ();
memset(mem_jt9.data(),0,sizeof(struct dec_data)); //Zero all decoding params in shared memory memset(mem_jt9.data(),0,sizeof(struct dec_data)); //Zero all decoding params in shared memory
mem_jt9.unlock ();
unsigned downSampleFactor; unsigned downSampleFactor;
{ {

View File

@ -3011,7 +3011,9 @@ void MainWindow::decode() //decode()
&narg[0],&m_TRperiod,&m_msg[0][0], &narg[0],&m_TRperiod,&m_msg[0][0],
dec_data.params.mycall,dec_data.params.hiscall,8000,12,12))); dec_data.params.mycall,dec_data.params.hiscall,8000,12,12)));
} else { } else {
mem_jt9->lock ();
memcpy(to, from, qMin(mem_jt9->size(), size)); memcpy(to, from, qMin(mem_jt9->size(), size));
mem_jt9->unlock ();
auto now = QDateTime::currentDateTimeUtc(); auto now = QDateTime::currentDateTimeUtc();
double tsec = fmod(double(now.toMSecsSinceEpoch()),86400000.0)/1000.0; double tsec = fmod(double(now.toMSecsSinceEpoch()),86400000.0)/1000.0;
@ -3072,11 +3074,13 @@ void::MainWindow::fast_decode_done()
void MainWindow::to_jt9(qint32 n, qint32 istart, qint32 idone) void MainWindow::to_jt9(qint32 n, qint32 istart, qint32 idone)
{ {
float ss0[3]; float ss0[3];
mem_jt9->lock ();
memcpy(ss0,(char*)mem_jt9->data(),12); memcpy(ss0,(char*)mem_jt9->data(),12);
ss0[0]=n; ss0[0]=n;
if(istart>=0) ss0[1]=istart; if(istart>=0) ss0[1]=istart;
if(idone>=0) ss0[2]=idone; if(idone>=0) ss0[2]=idone;
memcpy((char*)mem_jt9->data(),ss0,12); memcpy((char*)mem_jt9->data(),ss0,12);
mem_jt9->unlock ();
} }
void MainWindow::decodeDone () void MainWindow::decodeDone ()