Games Task Scheduler (GTS)
A multi-processor scheduling framework for games engines
3_priority_init.h
1 /*******************************************************************************
2  * Copyright 2019 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files(the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions :
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  ******************************************************************************/
22 #include "gts/platform/Thread.h"
23 
24 #include "gts/micro_scheduler/WorkerPool.h"
25 #include "gts/micro_scheduler/MicroScheduler.h"
26 
27 using namespace gts;
28 
29 namespace gts_examples {
30 
31 void priorityInit()
32 {
33  printf ("================\n");
34  printf ("priorityInit\n");
35  printf ("================\n");
36 
37  // The GTS micro-schedule implements task priorities, yet it does not
38  // context switch when a higher priority task is spawned. To achieve
39  // context switching behavior, one needs to use the OS's preemptive scheduler.
40 
41  // Let's say we need two priorities hi and low.
42  constexpr uint32_t PRIORITY_COUNT = 2;
43  WorkerPool workerPool[PRIORITY_COUNT];
44  MicroScheduler microScheduler[PRIORITY_COUNT];
45 
46  // For each priority, build a pool with a different priority and attach
47  // a scheduler to it.
48  for (uint32_t iPriority = 0; iPriority < PRIORITY_COUNT; ++iPriority)
49  {
50  WorkerPoolDesc workerPoolDesc;
51 
52  for (uint32_t iWorker = 0; iWorker < Thread::getHardwareThreadCount(); iWorker++)
53  {
54  WorkerThreadDesc workerDesc;
55  workerDesc.priority = (Thread::Priority)((uint32_t)Thread::Priority::PRIORITY_NORMAL + iPriority);
56  workerPoolDesc.workerDescs.push_back(workerDesc);
57  }
58 
59  workerPool[iPriority].initialize(workerPoolDesc);
60  microScheduler[iPriority].initialize(&workerPool[iPriority]);
61  }
62 
63  // Task submitted from microScheduler[1] running on threads from
64  // workerPool[1] will preempt any running tasks from microScheduler[0].
65 }
66 
67 } // namespace gts_examples
A work-stealing task scheduler. The scheduler is executed by the WorkerPool it is initialized with.
Definition: MicroScheduler.h:81
bool initialize(WorkerPool *pWorkerPool)
Initializes the MicroScheduler and attaches it to pWorkPool, where each worker in pWorkPool will exec...
A collection of running Worker threads that a MicroScheduler can be run on.
Definition: WorkerPool.h:54
bool initialize(uint32_t threadCount=0)
static GTS_INLINE uint32_t getHardwareThreadCount()
Gets the number of logical processor on this system.
Definition: Thread.h:447
The description of a WorkerPool.
Definition: MicroSchedulerTypes.h:172
gts::Vector< WorkerThreadDesc > workerDescs
The description of each worker thread.
Definition: MicroSchedulerTypes.h:184
The description of a worker Thread.
Definition: MicroSchedulerTypes.h:87
Thread::Priority priority
The priority at with the thread is scheduled by the OS.
Definition: MicroSchedulerTypes.h:120