Bug 1338924 - Add GMPTask that takes/runs a lambda and a simple mechanism to set a timer on a lambda task. r=gerald

I'll use this for timers in a later patch.

MozReview-Commit-ID: J9QsMmrAWoI
This commit is contained in:
Chris Pearce 2017-02-14 11:09:38 +13:00
parent 4c05a6c492
commit c6244a170d
2 changed files with 66 additions and 31 deletions

View File

@ -8,6 +8,10 @@
#include "GMPTimerChild.h"
#include "mozilla/Monitor.h"
#include "GMPChild.h"
#include "mozilla/Mutex.h"
#include "base/thread.h"
#include "mozilla/ReentrantMonitor.h"
#include <ctime>
namespace mozilla {
@ -100,6 +104,21 @@ private:
Monitor mMonitor;
};
class GMPThreadImpl : public GMPThread
{
public:
GMPThreadImpl();
virtual ~GMPThreadImpl();
// GMPThread
void Post(GMPTask* aTask) override;
void Join() override;
private:
Mutex mMutex;
base::Thread mThread;
};
GMPErr
CreateThread(GMPThread** aThread)
{
@ -139,6 +158,21 @@ SyncRunOnMainThread(GMPTask* aTask)
return GMPNoErr;
}
class GMPMutexImpl : public GMPMutex
{
public:
GMPMutexImpl();
virtual ~GMPMutexImpl();
// GMPMutex
void Acquire() override;
void Release() override;
void Destroy() override;
private:
ReentrantMonitor mMonitor;
};
GMPErr
CreateMutex(GMPMutex** aMutex)
{
@ -280,5 +314,32 @@ GMPMutexImpl::Release()
mMonitor.Exit();
}
GMPTask*
NewGMPTask(std::function<void()>&& aFunction)
{
class Task : public GMPTask
{
public:
explicit Task(std::function<void()>&& aFunction)
: mFunction(Move(aFunction))
{
}
void Destroy() override
{
delete this;
}
~Task() override
{
}
void Run() override
{
mFunction();
}
private:
std::function<void()> mFunction;
};
return new Task(Move(aFunction));
}
} // namespace gmp
} // namespace mozilla

View File

@ -6,10 +6,8 @@
#ifndef GMPPlatform_h_
#define GMPPlatform_h_
#include "mozilla/Mutex.h"
#include "gmp-platform.h"
#include "base/thread.h"
#include "mozilla/ReentrantMonitor.h"
#include <functional>
namespace mozilla {
namespace gmp {
@ -20,35 +18,11 @@ void InitPlatformAPI(GMPPlatformAPI& aPlatformAPI, GMPChild* aChild);
GMPErr RunOnMainThread(GMPTask* aTask);
class GMPThreadImpl : public GMPThread
{
public:
GMPThreadImpl();
virtual ~GMPThreadImpl();
GMPTask*
NewGMPTask(std::function<void()>&& aFunction);
// GMPThread
void Post(GMPTask* aTask) override;
void Join() override;
private:
Mutex mMutex;
base::Thread mThread;
};
class GMPMutexImpl : public GMPMutex
{
public:
GMPMutexImpl();
virtual ~GMPMutexImpl();
// GMPMutex
void Acquire() override;
void Release() override;
void Destroy() override;
private:
ReentrantMonitor mMonitor;
};
GMPErr
SetTimerOnMainThread(GMPTask* aTask, int64_t aTimeoutMS);
} // namespace gmp
} // namespace mozilla