Leosac  0.7.0
OpenSourceAccessControl
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 
25 using namespace Leosac;
26 using namespace Leosac::Tasks;
27 
29 {
30  if (policy == TargetThread::POOL)
31  {
32  std::thread(std::bind(&Task::run, t)).detach();
33  }
34  else
35  {
36  std::lock_guard<std::mutex> lg(mutex_);
37  queues_[policy].push(t);
38  }
39 }
40 
42 {
43  mutex_.lock();
44  auto &queue = queues_[me];
45  int run = queue.size();
46  mutex_.unlock();
47 
48  while (run)
49  {
50  mutex_.lock();
51  auto task = queue.front();
52  queue.pop();
53  mutex_.unlock();
54  task->run();
55  run--;
56  }
57 }
58 
60 {
61  std::lock_guard<std::mutex> lg(mutex_);
62  assert(queues_.count(me) == 0);
63  queues_[me];
64 }
65 
67  : kptr_(kptr)
68 {
69 }
70 
72 {
73  assert(kptr_);
74  return *kptr_;
75 }
This is the header file for a generated source file, GitSHA1.cpp.
std::shared_ptr< Task > TaskPtr
Definition: LeosacFwd.hpp:45
Scheduler(Kernel *kptr)
Construct a scheduler object (generally 1 per application).
Definition: Scheduler.cpp:66
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
TargetThread
Definition: Scheduler.hpp:32
void register_thread(TargetThread me)
This is currently useless.
Definition: Scheduler.cpp:59
Core of Leosac.
Definition: kernel.hpp:73
Kernel & kernel()
Retrieve the kernel reference associated with the scheduler.
Definition: Scheduler.cpp:71
void update(TargetThread me) noexcept
This will run queued tasks that are scheduled to run on thread me.
Definition: Scheduler.cpp:41