forked from remittor/delphi-iocp-server-sample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompletionPort.pas
442 lines (372 loc) · 12.3 KB
/
CompletionPort.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
unit CompletionPort;
{*******************************************************************************
TCompletionPort: Êëàññ - îáåðòêà äëÿ windows completion port
Created: ?
Last modification: 23.08.2008
To do: Not
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
TMultiReadSingleWrite - ïðèìèòèâ ñèíõðîíèçàöèè, îáåñïå÷èâàåò äîñòóï ìíîæåñòâà
ïîòîêîâ íà ÷òåíèå, íî òîëüêî îäíîãî - íà çàïèñü.
Created: 31.08.2008
Last modified: -
To do: No
********************************************************************************
Author: Sergey N. Naberegnyh ( Íàáåðåæíûõ Ñ.Í. )
*******************************************************************************}
{ ********************************************************************************
// Martin Harvey 27/5/2000
TSimpleSynchronizer - Multiple Read Exclisive Write Synchronizer
// Martin Harvey 5/6/2000
TEventSynchronizer - Multiple Read Exclisive Write Synchronizer
******************************************************************************** }
{$I CompVersionDef.inc}
interface
uses
Windows;
type
POverlappedEx = ^TOverlappedEx;
TOverlappedEx = record
Ovp: TOverlapped;
Obj: Pointer;
end;
TCompletionPort = class
private
FMaxActiveThreads: Cardinal;
FHandle: THandle;
class function GetProcessorsCount(): integer;
public
constructor Create(MaxActiveThreads: Cardinal = 0);
destructor Destroy;override;
function AddDevice(hDevice: THandle; CompletionKey: Cardinal): boolean;
function FreeDevice(hDevice: THandle): boolean;
function WaitCompletion(out NumBytes, CompKey: Cardinal; out pOvp: POverlapped; TimeOut: Cardinal = INFINITE): boolean;
function SetCompletion(NumBytes, CompKey: Cardinal; pOvp: POverlapped): boolean;
property ProcessorsCount: integer read GetProcessorsCount;
property Handle: THandle read FHandle;
property MaxActiveThreads: Cardinal read FMaxActiveThreads;
end;
TMultiReadSingleWrite = class(TObject)
private
FSecAttr: TSecurityAttributes;
FSecDesc: TSecurityDescriptor;
FSyncMutex: THandle;
FReadEvent: THandle;
FWriteEvent: THandle;
FReadLockCount: Integer;
FWaitReadCount: Integer;
FWriteLockCount: Integer;
FSpinCount: Integer;
procedure CaptureMutex;
procedure SetSpinCount(Value: Integer);
public
constructor Create(ASpinCount: Integer = 32);
destructor Destroy; override;
procedure ReadLock;
procedure ReadUnlock;
procedure WriteLock;
procedure WriteUnlock;
property SpinCount: Integer read FSpinCount write SetSpinCount;
end;
TSimpleSynchronizer = class(TObject)
private
FDataLock: TRTLCriticalSection;
FWriteLock: TRTLCriticalSection;
FActRead: Integer;
FReadRead: Integer;
FActWrite: Integer;
FWriteWrite: Integer;
FReaderSem: THandle;
FWriterSem: THandle;
public
constructor Create;
destructor Destroy; override;
procedure StartRead;
procedure StartWrite;
procedure EndRead;
procedure EndWrite;
end;
TEventSynchronizer = class(TObject)
private
FDataLock: TRTLCriticalSection;
FWriteLock: TRTLCriticalSection;
FReaders: Integer;
FWriters: Integer;
FNoReaders: THandle;
FNoWriters: THandle;
public
constructor Create;
destructor Destroy; override;
procedure StartRead;
procedure StartWrite;
procedure EndRead;
procedure EndWrite;
end;
implementation
uses
SysUtils;
var
CountOfProcessors: integer = 0;
function _ProcessorsCount: integer;
var
Info: TSystemInfo;
begin
GetSystemInfo(Info);
Result:= integer(Info.dwNumberOfProcessors);
end;
{ TCompletionPort }
constructor TCompletionPort.Create(MaxActiveThreads: Cardinal);
begin
FMaxActiveThreads := MaxActiveThreads;
FHandle := CreateIoCompletionPort(INVALID_HANDLE_VALUE, 0, 0, FMaxActiveThreads);
if FHandle = 0 then {$IFDEF VER140__}RaiseLastOsError;{$ELSE}RaiseLastWin32Error;{$ENDIF}
end;
function TCompletionPort.AddDevice(hDevice: THandle; CompletionKey: Cardinal): boolean;
begin
Result := CreateIoCompletionPort(hDevice, Handle, CompletionKey, 0) = FHandle;
end;
destructor TCompletionPort.Destroy;
begin
if FHandle <> 0 then CloseHandle(FHandle);
inherited;
end;
function TCompletionPort.FreeDevice(hDevice: THandle): boolean;
begin
Result := CloseHandle(hDevice);
end;
function TCompletionPort.SetCompletion(NumBytes, CompKey: Cardinal; pOvp: POverlapped): boolean;
begin
Result := PostQueuedCompletionStatus(Handle, NumBytes, CompKey, pOvp);
end;
function TCompletionPort.WaitCompletion(out NumBytes, CompKey: Cardinal; out pOvp: POverlapped; TimeOut: Cardinal = INFINITE): boolean;
begin
Result := GetQueuedCompletionStatus(Handle, NumBytes, CompKey, pOvp, TimeOut);
end;
class function TCompletionPort.GetProcessorsCount: integer;
begin
if CountOfProcessors = 0 then CountOfProcessors := _ProcessorsCount;
Result := CountOfProcessors;
end;
{ TMultiReadSingleWrite }
procedure TMultiReadSingleWrite.CaptureMutex;
var
n, SC: integer;
begin
SC := FSpinCount;
if (CountOfProcessors > 1) and (SC > 0) then begin
for n:=0 to Pred(SC) do begin
if WaitForSingleObject(FSyncMutex, 0) = WAIT_OBJECT_0 then Exit;
end;
end;
WaitForSingleObject(FSyncMutex, INFINITE);
end;
constructor TMultiReadSingleWrite.Create(ASpinCount: Integer = 32);
begin
if ASpinCount > 0 then FSpinCount := ASpinCount;
if CountOfProcessors = 0 then CountOfProcessors := _ProcessorsCount;
if not InitializeSecurityDescriptor(@FSecDesc, SECURITY_DESCRIPTOR_REVISION) then
{$IFDEF VER140__}RaiseLastOsError;{$ELSE}RaiseLastWin32Error;{$ENDIF}
if not SetSecurityDescriptorDacl(@FSecDesc, True, nil, False) then
{$IFDEF VER140__}RaiseLastOsError;{$ELSE}RaiseLastWin32Error;{$ENDIF}
FSecAttr.nLength := SizeOf(FSecAttr);
FSecAttr.lpSecurityDescriptor := @FSecDesc;
FSecAttr.bInheritHandle := False;
FSyncMutex := CreateMutex(@FSecAttr, False, nil);
if FSyncMutex = 0 then
{$IFDEF VER140__}RaiseLastOsError;{$ELSE}RaiseLastWin32Error;{$ENDIF}
FReadEvent := CreateEvent(@FSecAttr, True, False, nil);
if FReadEvent = 0 then
{$IFDEF VER140__}RaiseLastOsError;{$ELSE}RaiseLastWin32Error;{$ENDIF}
FWriteEvent := CreateEvent(@FSecAttr, False, False, nil);
if FWriteEvent = 0 then
{$IFDEF VER140__}RaiseLastOsError;{$ELSE}RaiseLastWin32Error;{$ENDIF}
end;
destructor TMultiReadSingleWrite.Destroy;
begin
if FWriteEvent <> 0 then CloseHandle(FWriteEvent);
if FReadEvent <> 0 then CloseHandle(FReadEvent);
if FSyncMutex <> 0 then CloseHandle(FSyncMutex);
inherited;
end;
procedure TMultiReadSingleWrite.ReadLock;
begin
CaptureMutex;
if FWriteLockCount = 0 then begin
Inc(FReadLockCount);
ReleaseMutex(FSyncMutex);
end else begin
Inc(FWaitReadCount);
SignalObjectAndWait(FSyncMutex, FReadEvent, INFINITE, false);
end;
end;
procedure TMultiReadSingleWrite.ReadUnlock;
begin
CaptureMutex;
Dec(FReadLockCount);
if (FReadLockCount = 0) and (FWriteLockCount > 0) then SetEvent(FWriteEvent);
ReleaseMutex(FSyncMutex);
end;
procedure TMultiReadSingleWrite.SetSpinCount(Value: integer);
begin
if Value < 0 then Value := 0;
InterlockedExchange(FSpinCount, Value); // FSpinCount := Value
end;
procedure TMultiReadSingleWrite.WriteLock;
begin
CaptureMutex;
Inc(FWriteLockCount);
ResetEvent(FReadEvent);
if (FReadLockCount = 0) and (FWriteLockCount = 1) then begin
ReleaseMutex(FSyncMutex);
end else begin
SignalObjectAndWait(FSyncMutex, FWriteEvent, INFINITE, False);
end;
end;
procedure TMultiReadSingleWrite.WriteUnlock;
begin
CaptureMutex;
Dec(FWriteLockCount);
if FWriteLockCount = 0 then begin
Inc(FReadLockCount, FWaitReadCount);
FWaitReadCount := 0;
SetEvent(FReadEvent);
end else begin
SetEvent(FWriteEvent);
end;
ReleaseMutex(FSyncMutex);
end;
{ TSimpleSynchronizer }
constructor TSimpleSynchronizer.Create;
begin
inherited Create;
InitializeCriticalSection(FDataLock);
InitializeCriticalSection(FWriteLock);
FReaderSem := CreateSemaphore(nil, 0, High(Integer), nil);
FWriterSem := CreateSemaphore(nil, 0, High(Integer), nil);
{ Initial values of 0 OK for all counts }
end;
destructor TSimpleSynchronizer.Destroy;
begin
DeleteCriticalSection(FDataLock);
DeleteCriticalSection(FWriteLock);
CloseHandle(FReaderSem);
CloseHandle(FWriterSem);
inherited Destroy;
end;
procedure TSimpleSynchronizer.StartRead;
begin
EnterCriticalSection(FDataLock);
Inc(FActRead);
if FActWrite = 0 then begin
Inc(FReadRead);
ReleaseSemaphore(FReaderSem, 1, nil);
end;
LeaveCriticalSection(FDataLock);
WaitForSingleObject(FReaderSem, INFINITE);
end;
procedure TSimpleSynchronizer.StartWrite;
begin
EnterCriticalSection(FDataLock);
Inc(FActWrite);
if FReadRead = 0 then begin
Inc(FWriteWrite);
ReleaseSemaphore(FWriterSem, 1, nil);
end;
LeaveCriticalSection(FDataLock);
WaitForSingleObject(FWriterSem, INFINITE);
EnterCriticalSection(FWriteLock);
end;
procedure TSimpleSynchronizer.EndRead;
begin
EnterCriticalSection(FDataLock);
Dec(FReadRead);
Dec(FActRead);
if FReadRead = 0 then begin
while FWriteWrite < FActWrite do begin
Inc(FWriteWrite);
ReleaseSemaphore(FWriterSem, 1, nil);
end;
end;
LeaveCriticalSection(FDataLock);
end;
procedure TSimpleSynchronizer.EndWrite;
begin
LeaveCriticalSection(FWriteLock);
EnterCriticalSection(FDataLock);
Dec(FWriteWrite);
Dec(FActWrite);
if FActWrite = 0 then begin
while FReadRead < FActRead do begin
Inc(FReadRead);
ReleaseSemaphore(FReaderSem, 1, nil);
end;
end;
LeaveCriticalSection(FDataLock);
end;
{ TEventSynchronizer }
constructor TEventSynchronizer.Create;
begin
inherited Create;
InitializeCriticalSection(FDataLock);
InitializeCriticalSection(FWriteLock);
FNoReaders := CreateEvent(nil, true, true, nil);
FNoWriters := CreateEvent(nil, true, true, nil);
end;
destructor TEventSynchronizer.Destroy;
begin
DeleteCriticalSection(FDataLock);
DeleteCriticalSection(FWriteLock);
CloseHandle(FNoReaders);
CloseHandle(FNoWriters);
inherited Destroy;
end;
procedure TEventSynchronizer.StartRead;
var
Block: boolean;
begin
EnterCriticalSection(FDatalock);
if FReaders = 0 then ResetEvent(FNoReaders);
Inc(FReaders);
Block := FWriters > 0;
LeaveCriticalSection(FDataLock);
if Block then WaitForSingleObject(FNoWriters, INFINITE);
end;
procedure TEventSynchronizer.StartWrite;
var
Block: boolean;
begin
EnterCriticalSection(FDataLock);
if FWriters = 0 then ResetEvent(FNoWriters);
Inc(FWriters);
Block := FReaders > 0;
LeaveCriticalSection(FDataLock);
if Block then WaitForSingleObject(FNoReaders, INFINITE);
EnterCriticalSection(FWriteLock);
end;
procedure TEventSynchronizer.EndRead;
begin
EnterCriticalSection(FDataLock);
Dec(FReaders);
if FReaders = 0 then SetEvent(FNoReaders);
LeaveCriticalSection(FDataLock);
end;
procedure TEventSynchronizer.EndWrite;
begin
LeaveCriticalSection(FWriteLock);
EnterCriticalSection(FDataLock);
Dec(FWriters);
if FWriters = 0 then SetEvent(FNoWriters);
LeaveCriticalSection(FDataLock);
end;
end.
// èùó ïðèìèòèâ ñèíõðîíèçàöèè, îáåñïå÷èâàþùèé äîñòóï ìíîæåñòâà ïîòîêîâ íà ÷òåíèå, íî òîëüêî îäíîãî - íà çàïèñü
// Multi Read Single Write
Îáäóìûâàþ ïåðåäåëêó ñâîåãî TCP ñåðâåðà. Áóäó äåëàòü àñèíõðîííóþ îáðàáîòàêó çàïðîñîâ (IOCP).
Èìååòñÿ íåñêîëüêî âñïîì. ïîòîêîâ (2..4), êîòîðûå è îáñëóæèâàþò êëèåíòîâ (ïóë ïîòîêîâ) . Â êàæäîì èç íèõ âûäåëÿåòñÿ ïàìÿòü ïîä ëîãè.
 îñíîâíîì ïîòîêå (èëè èíîì ïîòîêå) ïëàíèðóþ ñäåëàòü ïåðåîäè÷åñêóþ (èíòåðâàë 4 ñåê) âûãðóçêó ëîãîâ â ôàéë.
Êàê òóò ëó÷øå ñèíõðîíèçèðîâàòü?
ÇÛ. Ëîãè âåäóòñÿ ñïåöèàëüíî â êàæäîì èç ïîòîêîâ, ÷òî áû èçáàâèòüñÿ îò "ëèøíèõ" êðèò. ñåêöèé.
// ëîãèðîâàíèå http://www.viva64.com/ru/a/0018/
http://www.frolov-lib.ru/books/bsp/v26/ch5_4.htm
If your interested in setting the registry keys to enable output,
then check out the SetDbgPrintFiltering utility at http://www.osronline.com/downloads.
Ïîòîêè è èõ ñèíõðîíèçàöèÿ http://forum.vingrad.ru/forum/topic-60076.html