The Pedigree Project 0.1
RoundRobinCoreAllocator.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#include "pedigree/kernel/LockGuard.h"
21#include "pedigree/kernel/Log.h"
22#include "pedigree/kernel/process/PerProcessorScheduler.h"
23#include "pedigree/kernel/process/RoundRobinCoreAllocator.h"
24#include "pedigree/kernel/process/Thread.h"
25#include "pedigree/kernel/utilities/Iterator.h"
26#include "pedigree/kernel/utilities/utility.h"
27
29class Thread;
30
31RoundRobinCoreAllocator::RoundRobinCoreAllocator() : m_ProcMap(), m_pNext(0) {}
32
33RoundRobinCoreAllocator::~RoundRobinCoreAllocator() {}
34
35bool RoundRobinCoreAllocator::initialise(List<PerProcessorScheduler*>& procList) {
37 PerProcessorScheduler* pFirst = m_pNext = *it;
38 it++;
39
40 // 1 CPU?
41 if (it == procList.end()) {
42 NOTICE("RoundRobinCoreAllocator: quitting, only one CPU was present.");
43 m_ProcMap.insert(pFirst, pFirst);
44 return true;
45 }
46
47 for (; it != procList.end(); it++) {
48 m_ProcMap.insert(pFirst, *it);
49 pFirst = *it;
50 }
51
52 // Loop.
53 m_ProcMap.insert(pFirst, m_pNext);
54
55 return true;
56}
57
58PerProcessorScheduler* RoundRobinCoreAllocator::allocateThread(Thread* pThread) {
59 ThreadPlacement placement;
60 pThread->snapshotPlacementLocked(placement);
61 LockGuard<Spinlock> guard(m_Lock);
62 PerProcessorScheduler* first = m_pNext;
63 do {
64 m_pNext = m_ProcMap.lookup(m_pNext);
65 if (placement.allowed.empty() || placement.allowed.contains(m_pNext->logicalCpu()))
66 return m_pNext;
67 } while (m_pNext != first);
68 FATAL("Thread allocator found no allowed online processor.");
69}
Definition List.h:61
Iterator begin()
Definition List.h:122
::Iterator< T, node_t > Iterator
Definition List.h:67
Iterator end()
Definition List.h:132
void snapshotPlacementLocked(ThreadPlacement &placement) const
E lookup(const K &key) const
Definition Tree.h:193
void insert(const K &key, const E &value)
Definition Tree.h:149