Skip to content

Commit

Permalink
Core: possible fix for audio.onComplete (#657)
Browse files Browse the repository at this point in the history
  • Loading branch information
ggcrunchy committed Dec 5, 2023
1 parent 7abc353 commit 1beb40a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
51 changes: 49 additions & 2 deletions librtt/Rtt_Scheduler.cpp
Expand Up @@ -12,6 +12,8 @@
#include "Rtt_Scheduler.h"
#include "Rtt_Runtime.h"

#include <mutex>

// ----------------------------------------------------------------------------

namespace Rtt
Expand All @@ -25,17 +27,22 @@ Task::~Task()

template class Rtt::Array<Rtt::Task *>;

static std::mutex sListMutex;
static std::mutex sTaskMutex;

// ----------------------------------------------------------------------------

Scheduler::Scheduler( Runtime& owner )
: fOwner( owner ),
fFirstPending( NULL ),
fProcessing( false ),
fTasks( owner.GetAllocator() )
{
}

Scheduler::~Scheduler()
{
SyncPendingList();
}

#if 0
Expand All @@ -51,13 +58,21 @@ void
Scheduler::Append( Task* e )
{
//Rtt_ASSERT( fProcessing == false ); //**tjn removed

fTasks.Append( e );

std::lock_guard<std::mutex> lock( sListMutex );

e->setNext( fFirstPending );

fFirstPending = e;
}

void
Scheduler::Delete(Task* e)
{
SyncPendingList();

std::lock_guard<std::mutex> lock( sTaskMutex );

int i = 0;
while (i < fTasks.Length())
{
Expand All @@ -78,6 +93,10 @@ Scheduler::Run()
{
fProcessing = true;

SyncPendingList();

std::lock_guard<std::mutex> lock( sTaskMutex );

int i = 0;
while (i < fTasks.Length())
{
Expand All @@ -99,6 +118,34 @@ Scheduler::Run()
fProcessing = false;
}

Task*
Scheduler::ExtractPendingList()
{
std::lock_guard<std::mutex> lock( sListMutex );

Task* first = fFirstPending;

fFirstPending = NULL;

return first;
}

void
Scheduler::SyncPendingList()
{
Task* first = ExtractPendingList();

if ( NULL != first )
{
std::lock_guard<std::mutex> lock( sTaskMutex );

for ( Task* cur = first; cur; cur = cur->getNext() )
{
fTasks.Append( cur );
}
}
}

// ----------------------------------------------------------------------------

} // namespace Rtt
Expand Down
11 changes: 10 additions & 1 deletion librtt/Rtt_Scheduler.h
Expand Up @@ -25,7 +25,7 @@ class Scheduler;
class Task
{
public:
Task() : fKeepAlive(false) {}
Task() : fKeepAlive(false), fNext(NULL) {}
Task(bool keepAlive) : fKeepAlive(keepAlive) {}
virtual ~Task();

Expand All @@ -35,8 +35,12 @@ class Task
bool getKeepAlive() const { return fKeepAlive; }
void setKeepAlive(bool val) { fKeepAlive = val; }

Task* getNext() const { return fNext; }
void setNext( Task* next ) { fNext = next; }

private:
bool fKeepAlive;
Task* fNext;
};

class Scheduler
Expand All @@ -59,8 +63,13 @@ class Scheduler
public:
Owner& GetOwner() { return fOwner; }

private:
Task* ExtractPendingList();
void SyncPendingList();

private:
Owner& fOwner;
Task* fFirstPending;

PtrArray< Task > fTasks;
bool fProcessing;
Expand Down

0 comments on commit 1beb40a

Please sign in to comment.