Updated LibPdInstance.cs.

This commit is contained in:
Niall Moody 2021-10-03 13:51:04 +01:00
parent 4c17055789
commit 1b3c08893a

View file

@ -437,12 +437,15 @@ public class LibPdInstance : MonoBehaviour
/// We use this to ensure libpd -> Unity events get sent to all LibPdInstances.
/*!
It's also used to ensure libpd_queued_release() only gets called once
all LibPdInstances have been destroyed.
IMPORTANT: Bear in mind that C#'s List is not thread-safe. That's not a
problem for the way we're using it at the time of writing, because
activeInstances only gets accessed from the main thread in OnEnable,
OnDisable and the various BangOutput, FloatOutput, etc. delegates that
themselves get called from Update.
activeInstances should *NEVER* be accessed from the audio thread though.
*/
private static List<LibPdInstance> activeInstances = new List<LibPdInstance>();
/// The WeakReference pointing to ourselves in activeInstances.
//private WeakReference instanceReference;
/// True if we were unable to intialise Pd's audio processing.
private bool pdFail = false;
@ -451,8 +454,10 @@ public class LibPdInstance : MonoBehaviour
/// True if we have successfully loaded our patch.
private bool loaded = false;
/// Global variable used to ensure we don't initialise LibPd more than once.
/// Global variable used to ensure we don't initialise libpd more than once.
private static bool pdInitialised = false;
/// Global reference count used to determine when to un-initialise libpd.
private static int numInstances = 0;
#endregion
#region events
@ -617,7 +622,10 @@ public class LibPdInstance : MonoBehaviour
libpd_finish_message("pd", "dsp");
if(!patchFail)
{
loaded = true;
++numInstances;
}
}
}
}
@ -658,8 +666,10 @@ public class LibPdInstance : MonoBehaviour
libpd_free_instance(instance);
}
--numInstances;
//If we're the last instance left, release libpd's ringbuffer.
if(pdInitialised && (activeInstances.Count < 1))
if(pdInitialised && (numInstances < 1))
{
if(printHook != null)
{