Beatmup
Beatmup::Context Class Reference

Basic class: task and memory management, any kind of static data. More...

#include <context.h>

Inheritance diagram for Beatmup::Context:
Beatmup::Object Beatmup::Android::Context

Classes

class  EventListener
 An event listener (bunch of callbacks) More...
 
class  Impl
 Context class implementation (pimpl) More...
 

Public Member Functions

 Context ()
 
 Context (const PoolIndex numThreadPools)
 
 ~Context ()
 
float performTask (AbstractTask &task, const PoolIndex pool=DEFAULT_POOL)
 Performs a given task. More...
 
void repeatTask (AbstractTask &task, bool abortCurrent, const PoolIndex pool=DEFAULT_POOL)
 Ensures a given task executed at least once. More...
 
Job submitTask (AbstractTask &task, const PoolIndex pool=DEFAULT_POOL)
 Adds a new task to the jobs queue. More...
 
Job submitPersistentTask (AbstractTask &task, const PoolIndex pool=DEFAULT_POOL)
 Adds a new persistent task to the jobs queue. More...
 
void waitForJob (Job job, const PoolIndex pool=DEFAULT_POOL)
 Waits until a given job finishes. More...
 
bool abortJob (Job job, const PoolIndex pool=DEFAULT_POOL)
 Aborts a given submitted job. More...
 
void wait (const PoolIndex pool=DEFAULT_POOL)
 Blocks until all the submitted jobs are executed. More...
 
bool busy (const PoolIndex pool=DEFAULT_POOL)
 Queries whether a given thread pool is busy with a task. More...
 
void check (const PoolIndex pool=DEFAULT_POOL)
 Checks if a specific thread pool is doing great: rethrows exceptions occurred during tasks execution, if any. More...
 
const ThreadIndex maxAllowedWorkerCount (const PoolIndex pool=DEFAULT_POOL) const
 
void limitWorkerCount (ThreadIndex maxValue, const PoolIndex pool=DEFAULT_POOL)
 Limits maximum number of threads (workers) when performing tasks in a given pool. More...
 
void setEventListener (EventListener *eventListener)
 Installs new event listener. More...
 
EventListenergetEventListener () const
 Returns current event listener (or NULL) More...
 
bool isGpuQueried () const
 
bool isGpuReady () const
 
void warmUpGpu ()
 Initializes GPU within a given Context if not yet (takes no effect if it already is). More...
 
bool queryGpuInfo (std::string &vendor, std::string &renderer)
 Initializes the GPU if not yet and queries information about it. More...
 
bool isManagingThread () const
 
GL::RecycleBingetGpuRecycleBin () const
 
bool operator== (const Context &context) const
 Context comparaison operator Two different instances of contexts are basically never identical; returning true only if the two point to the same instance. More...
 
- Public Member Functions inherited from Beatmup::Object
virtual ~Object ()
 

Static Public Attributes

static const PoolIndex DEFAULT_POOL = 0
 

Private Member Functions

 Context (const Context &)=delete
 disabling copying constructor More...
 

Private Attributes

Implimpl
 
GL::RecycleBinrecycleBin
 stores GPU garbage: resources managed by GPU and might be freed in the managing thread only More...
 

Detailed Description

Basic class: task and memory management, any kind of static data.

Definition at line 59 of file context.h.

Constructor & Destructor Documentation

◆ Context() [1/3]

Beatmup::Context::Context ( const Context )
privatedelete

disabling copying constructor

◆ Context() [2/3]

Context::Context ( )

Definition at line 226 of file context.cpp.

226 : Context(1) {}

◆ Context() [3/3]

Context::Context ( const PoolIndex  numThreadPools)

Definition at line 227 of file context.cpp.

227  {
228  impl = new Impl(numThreadPools);
229  recycleBin = new GL::RecycleBin(*this);
230 }
GL::RecycleBin * recycleBin
stores GPU garbage: resources managed by GPU and might be freed in the managing thread only
Definition: context.h:64
Impl * impl
Definition: context.h:62
Stores references to GPU resources that will not be used anymore and needed to be recycled in a threa...
Definition: recycle_bin.h:34

◆ ~Context()

Context::~Context ( )

Definition at line 233 of file context.cpp.

233  {
234  if (recycleBin)
235  recycleBin->emptyBin();
236  delete recycleBin;
237  delete impl;
238 }
void emptyBin()
Empty the bin destroying all the items in a GPU-aware thread.
Definition: recycle_bin.cpp:91

Member Function Documentation

◆ performTask()

float Context::performTask ( AbstractTask task,
const PoolIndex  pool = DEFAULT_POOL 
)

Performs a given task.

Parameters
taskThe task
poolA thread pool to run the task in
Returns
task execution time

Definition at line 240 of file context.cpp.

240  {
241  return impl->performTask(pool, task);
242 }
float performTask(PoolIndex pool, AbstractTask &task)
Definition: context.cpp:137
Beatmup::NNets::InferenceTask * task

◆ repeatTask()

void Context::repeatTask ( AbstractTask task,
bool  abortCurrent,
const PoolIndex  pool = DEFAULT_POOL 
)

Ensures a given task executed at least once.

Parameters
taskThe task
abortCurrentif true and the same task is currently running, the abort signal is sent.
poolA thread pool to run the task in

Definition at line 244 of file context.cpp.

244  {
245  return impl->repeatTask(pool, task, abortCurrent);
246 }
void repeatTask(PoolIndex pool, AbstractTask &task, bool abortCurrent)
Definition: context.cpp:148

◆ submitTask()

Job Context::submitTask ( AbstractTask task,
const PoolIndex  pool = DEFAULT_POOL 
)

Adds a new task to the jobs queue.

Parameters
taskThe task
poolA thread pool to run the task in

Definition at line 248 of file context.cpp.

248  {
249  return impl->submitTask(pool, task);
250 }
Job submitTask(const PoolIndex pool, AbstractTask &task)
Definition: context.cpp:154

◆ submitPersistentTask()

Job Context::submitPersistentTask ( AbstractTask task,
const PoolIndex  pool = DEFAULT_POOL 
)

Adds a new persistent task to the jobs queue.

Persistent task is repeated until it decides itself to quit.

Parameters
taskThe task
poolA thread pool to run the task in

Definition at line 252 of file context.cpp.

252  {
253  return impl->submitPersistentTask(pool, task);
254 }
Job submitPersistentTask(const PoolIndex pool, AbstractTask &task)
Definition: context.cpp:160

◆ waitForJob()

void Context::waitForJob ( Job  job,
const PoolIndex  pool = DEFAULT_POOL 
)

Waits until a given job finishes.

Parameters
jobThe job
poolPool index

Definition at line 256 of file context.cpp.

256  {
257  impl->waitForJob(pool, job);
258 }
void waitForJob(const PoolIndex pool, Job job)
Definition: context.cpp:166
JNIEnv jlong jint jint job

◆ abortJob()

bool Context::abortJob ( Job  job,
const PoolIndex  pool = DEFAULT_POOL 
)

Aborts a given submitted job.

Parameters
jobThe job
poolPool index
Returns
true if the job was interrupted while running.

Definition at line 260 of file context.cpp.

260  {
261  return impl->abortJob(pool, job);
262 }
bool abortJob(const PoolIndex pool, Job job)
Definition: context.cpp:172

◆ wait()

void Context::wait ( const PoolIndex  pool = DEFAULT_POOL)

Blocks until all the submitted jobs are executed.

Parameters
poolPool index

Definition at line 264 of file context.cpp.

264  {
265  impl->wait(pool);
266 }
void wait(const PoolIndex pool)
Definition: context.cpp:178

◆ busy()

bool Context::busy ( const PoolIndex  pool = DEFAULT_POOL)

Queries whether a given thread pool is busy with a task.

Parameters
poolThe thread pool to query
Returns
true if the thread pool is running a task, false otherwise

Definition at line 268 of file context.cpp.

268  {
269  return impl->busy(pool);
270 }
bool busy(const PoolIndex pool)
Definition: context.cpp:184

◆ check()

void Context::check ( const PoolIndex  pool = DEFAULT_POOL)

Checks if a specific thread pool is doing great: rethrows exceptions occurred during tasks execution, if any.

Parameters
poolThe thread pool to query

Definition at line 272 of file context.cpp.

272  {
273  impl->check(pool);
274 }
void check(const PoolIndex pool)
Definition: context.cpp:190

◆ maxAllowedWorkerCount()

const ThreadIndex Context::maxAllowedWorkerCount ( const PoolIndex  pool = DEFAULT_POOL) const
Returns
maximum number of working threads per task in a given pool.

Definition at line 276 of file context.cpp.

276  {
277  return impl->maxAllowedWorkerCount(pool);
278 }
const ThreadIndex maxAllowedWorkerCount(const PoolIndex pool) const
Definition: context.cpp:195

◆ limitWorkerCount()

void Context::limitWorkerCount ( ThreadIndex  maxValue,
const PoolIndex  pool = DEFAULT_POOL 
)

Limits maximum number of threads (workers) when performing tasks in a given pool.

Parameters
maxValueNumber limiting the worker count
poolThe thread pool
Returns
new maximum limit.

Definition at line 280 of file context.cpp.

280  {
281  impl->limitWorkerCount(pool, maxValue);
282 }
void limitWorkerCount(const PoolIndex pool, ThreadIndex maxValue)
Definition: context.cpp:201

◆ setEventListener()

void Context::setEventListener ( EventListener eventListener)

Installs new event listener.

Definition at line 284 of file context.cpp.

284  {
285  impl->eventListener = eventListener;
286 }
Context::EventListener * eventListener
an event listener
Definition: context.cpp:116

◆ getEventListener()

Context::EventListener * Context::getEventListener ( ) const

Returns current event listener (or NULL)

Definition at line 288 of file context.cpp.

288  {
289  return impl->eventListener;
290 }

◆ isGpuQueried()

bool Context::isGpuQueried ( ) const
Returns
true if GPU was queried

Definition at line 292 of file context.cpp.

292  {
293  return impl->isGpuQueried();
294 }
bool isGpuQueried() const
Definition: context.cpp:207

◆ isGpuReady()

bool Context::isGpuReady ( ) const
Returns
true if GPU was queried and ready to use

Definition at line 296 of file context.cpp.

296  {
297  return impl->isGpuReady();
298 }
bool isGpuReady() const
Definition: context.cpp:212

◆ warmUpGpu()

void Context::warmUpGpu ( )

Initializes GPU within a given Context if not yet (takes no effect if it already is).

GPU initialization may take some time and is done when a first task using GPU is being run. Warming up the GPU is useful to avoid the app get stucked for some time when it launches its first task on GPU.

Definition at line 300 of file context.cpp.

300  {
301  if (!isGpuReady()) {
302  GpuTask task;
303  performTask(task);
304  }
305 }
bool isGpuReady() const
Definition: context.cpp:296
float performTask(AbstractTask &task, const PoolIndex pool=DEFAULT_POOL)
Performs a given task.
Definition: context.cpp:240
Template of a task using GPU.
Definition: gpu_task.h:27

◆ queryGpuInfo()

bool Context::queryGpuInfo ( std::string &  vendor,
std::string &  renderer 
)

Initializes the GPU if not yet and queries information about it.

Parameters
[out]vendorGPU vendor string.
[out]rendererrenderer string.
Returns
true if a GPU is found.

Definition at line 307 of file context.cpp.

307  {
308  class GpuQueryingTask : public AbstractTask {
309  public:
310  bool gpuFound;
311  std::string &vendor, &renderer;
312 
313  GpuQueryingTask(std::string& vendor, std::string& renderer) :
314  vendor(vendor), renderer(renderer) {}
315 
316  TaskDeviceRequirement getUsedDevices() const { return TaskDeviceRequirement::GPU_OR_CPU; }
317 
318  bool process(TaskThread& thread) {
319  gpuFound = false;
320  return true;
321  }
322 
323  bool processOnGPU(GraphicPipeline& gpu, TaskThread& thread) {
324  vendor = gpu.getGpuVendorString();
326  gpuFound = true;
327  return true;
328  }
329  } task(vendor, renderer);
330 
331  performTask(task);
332 
333  return task.gpuFound;
334 }
Task: an operation that can be executed by multiple threads in parallel.
Definition: parallelism.h:90
Internal low-level GPU control API.
Definition: pipeline.h:33
const char * getGpuRendererString() const
Definition: pipeline.cpp:951
const char * getGpuVendorString() const
Definition: pipeline.cpp:946
Thread executing tasks.
Definition: parallelism.h:154
Beatmup::SceneRenderer * renderer

◆ isManagingThread()

bool Context::isManagingThread ( ) const

Definition at line 336 of file context.cpp.

336  {
337  return impl->isManagingThread();
338 }
bool isManagingThread() const
Definition: context.cpp:217

◆ getGpuRecycleBin()

GL::RecycleBin * Context::getGpuRecycleBin ( ) const
Returns
GPU recycle bin to store GPU resources that can be freed only within a GPU-aware thread

Definition at line 340 of file context.cpp.

340  {
341  return recycleBin;
342 }

◆ operator==()

bool Beatmup::Context::operator== ( const Context context) const
inline

Context comparaison operator Two different instances of contexts are basically never identical; returning true only if the two point to the same instance.

Definition at line 244 of file context.h.

244  {
245  return this == &context;
246  }

Member Data Documentation

◆ impl

Impl* Beatmup::Context::impl
private

Definition at line 63 of file context.h.

◆ recycleBin

GL::RecycleBin* Beatmup::Context::recycleBin
private

stores GPU garbage: resources managed by GPU and might be freed in the managing thread only

Definition at line 64 of file context.h.

◆ DEFAULT_POOL

const PoolIndex Beatmup::Context::DEFAULT_POOL = 0
static

Definition at line 67 of file context.h.


The documentation for this class was generated from the following files: