The Pedigree Project  0.1
RoundRobin.cc
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #if defined(THREADS)
21 #include "pedigree/kernel/process/RoundRobin.h"
22 #include "pedigree/kernel/LockGuard.h"
23 #include "pedigree/kernel/process/Thread.h"
24 #include "pedigree/kernel/processor/types.h"
25 #include "pedigree/kernel/utilities/Iterator.h"
26 #include "pedigree/kernel/utilities/assert.h"
27 #include "pedigree/kernel/utilities/utility.h"
28 
29 RoundRobin::RoundRobin() : m_Lock(false)
30 {
31 }
32 
34 {
35 }
36 
38 {
39 }
40 
42 {
43  LockGuard<Spinlock> guard(m_Lock);
44 
45  for (size_t i = 0; i < MAX_PRIORITIES; i++)
46  {
47  for (ThreadList::Iterator it = m_pReadyQueues[i].begin();
48  it != m_pReadyQueues[i].end(); it++)
49  {
50  if (*it == pThread)
51  {
52  m_pReadyQueues[i].erase(it);
53  return;
54  }
55  }
56  }
57 }
58 
60 {
61  LockGuard<Spinlock> guard(m_Lock);
62 
63  Thread *pThread = 0;
64  for (size_t i = 0; i < MAX_PRIORITIES; i++)
65  {
66  if (m_pReadyQueues[i].size())
67  {
68  pThread = m_pReadyQueues[i].popFront();
69  if (pThread == pCurrentThread)
70  continue;
71 
72  if (pThread)
73  {
74  return pThread;
75  }
76  }
77  }
78  return 0;
79 }
80 
82 {
83  if (RoundRobin::isReady(pThread))
84  {
85  assert(pThread->getPriority() < MAX_PRIORITIES);
86 
87  for (List<Thread *>::Iterator it =
88  m_pReadyQueues[pThread->getPriority()].begin();
89  it != m_pReadyQueues[pThread->getPriority()].end(); ++it)
90  {
91  if ((*it) == pThread)
92  {
93  // WARNING("RoundRobin: A thread was already in this priority
94  // queue");
95  return;
96  }
97  }
98 
99  m_pReadyQueues[pThread->getPriority()].pushBack(pThread);
100  }
101 }
102 
103 bool RoundRobin::isReady(Thread *pThread)
104 {
105  return pThread->getStatus() == Thread::Ready;
106 }
107 
108 #endif
virtual ~RoundRobin()
Definition: RoundRobin.cc:33
void pushBack(const T &value)
Definition: List.h:232
Iterator erase(Iterator &Iter)
Definition: List.h:343
virtual Thread * getNext(Thread *pCurrentThread)
Definition: RoundRobin.cc:59
T popFront()
Definition: List.h:319
Definition: List.h:64
::Iterator< Thread *, node_t > Iterator
Definition: List.h:71
#define assert(x)
Definition: assert.h:37
Iterator begin()
Definition: List.h:123
virtual void removeThread(Thread *pThread)
Definition: RoundRobin.cc:41
Status getStatus() const
Definition: Thread.h:192
Definition: Thread.h:54
virtual void addThread(Thread *pThread)
Definition: RoundRobin.cc:37
virtual void threadStatusChanged(Thread *pThread)
Definition: RoundRobin.cc:81
Iterator end()
Definition: List.h:135