-
Notifications
You must be signed in to change notification settings - Fork 35
/
NtUtils.Threads.Worker.pas
155 lines (128 loc) · 4.61 KB
/
NtUtils.Threads.Worker.pas
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
151
152
153
154
155
unit NtUtils.Threads.Worker;
{
The module provides support for using worker factories (the type of kernel
objects behind thread pools).
}
interface
uses
Ntapi.nttp, Ntapi.ntioapi, NtUtils, NtUtils.Synchronization;
// Create a worker factory object
function NtxCreateWorkerFactory(
out hxWorkerFactory: IHandle;
[Access(IO_COMPLETION_MODIFY_STATE)] const hxCompletionPort: IHandle;
[in] StartRoutine: TWorkerFactoryRoutine;
[in, opt] StartParameter: Pointer;
MaxThreadCount: Cardinal = 0;
[opt] const ObjectAttributes: IObjectAttributes = nil;
StackReserve: NativeUInt = 0;
StackCommit: NativeUInt = 0
): TNtxStatus;
// Query basic information about a worker factory
function NtxQueryWorkerFactory(
[Access(WORKER_FACTORY_QUERY_INFORMATION)] const hxWorkerFactory: IHandle;
out Info: TWorkerFactoryBasicInformation
): TNtxStatus;
type
NtxWorkerFactory = class abstract
// Set fixed-size information for a worker factory
class function &Set<T>(
[Access(WORKER_FACTORY_SET_INFORMATION)] const hxWorkerFactory: IHandle;
InfoClass: TWorkerFactoryInfoClass;
const Buffer: T
): TNtxStatus; static;
end;
// Shutdown a worker factory
function NtxShutdownWorkerFactory(
[Access(WORKER_FACTORY_SHUTDOWN)] const hxWorkerFactory: IHandle;
out PendingWorkerCount: Cardinal
): TNtxStatus;
// Release a worker from a worker factory
function NtxReleaseWorkerFactoryWorker(
[Access(WORKER_FACTORY_RELEASE_WORKER)] const hxWorkerFactory: IHandle
): TNtxStatus;
// Mark a worker from a worker factory as being ready
function NtxWorkerFactoryWorkerReady(
[Access(WORKER_FACTORY_READY_WORKER)] const hxWorkerFactory: IHandle
): TNtxStatus;
// Wait for a queued task on the I/O completion port of the worker factory
function NtxWaitForWorkViaWorkerFactory(
[Access(WORKER_FACTORY_WAIT)] const hxWorkerFactory: IHandle;
out MiniPacket: TIoCompletionPacket
): TNtxStatus;
implementation
uses
Ntapi.ntdef, Ntapi.ntpsapi, NtUtils.Objects;
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
function NtxCreateWorkerFactory;
var
ObjAttr: PObjectAttributes;
hWorkerFactory: THandle;
begin
Result := AttributesRefOrNil(ObjAttr, ObjectAttributes);
if not Result.IsSuccess then
Exit;
Result.Location := 'NtCreateWorkerFactory';
Result.LastCall.Expects<TIoCompletionAccessMask>(IO_COMPLETION_MODIFY_STATE);
Result.Status := NtCreateWorkerFactory(
hWorkerFactory,
AccessMaskOverride(WORKER_FACTORY_ALL_ACCESS, ObjectAttributes),
ObjAttr,
HandleOrDefault(hxCompletionPort),
NtCurrentProcess,
StartRoutine,
StartParameter,
MaxThreadCount,
StackReserve,
StackCommit
);
if Result.IsSuccess then
hxWorkerFactory := Auto.CaptureHandle(hWorkerFactory);
end;
function NtxQueryWorkerFactory;
begin
Result.Location := 'NtQueryInformationWorkerFactory';
Result.LastCall.UsesInfoClass(WorkerFactoryBasicInformation, icQuery);
Result.LastCall.Expects<TWorkerFactoryAccessMask>(WORKER_FACTORY_QUERY_INFORMATION);
Result.Status := NtQueryInformationWorkerFactory(
HandleOrDefault(hxWorkerFactory),
WorkerFactoryBasicInformation,
@Info, SizeOf(Info), nil);
end;
class function NtxWorkerFactory.&Set<T>;
begin
Result.Location := 'NtSetInformationWorkerFactory';
Result.LastCall.UsesInfoClass(InfoClass, icSet);
Result.LastCall.Expects<TWorkerFactoryAccessMask>(WORKER_FACTORY_SET_INFORMATION);
Result.Status := NtSetInformationWorkerFactory(
HandleOrDefault(hxWorkerFactory), InfoClass, @Buffer, SizeOf(Buffer));
end;
function NtxShutdownWorkerFactory;
begin
PendingWorkerCount := 0;
Result.Location := 'NtShutdownWorkerFactory';
Result.LastCall.Expects<TWorkerFactoryAccessMask>(WORKER_FACTORY_SHUTDOWN);
Result.Status := NtShutdownWorkerFactory(HandleOrDefault(hxWorkerFactory),
Integer(PendingWorkerCount));
end;
function NtxReleaseWorkerFactoryWorker;
begin
Result.Location := 'NtReleaseWorkerFactoryWorker';
Result.LastCall.Expects<TWorkerFactoryAccessMask>(WORKER_FACTORY_RELEASE_WORKER);
Result.Status := NtReleaseWorkerFactoryWorker(HandleOrDefault(hxWorkerFactory));
end;
function NtxWorkerFactoryWorkerReady;
begin
Result.Location := 'NtWorkerFactoryWorkerReady';
Result.LastCall.Expects<TWorkerFactoryAccessMask>(WORKER_FACTORY_READY_WORKER);
Result.Status := NtWorkerFactoryWorkerReady(HandleOrDefault(hxWorkerFactory));
end;
function NtxWaitForWorkViaWorkerFactory;
begin
Result.Location := 'NtWaitForWorkViaWorkerFactory';
Result.LastCall.Expects<TWorkerFactoryAccessMask>(WORKER_FACTORY_WAIT);
Result.Status := NtWaitForWorkViaWorkerFactory(
HandleOrDefault(hxWorkerFactory), MiniPacket);
end;
end.