From 2e61743ff0092032c2f2590b8333cea55758fd13 Mon Sep 17 00:00:00 2001 From: JP Cimalando Date: Sun, 24 Feb 2019 11:02:33 +0100 Subject: javaopl3: thread safe initialization and cleanup --- src/chips/java/JavaOPL3.hpp | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) (limited to 'src/chips/java/JavaOPL3.hpp') diff --git a/src/chips/java/JavaOPL3.hpp b/src/chips/java/JavaOPL3.hpp index a18266a..df71cab 100644 --- a/src/chips/java/JavaOPL3.hpp +++ b/src/chips/java/JavaOPL3.hpp @@ -47,6 +47,7 @@ #include #include #include +#include "../common/mutex.hpp" #ifndef UINT32_MAX #define UINT32_MAX 0xffffffffU @@ -602,6 +603,7 @@ private: void setRhythmMode(); static int InstanceCount; + static Mutex InstanceMutex; // OPLEmul interface public: @@ -614,6 +616,7 @@ public: OperatorDataStruct *OPL3::OperatorData; OPL3DataStruct *OPL3::OPL3Data; int OPL3::InstanceCount; +Mutex OPL3::InstanceMutex; void OPL3::Update(float *output, int numsamples) { while (numsamples--) { @@ -735,10 +738,13 @@ OPL3::OPL3(bool fullpan) nts = dam = dvb = ryt = bd = sd = tom = tc = hh = _new = connectionsel = 0; vibratoIndex = tremoloIndex = 0; - if (InstanceCount++ == 0) { - OPL3Data = new struct OPL3DataStruct; - OperatorData = new struct OperatorDataStruct; + MutexHolder lock(InstanceMutex); + if (InstanceCount++ == 0) + { + OPL3Data = new struct OPL3DataStruct; + OperatorData = new struct OperatorDataStruct; + } } initOperators(); @@ -770,12 +776,16 @@ OPL3::~OPL3() delete channels4op[array][channelNumber]; } } - if (--InstanceCount == 0) + { - delete OPL3Data; - OPL3Data = NULL; - delete OperatorData; - OperatorData = NULL; + MutexHolder lock(InstanceMutex); + if (--InstanceCount == 0) + { + delete OPL3Data; + OPL3Data = NULL; + delete OperatorData; + OperatorData = NULL; + } } } -- cgit v1.2.3