-
-
Notifications
You must be signed in to change notification settings - Fork 140
/
WaitForNewPinnedTasks.cpp
150 lines (125 loc) · 5.48 KB
/
WaitForNewPinnedTasks.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright (c) 2021 Doug Binks
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgement in the product documentation would be
// appreciated but is not required.
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
// 3. This notice may not be removed or altered from any source distribution.
#include "TaskScheduler.h"
#include <stdio.h>
using namespace enki;
TaskScheduler g_TS;
static std::atomic<int32_t> g_Run;
enum class IOThreadId
{
FILE_IO, // more than one file io thread may be useful if handling lots of small files
NETWORK_IO_0,
NETWORK_IO_1,
NETWORK_IO_2,
NUM,
};
struct ParallelTaskSet : ITaskSet
{
ParallelTaskSet() { m_SetSize = 100; }
void ExecuteRange( TaskSetPartition range_, uint32_t threadnum_ ) override
{
bool bIsIOThread = threadnum_ >= g_TS.GetNumTaskThreads() - (uint32_t)IOThreadId::NUM;
if( bIsIOThread )
{
assert( false ); //for this example this is an error - but external threads can run tasksets in general
printf(" Run %d: ParallelTaskSet on thread %d which is an IO thread\n", g_Run.load(), threadnum_);
}
else
{
printf(" Run %d: ParallelTaskSet on thread %d which is not an IO thread\n", g_Run.load(), threadnum_);
}
}
};
struct RunPinnedTaskLoopTask : IPinnedTask
{
void Execute() override
{
while( !g_TS.GetIsShutdownRequested() )
{
g_TS.WaitForNewPinnedTasks(); // this thread will 'sleep' until there are new pinned tasks
g_TS.RunPinnedTasks();
}
}
};
struct PretendDoFileIO : IPinnedTask
{
void Execute() override
{
printf(" Run %d: PretendDoFileIO on thread %d\n", g_Run.load(), threadNum );
// sleep used as a 'pretend' blocking workload
std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) );
}
};
struct PretendDoNetworkIO : IPinnedTask
{
void Execute() override
{
printf(" Run %d: PretendDoNetworkIO on thread %d\n", g_Run.load(), threadNum );
// sleep used as a 'pretend' blocking workload
std::this_thread::sleep_for( std::chrono::milliseconds( 5 ) );
}
};
static const int REPEATS = 5;
int main(int argc, const char * argv[])
{
enki::TaskSchedulerConfig config;
// In this example we create more threads than the hardware can run,
// because the IO threads will spend most of their time idle or blocked
// and therefore not scheduled for CPU time by the OS
config.numTaskThreadsToCreate += (uint32_t)IOThreadId::NUM;
g_TS.Initialize( config );
// in this example we place our IO threads at the end
uint32_t theadNumIOStart = g_TS.GetNumTaskThreads() - (uint32_t)IOThreadId::NUM;
RunPinnedTaskLoopTask runPinnedTaskLoopTasks[ (uint32_t)IOThreadId::NUM ];
for( uint32_t ioThreadID = 0; ioThreadID < (uint32_t)IOThreadId::NUM; ++ioThreadID )
{
runPinnedTaskLoopTasks[ioThreadID].threadNum = ioThreadID + theadNumIOStart;
g_TS.AddPinnedTask( &runPinnedTaskLoopTasks[ioThreadID] );
}
for( g_Run = 0; g_Run< REPEATS; ++g_Run )
{
printf("Run %d\n", g_Run.load() );
// set of a ParallelTaskSet
// to demonstrate can perform work on enkiTS worker threads but WaitForNewPinnedTasks will
// not perform work. This can be used to fully subscribe machine with enkiTS worker threads but
// have extra IO bound threads to handle blocking tasks
ParallelTaskSet parallelTaskSet;
g_TS.AddTaskSetToPipe( ¶llelTaskSet );
// Send pretend file IO task to external thread FILE_IO
PretendDoFileIO pretendDoFileIO;
pretendDoFileIO.threadNum = (uint32_t)IOThreadId::FILE_IO + theadNumIOStart;
g_TS.AddPinnedTask( &pretendDoFileIO );
// Send pretend network IO tasks to external thread NETWORK_IO_0 ... NUMEXTERNALTHREADS
PretendDoNetworkIO pretendDoNetworkIO[ (uint32_t)IOThreadId::NUM - (uint32_t)IOThreadId::NETWORK_IO_0 ];
for( uint32_t ioThreadID = (uint32_t)IOThreadId::NETWORK_IO_0; ioThreadID < (uint32_t)IOThreadId::NUM; ++ioThreadID )
{
pretendDoNetworkIO[ioThreadID-(uint32_t)IOThreadId::NETWORK_IO_0].threadNum = ioThreadID + theadNumIOStart;
g_TS.AddPinnedTask( &pretendDoNetworkIO[ ioThreadID - (uint32_t)IOThreadId::NETWORK_IO_0 ] );
}
g_TS.WaitforTask( ¶llelTaskSet );
// in this example we need to wait for IO tasks to complete before running next loop
g_TS.WaitforTask( &pretendDoFileIO );
for( uint32_t ioThreadID = (uint32_t)IOThreadId::NETWORK_IO_0; ioThreadID < (uint32_t)IOThreadId::NUM; ++ioThreadID )
{
g_TS.WaitforTask( &pretendDoNetworkIO[ ioThreadID - (uint32_t)IOThreadId::NETWORK_IO_0 ] );
}
}
// ensure runPinnedTaskLoopTasks complete by explicitly calling WaitforAllAndShutdown
g_TS.WaitforAllAndShutdown();
return 0;
}