From 46638f6f0790156334890a650a6337b3d6346afd Mon Sep 17 00:00:00 2001 From: Bill Somerville Date: Sun, 15 Mar 2020 00:25:35 +0000 Subject: [PATCH] Add shared memory synchronization to make access safe --- lib/jt9a.f90 | 15 +++++++++++---- main.cpp | 2 ++ widgets/mainwindow.cpp | 4 ++++ 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/jt9a.f90 b/lib/jt9a.f90 index 5ea7ac949..13765e9b1 100644 --- a/lib/jt9a.f90 +++ b/lib/jt9a.f90 @@ -16,7 +16,6 @@ subroutine jt9a() integer*2 id2a(180000) integer*1 attach_jt9 -! integer*1 lock_jt9,unlock_jt9 integer size_jt9 ! Multiple instances: character*80 mykey @@ -41,11 +40,14 @@ subroutine jt9a() call c_f_pointer(address_jt9(),shared_data) ! 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() go to 999 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) go to 10 endif @@ -53,11 +55,13 @@ subroutine jt9a() nbytes=size_jt9() if(nbytes.le.0) then + call unlock_jt9() print*,'jt9a: Shared memory mem_jt9 does not exist.' print*,"Must start 'jt9 -s ' from within WSJT-X." go to 999 endif local_params=shared_data%params !save a copy because wsjtx carries on accessing + call unlock_jt9() call flush(6) call timer('decoder ',0) 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 -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) go to 100 endif shared_data%ss(3,1)=0. + call unlock_jt9() go to 10 999 call timer('decoder ',101) diff --git a/main.cpp b/main.cpp index fc791adec..74174e378 100644 --- a/main.cpp +++ b/main.cpp @@ -364,7 +364,9 @@ int main(int argc, char *argv[]) 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 + mem_jt9.unlock (); unsigned downSampleFactor; { diff --git a/widgets/mainwindow.cpp b/widgets/mainwindow.cpp index b4c59f79d..52394c8a5 100644 --- a/widgets/mainwindow.cpp +++ b/widgets/mainwindow.cpp @@ -3011,7 +3011,9 @@ void MainWindow::decode() //decode() &narg[0],&m_TRperiod,&m_msg[0][0], dec_data.params.mycall,dec_data.params.hiscall,8000,12,12))); } else { + mem_jt9->lock (); memcpy(to, from, qMin(mem_jt9->size(), size)); + mem_jt9->unlock (); auto now = QDateTime::currentDateTimeUtc(); 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) { float ss0[3]; + mem_jt9->lock (); memcpy(ss0,(char*)mem_jt9->data(),12); ss0[0]=n; if(istart>=0) ss0[1]=istart; if(idone>=0) ss0[2]=idone; memcpy((char*)mem_jt9->data(),ss0,12); + mem_jt9->unlock (); } void MainWindow::decodeDone ()