Leosac  0.8.0
Open Source Access Control
Scheduler.cpp
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 #include "Scheduler.hpp"
21 #include "core/tasks/Task.hpp"
22 #include <assert.h>
23 #include <future>
24 #include <functional>
25 
26 using namespace Leosac;
27 using namespace Leosac::Tasks;
28 
30 {
31  if (policy == TargetThread::POOL)
32  {
33  std::thread(std::bind(&Task::run, t)).detach();
34  }
35  else
36  {
37  std::lock_guard<std::mutex> lg(mutex_);
38  queues_[policy].push(t);
39  }
40 }
41 
43 {
44  mutex_.lock();
45  auto &queue = queues_[me];
46  int run = queue.size();
47  mutex_.unlock();
48 
49  while (run)
50  {
51  mutex_.lock();
52  auto task = queue.front();
53  queue.pop();
54  mutex_.unlock();
55  task->run();
56  run--;
57  }
58 }
59 
61 {
62  std::lock_guard<std::mutex> lg(mutex_);
63  assert(queues_.count(me) == 0);
64  queues_[me];
65 }
66 
68  : kptr_(kptr)
69 {
70 }
71 
73 {
74  assert(kptr_);
75  return *kptr_;
76 }
Leosac::Scheduler::Scheduler
Scheduler(Kernel *kptr)
Construct a scheduler object (generally 1 per application).
Definition: Scheduler.cpp:67
Leosac::Tasks::Task::run
void run()
Definition: Task.cpp:43
Scheduler.hpp
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
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::TargetThread
TargetThread
Definition: Scheduler.hpp:32
Leosac::Kernel
Core of Leosac.
Definition: kernel.hpp:73
Leosac::Tasks::TaskPtr
std::shared_ptr< Task > TaskPtr
Definition: LeosacFwd.hpp:45
Leosac::Scheduler::kernel
Kernel & kernel()
Retrieve the kernel reference associated with the scheduler.
Definition: Scheduler.cpp:72
Task.hpp
Leosac::Tasks
Definition: FetchRemoteConfig.hpp:28
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