+static void do_samples_finish(int ns_to, int silentch, int decode_pos);
+
+// optional worker thread handling
+
+#ifdef THREAD_ENABLED
+
+static void queue_channel_work(int ns_to, int silentch)
+{
+ unsigned int mask;
+ int ch;
+
+ worker->ns_to = ns_to;
+ worker->ctrl = spu.spuCtrl;
+ worker->decode_pos = spu.decode_pos;
+ worker->silentch = silentch;
+ worker->sRVBStart = spu.sRVBStart;
+ worker->ram = spu.spuMemC;
+
+ mask = worker->chmask = spu.dwChannelOn & 0xffffff;
+ for (ch = 0; mask != 0; ch++, mask >>= 1)
+ {
+ if (!(mask & 1)) continue;
+
+ worker->ch[ch].spos = s_chan[ch].spos;
+ worker->ch[ch].sbpos = s_chan[ch].iSBPos;
+ worker->ch[ch].sinc = s_chan[ch].sinc;
+ worker->ch[ch].adsr = s_chan[ch].ADSRX;
+ worker->ch[ch].start = s_chan[ch].pCurr - spu.spuMemC;
+ worker->ch[ch].loop = s_chan[ch].pLoop - spu.spuMemC;
+ if (s_chan[ch].prevflags & 1)
+ worker->ch[ch].start = worker->ch[ch].loop;
+
+ worker->ch[ch].ns_to = do_samples_skip(ch, ns_to);
+ }
+
+ worker->pending = 1;
+ sem_post(&worker->sem_avail);
+}
+
+static void do_channel_work(void)
+{
+ unsigned int mask, endmask = 0;
+ unsigned int decode_dirty_ch = 0;
+ int *SB, sinc, spos, sbpos;
+ int d, ch, ns_to;
+
+ ns_to = worker->ns_to;
+ memset(worker->sRVBStart, 0, ns_to * sizeof(worker->sRVBStart[0]) * 2);
+
+ mask = worker->chmask;
+ for (ch = 0; mask != 0; ch++, mask >>= 1)
+ {
+ if (!(mask & 1)) continue;
+
+ d = worker->ch[ch].ns_to;
+ spos = worker->ch[ch].spos;
+ sbpos = worker->ch[ch].sbpos;
+ sinc = worker->ch[ch].sinc;
+ SB = s_chan[ch].SB;
+
+ if (s_chan[ch].bNoise)
+ do_lsfr_samples(d, worker->ctrl, &spu.dwNoiseCount, &spu.dwNoiseVal);
+ else if (s_chan[ch].bFMod == 2
+ || (s_chan[ch].bFMod == 0 && spu_config.iUseInterpolation == 0))
+ do_samples_noint(decode_block_work, ch, d, SB, sinc, &spos, &sbpos);
+ else if (s_chan[ch].bFMod == 0 && spu_config.iUseInterpolation == 1)
+ do_samples_simple(decode_block_work, ch, d, SB, sinc, &spos, &sbpos);
+ else
+ do_samples_default(decode_block_work, ch, d, SB, sinc, &spos, &sbpos);
+
+ d = MixADSR(&worker->ch[ch].adsr, d);
+ if (d < ns_to) {
+ endmask |= 1 << ch;
+ worker->ch[ch].adsr.EnvelopeVol = 0;
+ memset(&ChanBuf[d], 0, (ns_to - d) * sizeof(ChanBuf[0]));
+ }
+
+ if (ch == 1 || ch == 3)
+ {
+ do_decode_bufs((void *)worker->ram, ch/2, ns_to, worker->decode_pos);
+ decode_dirty_ch |= 1 << ch;
+ }
+
+ if (s_chan[ch].bFMod == 2) // fmod freq channel
+ memcpy(iFMod, &ChanBuf, ns_to * sizeof(iFMod[0]));
+ if (s_chan[ch].bRVBActive)
+ mix_chan_rvb(0, ns_to, s_chan[ch].iLeftVolume, s_chan[ch].iRightVolume, worker->sRVBStart);
+ else
+ mix_chan(0, ns_to, s_chan[ch].iLeftVolume, s_chan[ch].iRightVolume);
+ }
+
+ worker->r_chan_end = endmask;
+ worker->r_decode_dirty = decode_dirty_ch;
+}
+
+static void sync_worker_thread(void)
+{
+ unsigned int mask;
+ int ch;
+
+ if (!worker->pending)
+ return;
+
+ sem_wait(&worker->sem_done);
+ worker->pending = 0;
+
+ mask = worker->chmask;
+ for (ch = 0; mask != 0; ch++, mask >>= 1) {
+ if (!(mask & 1)) continue;
+
+ // be sure there was no keyoff while thread was working
+ if (s_chan[ch].ADSRX.State != ADSR_RELEASE)
+ s_chan[ch].ADSRX.State = worker->ch[ch].adsr.State;
+ s_chan[ch].ADSRX.EnvelopeVol = worker->ch[ch].adsr.EnvelopeVol;
+ }
+
+ spu.dwChannelOn &= ~worker->r_chan_end;
+ spu.decode_dirty_ch |= worker->r_decode_dirty;
+
+ do_samples_finish(worker->ns_to, worker->silentch,
+ worker->decode_pos);
+}
+
+#else
+
+static void queue_channel_work(int ns_to, int silentch) {}
+static void sync_worker_thread(void) {}
+
+#endif // THREAD_ENABLED
+