Thread sync: Managed equivalent to pCB->Lock() ?

Post Reply
User avatar
IXix
Posts: 1004
Joined: Wed Nov 23, 2011 3:24 pm

Thread sync: Managed equivalent to pCB->Lock() ?

Post by IXix »

I'm making a managed machine to store/restore machine states (param, attributes and data) like PeerState and it works but sometime it causes Buzz to hang. I guess I need to lock the audio thread while I shuffle state data around but how do I do that in C#? What is the C# equivalent of CMICallbacks::Lock() and Unlock()?

Also, I'm hoping to be able to do this in response to a parameter change as well as from the GUI so how would I do a state capture/restore when a param change is detected without causing a meltdown? Any advice much appreciated. :geek:

wde
Posts: 283
Joined: Sun Jan 08, 2012 9:28 am

Re: Thread sync: Managed equivalent to pCB->Lock() ?

Post by wde »

I use this type of lock mechanism in AudioBlock:

Code: Select all

static object syncLock = new object();

public void Work()
{
	lock (syncLock)
	{
	// Things that need to be safe
	}
}
This might help with the GUI thread thing:

Code: Select all

Application.Current.Dispatcher.BeginInvoke((Action)(() =>
{
	// Do stuff here
}));
Can't wait to try your Snapshot machine to save different states of VSTs!

User avatar
UNZ
Posts: 760
Joined: Mon Nov 21, 2011 9:42 pm
Contact:

Re: Thread sync: Managed equivalent to pCB->Lock() ?

Post by UNZ »

wde wrote:
Thu Oct 07, 2021 12:57 pm
static object syncLock = new object();
yes, this works for synchronizing your own machine's threads, where you can lock all your own threads with your own object. But note that you make your own lock object here, so this will not work for locking the state of OTHER machines and threads which you don't own. The other machines don't know anything about your lock object (and neither does buzz). You need to use THEIR lock objects. that's why machineinterface has the lock functions..there must be some equivalent in the .net api and if not, call the native function from .net.

that said, dispatching to the right buzz thread and doing it there might work... but it has to be the one where buzz calls these functions on usually, and again you should take the same locks buzz takes while calling them..

User avatar
IXix
Posts: 1004
Joined: Wed Nov 23, 2011 3:24 pm

Re: Thread sync: Managed equivalent to pCB->Lock() ?

Post by IXix »

wde wrote:
Thu Oct 07, 2021 12:57 pm
... helpful stuff...
Thanks! I'm out of my depth just finding my way around C#, .NET AND WPF. I spent ten minutes poking around BuzzGUI looking for how to implement Tick() until I realised my mistake. :oops: :lol:
wde wrote:
Thu Oct 07, 2021 12:57 pm
Can't wait to try your Snapshot machine to save different states of VSTs!
It's amazing! I hadn't considered it before but it doesn't save the VST, it saves PVST which means a snapshot can load an entirely different plugin! :shock:
UNZ wrote:
Thu Oct 07, 2021 2:05 pm
if not, call the native function from .net.
How would I go about doing that?
UNZ wrote:
Thu Oct 07, 2021 2:05 pm
that said, dispatching to the right buzz thread and doing it there might work... but it has to be the one where buzz calls these functions on usually, and again you should take the same locks buzz takes while calling them..
Whenever I intercept a call from a button click, the thread id is 1 so I guess that's the one I'm aiming for. The invoke thing wde posted above looks promising (and easy) so I'll give that a try first. Wish me luck!

btw. UNZ I think you missed a message but it's not urgent now, since I started doing this. I'd like to get that hack working eventually but it can wait.

User avatar
UNZ
Posts: 760
Joined: Mon Nov 21, 2011 9:42 pm
Contact:

Re: Thread sync: Managed equivalent to pCB->Lock() ?

Post by UNZ »

yeah sorry i just saw the message.

i'll take a look one of these days, a bit busy now

User avatar
IXix
Posts: 1004
Joined: Wed Nov 23, 2011 3:24 pm

Re: Thread sync: Managed equivalent to pCB->Lock() ?

Post by IXix »

wde wrote:
Thu Oct 07, 2021 12:57 pm
This might help with the GUI thread thing:

Code: Select all

Application.Current.Dispatcher.BeginInvoke((Action)(() =>
{
	// Do stuff here
}));
Hmmm, Application doesn't seem to have a 'Current' (different .NET version?) I did find 'BuzzGUI.Common.DispatcherAction' which looks like it does something similar. Nothing happens when I do this but there's a strong chance that I'm doing it completely wrong...

Code: Select all

var da = new DispatcherAction(() =>
{
   MessageBox.Show("Hello!");
});
da.Dispatch(System.Windows.Threading.DispatcherPriority.Normal);
I can't find anything that looks like a machine lock anywhere in BuzzGUI. :(

User avatar
IXix
Posts: 1004
Joined: Wed Nov 23, 2011 3:24 pm

Re: Thread sync: Managed equivalent to pCB->Lock() ?

Post by IXix »

IXix wrote:
Fri Oct 08, 2021 6:12 pm
Hmmm, Application doesn't seem to have a 'Current'
Ignore that idiot from last night, he doesn't know what he's doing. Must've been missing a reference in a particular file because I just found it while trying stuff elsewhere. Early signs are promising, I just restored a single global param on one machine in response to a slot change and it didn't lock up like it always has done before! :dance:

edit: Totally working! :dance: :dance: :dance: :dance:

Post Reply