Scheduler
eventList.cpp
Go to the documentation of this file.
1 
10 #include "eventList.h"
11 
12 #include <cassert>
13 #include <iostream>
14 
15 #include "event.h"
16 
17 using namespace Scheduler;
18 
19 EventList *EventList::instance = nullptr;
20 std::mutex EventList::mut;
21 
23 {
24  std::lock_guard<std::mutex> guard(mut);
25  if (instance == nullptr)
26  {
27  instance = new EventList();
28  }
29  return instance;
30 }
31 
32 
33 EventList::EventList()
34 {
35 }
36 
38 {
39  randomGenerator = gen;
40 }
41 
43 {
44  return randomGenerator;
45 }
46 
47 std::shared_ptr<Event> EventList::pop()
48 {
49  std::lock_guard<std::mutex> guard(mut);
50  std::shared_ptr<Event> e = list.front();
51  list.pop_front();
52  return e;
53 }
54 
55 
56 std::shared_ptr<Event> EventList::insert(std::shared_ptr<Event> e)
57 {
58  std::lock_guard<std::mutex> guard(mut);
59  if (e == nullptr)
60  return nullptr;
61  double time = e->getTime();
62 
63  if(time > endTime)
64  return nullptr;
65 
66 
67  auto p = list.end();
68  while(p != list.begin())
69  {
70  if ((*--p)->getTime() <= time)
71  {
72  p++;
73  break;
74  }
75  }
76  auto it = list.insert(p, e);
77  assert(e == (*it));
78  return e;
79 }
80 
81 
82 
83 void EventList::remove(std::shared_ptr<Event> e)
84 {
85  std::lock_guard<std::mutex> guard(mut);
86  for (auto it = list.begin(); it != list.end(); it++)
87  {
88  if (e.get() == (*it).get())
89  {
90  assert(e != nullptr);
91  list.erase(it);
92  return;
93  }
94  }
95 }
96 
97 
99 {
100  std::lock_guard<std::mutex> guard(mut);
101  return (emptied ? true : (list.begin() == list.end()));
102 }
103 
105 {
106  std::lock_guard<std::mutex> guard(mut);
107  emptied = true;
108 }
109 
110 
112 {
113  std::lock_guard<std::mutex> guard(mut);
114  std::cout << "Printing the event list:\n";
115  for (auto it = list.begin(); it != list.end(); it++)
116  {
117  assert((*it) != nullptr);
118  std::cout << " ";
119  (*it)->print();
120  }
121 }
122 
123 
124 std::shared_ptr<Event> EventList::getHead()
125 {
126  std::lock_guard<std::mutex> guard(mut);
127  return (*list.begin());
128 }
129 
130 
131 
132 
133 
134 
135 
136 
137 
138 
139 
140 
141 
142 
143 
144 
145 
146 
Utils::RandomGenerator * getRandomGenerator()
get the random generator
Definition: eventList.cpp:42
void remove(std::shared_ptr< Event > e)
remove an element from the list.
Definition: eventList.cpp:83
void setRandomGenerator(Utils::RandomGenerator *gen)
set a random generator TODO: shouldn&#39;t that be part of the constructor?
Definition: eventList.cpp:37
std::shared_ptr< Event > getHead()
get the element at the head of the list
Definition: eventList.cpp:124
std::shared_ptr< Event > pop()
Definition: eventList.cpp:47
list time
Definition: aging.py:11
std::shared_ptr< Event > insert(std::shared_ptr< Event > e)
Definition: eventList.cpp:56
static EventList * getInstance()
Singleton pattern.
Definition: eventList.cpp:22