Class Scheduler

Inheritance Relationships

Derived Type

Class Documentation

class switchml::Scheduler

The scheduler class which is responsible for distributing jobs across worker threads.

The scheduler implementation can choose any algorithm, queuing design, data structure, or distribution mechanism to serve its purpose.

The scheduler should only be accessed through the context api. Any scheduler implementation must be thread safe in the sense that it locks the scheduler access lock before any function and releases it before exiting.

If more fine grained locking is needed then the implementation can create its own locks.

Subclassed by switchml::FifoScheduler

Public Functions

~Scheduler() = default
Scheduler(Scheduler const&) = delete
void operator=(Scheduler const&) = delete
Scheduler(Scheduler&&) = default
Scheduler &operator=(Scheduler&&) = default
virtual bool EnqueueJob(std::shared_ptr<Job> job) = 0

Add a job to the Scheduler’s queue.

This function is called by the context after a user submits a new communication job.

Parameters

job[in] a shared pointer for the job that we will enqueue

Returns

true if we could add the request successfully.

Returns

false otherwise.

virtual bool GetJobSlice(WorkerTid worker_thread_id, JobSlice &job_slice) = 0

Get a job request slice.

This is called through the context by worker threads to get a job slice. How the Job is sliced and distributed depends on the scheduler implementation. the function will block the calling thread on the job_submitted_event_ until a job slice is retrieved OR the Stop is called. This is why it is important to check for the return value to make sure that a job slice has been received.

Parameters
  • worker_thread_id[in] The id of the worker thread that wants a job slice.

  • job_slice[out] A reference to a job slice variable.

Returns

true if the scheduler returned a valid job slice.

Returns

false the caller was forced to wakeup and the scheduler did not return a valid job slice.

virtual bool NotifyJobSliceCompletion(WorkerTid worker_thread_id, const JobSlice &job_slice) = 0

Signal the scheduler that a job slice has been finished.

Since the scheduler is the one responsible for creating job slices out of jobs, it is the only entity that can know when a job is completed.

Parameters
  • worker_thread_id[in] The id of the worker thread that finished the job slice.

  • job_slice[in] The job slice that finished.

Returns

true If the job corresponding to this job slice has been fully completed.

Returns

false If there is still some job slices to be completed either by the calling worker thread or others.

virtual void Stop()

Set the stopped_ flag to true and notify threads waiting on the job submitted event.

Each implementation should work to wakeup any waiting threads whether waiting on jobs or the scheduler itself. It should also clear any dynamically allocated state.

Public Static Functions

static std::unique_ptr<Scheduler> CreateInstance(Config &config)

Creates a Scheduler object based on the scheduler name in the config.

Parameters

config – a reference to the context configuration.

Returns

std::unique_ptr<Scheduler> an exclusive pointer to the created scheduler object.

Protected Functions

Scheduler(Config &config)

Construct a new Scheduler object.

Parameters

config[in] A reference to the context configuration

Protected Attributes

bool stopped_

A flag that signifies that the scheduler has been stopped_

std::mutex access_mutex_

A mutex that is used to wrap all functions of the scheduler to make them thread safe.

std::condition_variable job_submitted_event_

A condition variable used by GetJobSlice to block until a job is available.

Config &config_

A reference to the context configuration