Leosac  0.8.0
Open Source Access Control
Scheduler.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #pragma once
21 
22 #include "LeosacFwd.hpp"
24 #include <map>
25 #include <mutex>
26 #include <queue>
27 #include <thread>
28 
29 namespace Leosac
30 {
31 
32 enum class TargetThread
33 {
34  MAIN,
35  POOL,
36 };
37 
47 class Scheduler
48 {
49  public:
56  Scheduler(Kernel *kptr);
57 
58  Scheduler(const Scheduler &) = delete;
59  Scheduler(Scheduler &&) = delete;
60  Scheduler &operator=(const Scheduler &) = delete;
61  Scheduler &operator=(Scheduler &&) = delete;
62 
63  template <typename Callable>
64  typename std::enable_if<
65  !std::is_convertible<Callable, std::shared_ptr<Tasks::Task>>::value,
66  void>::type
67  enqueue(const Callable &call, TargetThread policy)
68  {
69  enqueue(Tasks::GenericTask::build(call), policy);
70  }
71 
75  void enqueue(Tasks::TaskPtr t, TargetThread policy);
76 
84  void update(TargetThread me) noexcept;
85 
90 
95  Kernel &kernel();
96 
97  private:
98  using TaskQueue = std::queue<Tasks::TaskPtr>;
99  using TaskQueueMap = std::map<TargetThread, TaskQueue>;
100 
109 
111  mutable std::mutex mutex_;
112 };
113 }
Leosac::Scheduler::Scheduler
Scheduler(Kernel *kptr)
Construct a scheduler object (generally 1 per application).
Definition: Scheduler.cpp:67
Leosac::Scheduler::TaskQueueMap
std::map< TargetThread, TaskQueue > TaskQueueMap
Definition: Scheduler.hpp:99
LeosacFwd.hpp
Leosac::Scheduler::TaskQueue
std::queue< Tasks::TaskPtr > TaskQueue
Definition: Scheduler.hpp:98
Leosac::Scheduler::operator=
Scheduler & operator=(const Scheduler &)=delete
Leosac::Scheduler::update
void update(TargetThread me) noexcept
This will run queued tasks that are scheduled to run on thread me.
Definition: Scheduler.cpp:42
Leosac::Scheduler::queues_
TaskQueueMap queues_
The internal queues of tasks.
Definition: Scheduler.hpp:108
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
Leosac::Scheduler::register_thread
void register_thread(TargetThread me)
This is currently useless.
Definition: Scheduler.cpp:60
Leosac::Scheduler
This is a scheduler that is used internally to schedule asynchronous / long running tasks.
Definition: Scheduler.hpp:47
Leosac::TargetThread
TargetThread
Definition: Scheduler.hpp:32
GenericTask.hpp
Leosac::Tasks::GenericTask::build
static TaskPtr build(const Callable &callable)
Definition: GenericTask.hpp:41
Leosac::Kernel
Core of Leosac.
Definition: kernel.hpp:73
Leosac::Tasks::TaskPtr
std::shared_ptr< Task > TaskPtr
Definition: LeosacFwd.hpp:45
Leosac::TargetThread::MAIN
@ MAIN
Leosac::Scheduler::kernel
Kernel & kernel()
Retrieve the kernel reference associated with the scheduler.
Definition: Scheduler.cpp:72
Leosac::Scheduler::mutex_
std::mutex mutex_
Definition: Scheduler.hpp:111
Leosac::Scheduler::enqueue
std::enable_if< !std::is_convertible< Callable, std::shared_ptr< Tasks::Task > >::value, void >::type enqueue(const Callable &call, TargetThread policy)
Definition: Scheduler.hpp:67
Leosac::Scheduler::kptr_
Kernel * kptr_
Definition: Scheduler.hpp:110
Leosac::TargetThread::POOL
@ POOL