# Why is AudioProcessorPlayer aquiring the callback lock when suspended

**URL:** <https://forum.juce.com/t/why-is-audioprocessorplayer-aquiring-the-callback-lock-when-suspended/21648>\
**Category:** Audio Plugins\
**Created:** [April 4, 2017, 12:20pm UTC](https://forum.juce.com/t/why-is-audioprocessorplayer-aquiring-the-callback-lock-when-suspended/21648 "2017-04-04T12:20:31Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrisboy2000](https://avatars.discourse-cdn.com/v4/letter/c/a3d4f5/32.png) [@chrisboy2000](https://forum.juce.com/u/chrisboy2000)\
**Post date:** [April 4, 2017, 12:20pm UTC](https://forum.juce.com/t/why-is-audioprocessorplayer-aquiring-the-callback-lock-when-suspended/21648/1 "2017-04-04T12:20:31Z")

</div>

From juce\_AudioProcessorPlayer.cpp:

```cpp
        if (processor != nullptr)
        {
            const ScopedLock sl2 (processor->getCallbackLock());

            if (! processor->isSuspended())
            {
                if (processor->isUsingDoublePrecision())
                {
                    conversionBuffer.makeCopyOf (buffer, true);
                    processor->processBlock (conversionBuffer, incomingMidi);
                    buffer.makeCopyOf (conversionBuffer, true);
                }
                else
                {
                    processor->processBlock (buffer, incomingMidi);
                }

                return;
            }
        }

```

As it’s stated in the docs, the `suspendProcessing` method is supposed to be used when a heavy task is to be performed. But this code:

```auto
suspendProcessing(true);

// Do something slow...
loadTeraBytesOfSamplesFromAUsb2Stick();

suspendProcessing(false);

```

causes a priority inversion and renders the whole suspendProcessing feature useless if the call between grabs the callback lock (which it should do for eg. reloading samples).

Am I missing something obvious?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [April 4, 2017, 12:47pm UTC](https://forum.juce.com/t/why-is-audioprocessorplayer-aquiring-the-callback-lock-when-suspended/21648/2 "2017-04-04T12:47:40Z")

</div>

Well that’s not how you’re supposed to use it!

You should never grab the callback lock for longer than an absolutely minimal time. The whole point of suspending the thread is that it lets you do these longer operations, but regardless of whether it’s suspended, you shouldn’t ever hold the callback lock.

---

<div class="post-metadata">

**Author:** ![chrisboy2000](https://avatars.discourse-cdn.com/v4/letter/c/a3d4f5/32.png) [@chrisboy2000](https://forum.juce.com/u/chrisboy2000)\
**Post date:** [April 4, 2017, 12:56pm UTC](https://forum.juce.com/t/why-is-audioprocessorplayer-aquiring-the-callback-lock-when-suspended/21648/3 "2017-04-04T12:56:11Z")

</div>

I am just grabbing the lock for deleting the sounds and each time I add a new sound to the synthesiser (after creating them, just for the array.add() method)

However, these run on a lower priority thread and I am getting some priority inversion warnings in my logger once in a while which got me curious.

I thought I was following the approach from JUCE’s own synthesiser class - although now that I am rereading the code, it uses it’s own critical section for changing voices / sounds instead of the global one. Is this the reason for that?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [April 4, 2017, 1:36pm UTC](https://forum.juce.com/t/why-is-audioprocessorplayer-aquiring-the-callback-lock-when-suspended/21648/4 "2017-04-04T13:36:02Z")

</div>

Yes, the actual audio processor callback lock is only to be used as an absolutely last resort, and must never ever held for more than an absolutely trivial, non-blocking operation.

---

<div class="post-metadata">

**Author:** ![chrisboy2000](https://avatars.discourse-cdn.com/v4/letter/c/a3d4f5/32.png) [@chrisboy2000](https://forum.juce.com/u/chrisboy2000)\
**Post date:** [April 4, 2017, 3:50pm UTC](https://forum.juce.com/t/why-is-audioprocessorplayer-aquiring-the-callback-lock-when-suspended/21648/5 "2017-04-04T15:50:51Z")

</div>

Alright, thanks for clearing that up. I was perhaps a bit too relaxed with throwing around CriticalSections and ScopedLocks. I am not using the getCallbackLock() method directly, so changing this won’t cause too much trouble.

Am I right in assuming that if the global callback lock is held by eg. the message thread, it could cause nasty behavior because the processBlock callback isn’t called and this may cause uninitialized buffer data being passed through the plugin?

If we’re at it, what would you consider as best practice for adding modules (filters, modulators, etc at runtime)?

1. Use a global CriticalSection (that is not the callback lock) that locks during insertion of the module into the array. Adding these modules is not supposed to happen while normal playback so it is not 100% important that it is super smooth. As long as the processBlock method is being called and has a chance to at least clear the buffers it’s getting from the host?
2. Add a local CriticalSection that is unique to the holder of the array? This may slow things down when there are many modules as it will call the enterLock function more often for different CriticalSections (I am assuming that acquiring a lock is slower if the current thread doesn’t have it already).
3. Suspend the processing while inserting and reenable it after the operation? In this case, maybe something like a ScopedSuspender could come in useful:

```cpp

{
    ScopedSuspender ss(processor); // Calls processor->suspendProcessing(true) on creation

    // Do some stuff

} // Calls processor->suspendProcessing(false) on deletion

```

Edit: By global I mean owned by the main processor - not actually being a global variable 🙂

---

<div class="post-metadata">

**Author:** ![fr810](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/fr810/32/841_2.png) [@fr810](https://forum.juce.com/u/fr810)\
**Post date:** [April 7, 2017, 11:54am UTC](https://forum.juce.com/t/why-is-audioprocessorplayer-aquiring-the-callback-lock-when-suspended/21648/6 "2017-04-07T11:54:58Z")

</div>

The audio plugin host uses your third suggestion which is probably the best. I like your idea of a ScopedSuspender. I’ll add this to our backlog.

---

<div class="post-metadata">

**Author:** ![chrisboy2000](https://avatars.discourse-cdn.com/v4/letter/c/a3d4f5/32.png) [@chrisboy2000](https://forum.juce.com/u/chrisboy2000)\
**Post date:** [April 7, 2017, 12:49pm UTC](https://forum.juce.com/t/why-is-audioprocessorplayer-aquiring-the-callback-lock-when-suspended/21648/7 "2017-04-07T12:49:11Z")

</div>

Alright, thanks for clearing that up. Come to think of it, the difference between 1. and 3. is rather academic if you use the ScopedTryLock (you’ll fail to enter instead of getting false from suspendProcessing, but the resulting behaviour should be the same).

This might be a starting point for the ScopedSuspender class - it’s not exactly rocket science 🙂

```cpp
/** A simple RAII class that suspends the given AudioProcessor on construction and "unsuspends" it when it goes out of scope. 
*
*	By using this class you make sure you don't leave your AudioProcessor hanging around being accidently suspended.
*/
class ScopedSuspender
{
public:

	ScopedSuspender(AudioProcessor* processorToSuspend) :
		p(processorToSuspend)
	{
		p->suspendProcessing(true);
	}

	~ScopedSuspender()
	{
		p->suspendProcessing(false);
	};

private:

	AudioProcessor* p;

};

```

I would prefer using a WeakReference as member in case the AudioProcessor gets deleted in the mean time, but the AudioProcessor class doesn’t support WeakReferences yet (as you need to define the friend class stuff in it’s private section).

---

<div class="post-metadata">

**Author:** ![chkn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/chkn/32/3862_2.png) [@chkn](https://forum.juce.com/u/chkn)\
**Post date:** [April 8, 2017, 8:11am UTC](https://forum.juce.com/t/why-is-audioprocessorplayer-aquiring-the-callback-lock-when-suspended/21648/8 "2017-04-08T08:11:21Z")

</div>

Another suggestion, the scoped-suspender should check also for re-entrance

---

<div class="post-metadata">

**Author:** ![yfede](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/yfede/32/506_2.png) [@yfede](https://forum.juce.com/u/yfede)\
**Post date:** [April 10, 2017, 9:01am UTC](https://forum.juce.com/t/why-is-audioprocessorplayer-aquiring-the-callback-lock-when-suspended/21648/9 "2017-04-10T09:01:03Z")

</div>

> [@chkn](#):
>
> the scoped-suspender should check also for re-entrance

Better than that, it should be the `suspendProcessing()` method that cares for reentrancy, so that if you call it twice with `true`, then you have to call it twice with `false` to actually resume from suspended state.

In other words, it should internally keep a counter rather than a simple boolean value, and the `AudioProcessor` should be suspended whenever the counter is bigger than 0

(and assert like crazy in case it gets decremented to a negative value)

---

<div class="post-metadata">

**Author:** ![railjonrogut](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/railjonrogut/32/505_2.png) [@railjonrogut](https://forum.juce.com/u/railjonrogut)\
**Post date:** [April 10, 2017, 9:37am UTC](https://forum.juce.com/t/why-is-audioprocessorplayer-aquiring-the-callback-lock-when-suspended/21648/10 "2017-04-10T09:37:48Z")

</div>

I’ve done a very rudimentary implementation of that in my plugin:

```
void CMyAudioProcessor::incLoadInstrCounter()
{
    ++m_iLoadInstrRefCounter;

    suspendProcessing (true);
    m_pGraph->suspendProcessing (true);
}

 void CMyAudioProcessor::decLoadInstrCounter()
{
    --m_iLoadInstrRefCounter;

    if (m_iLoadInstrRefCounter < 0)
        m_iLoadInstrRefCounter = 0;

    if (! m_iLoadInstrRefCounter)
        {
        m_pGraph->suspendProcessing (false);
        suspendProcessing (false);
        }
}

```

Rail
