forked from gabr42/OmniThreadLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OtlHooks.pas
492 lines (423 loc) · 16.3 KB
/
OtlHooks.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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
///<summary>OmniThreadLibrary hooks.</summary>
///<author>Primoz Gabrijelcic</author>
///<license>
///This software is distributed under the BSD license.
///
///Copyright (c) 2019, Primoz Gabrijelcic
///All rights reserved.
///
///Redistribution and use in source and binary forms, with or without modification,
///are permitted provided that the following conditions are met:
///- Redistributions of source code must retain the above copyright notice, this
/// list of conditions and the following disclaimer.
///- Redistributions in binary form must reproduce the above copyright notice,
/// this list of conditions and the following disclaimer in the documentation
/// and/or other materials provided with the distribution.
///- The name of the Primoz Gabrijelcic may not be used to endorse or promote
/// products derived from this software without specific prior written permission.
///
///THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
///ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
///WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
///DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
///ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
///(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
///LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
///ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
///(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
///SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
///</license>
///<remarks><para>
/// Home : http://www.omnithreadlibrary.com
/// Support : https://en.delphipraxis.net/forum/32-omnithreadlibrary/
/// Author : Primoz Gabrijelcic
/// E-Mail : [email protected]
/// Blog : http://thedelphigeek.com
/// Creation date : 2009-05-17
/// Last modification : 2016-08-31
/// Version : 1.04
///</para><para>
/// History:
/// 1.04: 2016-08-31
/// - Added thread pool lifecycle notifications.
/// 1.03: 2015-10-04
/// - Cleaned up 'uses' list.
/// 1.02: 2011-07-14
/// - Support for non-silent exceptions removed.
/// - Changed signature for exception filters.
/// 1.01: 2010-07-01
/// - Includes OTLOptions.inc.
///</para></remarks>
unit OtlHooks;
{$I OtlOptions.inc}
interface
uses
SysUtils,
Classes,
OtlSync,
OtlThreadPool;
type
TThreadNotificationType = (tntCreate, tntDestroy);
TThreadNotificationProc = procedure(notifyType: TThreadNotificationType;
const threadName: string);
TThreadNotificationMeth = procedure(notifyType: TThreadNotificationType;
const threadName: string) of object;
procedure RegisterThreadNotification(notifyProc: TThreadNotificationProc); overload;
procedure RegisterThreadNotification(notifyMethod: TThreadNotificationMeth); overload;
procedure UnregisterThreadNotification(notifyProc: TThreadNotificationProc); overload;
procedure UnregisterThreadNotification(notifyMethod: TThreadNotificationMeth); overload;
procedure SendThreadNotifications(notifyType: TThreadNotificationType;
const threadName: string);
type
TPoolNotificationType = (pntCreate, pntDestroy);
TPoolNotificationProc = procedure(notifyType: TPoolNotificationType;
const pool: IOmniThreadPool);
TPoolNotificationMeth = procedure(notifyType: TPoolNotificationType;
const pool: IOmniThreadPool) of object;
procedure RegisterPoolNotification(notifyProc: TPoolNotificationProc); overload;
procedure RegisterPoolNotification(notifyMethod: TPoolNotificationMeth); overload;
procedure UnregisterPoolNotification(notifyProc: TPoolNotificationProc); overload;
procedure UnregisterPoolNotification(notifyMethod: TPoolNotificationMeth); overload;
procedure SendPoolNotifications(notifyType: TPoolNotificationType;
const pool: IOmniThreadPool);
type
TExceptionFilterProc = procedure(var e: Exception; var continueProcessing: boolean);
TExceptionFilterMeth = procedure(var e: Exception; var continueProcessing: boolean) of object;
procedure RegisterExceptionFilter(filterProc: TExceptionFilterProc); overload;
procedure RegisterExceptionFilter(filterMethod: TExceptionFilterMeth); overload;
procedure UnregisterExceptionFilter(filterProc: TExceptionFilterProc); overload;
procedure UnregisterExceptionFilter(filterMethod: TExceptionFilterMeth); overload;
procedure FilterException(var e: Exception);
implementation
type
TProcMethodList = class(TList)
strict private
pmlLock: TOmniMREW;
strict protected
procedure Add(data, code: pointer); reintroduce; overload;
function Find(data, code: pointer): integer;
procedure Remove(data, code: pointer); overload;
public
procedure Add(method: TMethod); reintroduce; overload;
procedure Add(proc: pointer); reintroduce; overload;
procedure EnterReadLock; inline;
procedure ExitReadLock; inline;
procedure Remove(method: TMethod); overload;
procedure Remove(proc: pointer); overload;
end; { TProcMethodList }
TThreadNotifications = class
strict private
tnList: TProcMethodList;
public
constructor Create;
destructor Destroy; override;
procedure Notify(notifyType: TThreadNotificationType; const threadName: string);
procedure Register(notifyProc: TThreadNotificationProc); overload;
procedure Register(notifyMethod: TThreadNotificationMeth); overload;
procedure Unregister(notifyProc: TThreadNotificationProc); overload;
procedure Unregister(notifyMethod: TThreadNotificationMeth); overload;
end; { TThreadNotifications }
TPoolNotifications = class
strict private
pnList: TProcMethodList;
public
constructor Create;
destructor Destroy; override;
procedure Notify(notifyType: TPoolNotificationType; const pool: IOmniThreadPool);
procedure Register(notifyProc: TPoolNotificationProc); overload;
procedure Register(notifyMethod: TPoolNotificationMeth); overload;
procedure Unregister(notifyProc: TPoolNotificationProc); overload;
procedure Unregister(notifyMethod: TPoolNotificationMeth); overload;
end; { TPoolNotifications }
TExceptionFilters = class
strict private
efList: TProcMethodList;
public
constructor Create;
destructor Destroy; override;
procedure Register(filterProc: TExceptionFilterProc); overload;
procedure Register(filterMethod: TExceptionFilterMeth); overload;
procedure Unregister(filterProc: TExceptionFilterProc); overload;
procedure Unregister(filterMethod: TExceptionFilterMeth); overload;
procedure Filter(var e: Exception);
end; { TExceptionFilters }
var
GExceptionFilters : TExceptionFilters;
GThreadNotifications: TThreadNotifications;
GPoolNotifications : TPoolNotifications;
procedure RegisterThreadNotification(notifyProc: TThreadNotificationProc); overload;
begin
GThreadNotifications.Register(notifyProc);
end; { RegisterThreadNotification }
procedure RegisterThreadNotification(notifyMethod: TThreadNotificationMeth); overload;
begin
GThreadNotifications.Register(notifyMethod);
end; { RegisterThreadNotification }
procedure UnregisterThreadNotification(notifyProc: TThreadNotificationProc); overload;
begin
GThreadNotifications.Unregister(notifyProc);
end; { UnregisterThreadNotification }
procedure UnregisterThreadNotification(notifyMethod: TThreadNotificationMeth); overload;
begin
GThreadNotifications.Unregister(notifyMethod);
end; { UnregisterThreadNotification }
procedure SendThreadNotifications(notifyType: TThreadNotificationType;
const threadName: string);
begin
GThreadNotifications.Notify(notifyType, threadName);
end; { SendThreadNotifications }
procedure RegisterPoolNotification(notifyProc: TPoolNotificationProc);
begin
GPoolNotifications.Register(notifyProc);
end; { RegisterPoolNotification }
procedure RegisterPoolNotification(notifyMethod: TPoolNotificationMeth);
begin
GPoolNotifications.Register(notifyMethod);
end; { RegisterPoolNotification }
procedure UnregisterPoolNotification(notifyProc: TPoolNotificationProc);
begin
GPoolNotifications.Unregister(notifyProc);
end; { UnregisterPoolNotification }
procedure UnregisterPoolNotification(notifyMethod: TPoolNotificationMeth);
begin
GPoolNotifications.Unregister(notifyMethod);
end; { UnregisterPoolNotification }
procedure SendPoolNotifications(notifyType: TPoolNotificationType;
const pool: IOmniThreadPool);
begin
GPoolNotifications.Notify(notifyType, pool);
end; { SendPoolNotifications }
procedure RegisterExceptionFilter(filterProc: TExceptionFilterProc); overload;
begin
GExceptionFilters.Register(filterProc);
end; { RegisterExceptionFilter }
procedure RegisterExceptionFilter(filterMethod: TExceptionFilterMeth); overload;
begin
GExceptionFilters.Register(filterMethod);
end; { RegisterExceptionFilter }
procedure UnregisterExceptionFilter(filterProc: TExceptionFilterProc); overload;
begin
GExceptionFilters.Unregister(filterProc);
end; { UnregisterExceptionFilter }
procedure UnregisterExceptionFilter(filterMethod: TExceptionFilterMeth); overload;
begin
GExceptionFilters.Unregister(filterMethod);
end; { UnregisterExceptionFilter }
procedure FilterException(var e: Exception);
begin
GExceptionFilters.Filter(e);
end; { FilterException }
{ TProcMethodList }
procedure TProcMethodList.Add(data, code: pointer);
begin
pmlLock.EnterWriteLock;
try
inherited Add(data);
inherited Add(code);
finally pmlLock.ExitWriteLock; end;
end; { TProcMethodList.Add }
procedure TProcMethodList.Add(method: TMethod);
begin
Add(TMethod(method).Data, TMethod(method).Code);
end; { TProcMethodList.Add }
procedure TProcMethodList.Add(proc: pointer);
begin
Add(nil, proc);
end; { TProcMethodList.Add }
procedure TProcMethodList.EnterReadLock;
begin
pmlLock.EnterReadLock;
end; { TProcMethodList.EnterReadLock }
procedure TProcMethodList.ExitReadLock;
begin
pmlLock.ExitReadLock;
end; { TProcMethodList.ExitReadLock }
function TProcMethodList.Find(data, code: pointer): integer;
begin
Result := 0;
while Result < Count do begin
if (Items[Result] = data) and (Items[Result+1] = code) then
Exit;
Inc(Result, 2);
end;
Result := -1;
end; { TProcMethodList.Find }
procedure TProcMethodList.Remove(method: TMethod);
begin
Remove(TMethod(method).Data, TMethod(method).Code);
end; { TProcMethodList.Remove }
procedure TProcMethodList.Remove(proc: pointer);
begin
Remove(nil, proc);
end; { TProcMethodList.Remove }
procedure TProcMethodList.Remove(data, code: pointer);
var
idxMeth: integer;
begin
pmlLock.EnterWriteLock;
try
idxMeth := Find(data, code);
if idxMeth >= 0 then begin
Delete(idxMeth);
Delete(idxMeth);
end;
finally pmlLock.ExitWriteLock; end;
end; { TProcMethodList.Remove }
{ TThreadNotifications }
constructor TThreadNotifications.Create;
begin
inherited Create;
tnList := TProcMethodList.Create;
end; { TThreadNotifications.Create }
destructor TThreadNotifications.Destroy;
begin
FreeAndNil(tnList);
inherited Destroy;
end; { TThreadNotifications.Destroy }
procedure TThreadNotifications.Notify(notifyType: TThreadNotificationType;
const threadName: string);
var
iObserver: integer;
meth : TMethod;
begin
if not assigned(Self) then
Exit; //finalization
tnList.EnterReadLock;
try
iObserver := 0;
while iObserver < tnList.Count do begin
if tnList[iObserver] = nil then
TThreadNotificationProc(tnList[iObserver+1])(notifyType, threadName)
else begin
meth.Data := tnList[iObserver];
meth.Code := tnList[iObserver+1];
TThreadNotificationMeth(meth)(notifyType, threadName);
end;
Inc(iObserver, 2);
end;
finally tnList.ExitReadLock; end;
end; { TThreadNotifications.Notify }
procedure TThreadNotifications.Register(notifyProc: TThreadNotificationProc);
begin
tnList.Add(pointer(@notifyProc));
end; { TThreadNotifications.Register }
procedure TThreadNotifications.Register(notifyMethod: TThreadNotificationMeth);
begin
tnList.Add(TMethod(notifyMethod));
end; { TThreadNotifications.Register }
procedure TThreadNotifications.Unregister(notifyProc: TThreadNotificationProc);
begin
tnList.Remove(pointer(@notifyProc));
end; { TThreadNotifications.Unregister }
procedure TThreadNotifications.Unregister(notifyMethod: TThreadNotificationMeth);
begin
tnList.Remove(TMethod(notifyMethod));
end; { TThreadNotifications.Unregister }
{ TPoolNotifications }
constructor TPoolNotifications.Create;
begin
inherited Create;
pnList := TProcMethodList.Create;
end; { TPoolNotifications.Create }
destructor TPoolNotifications.Destroy;
begin
FreeAndNil(pnList);
inherited Destroy;
end; { TPoolNotifications.Destroy }
procedure TPoolNotifications.Notify(notifyType: TPoolNotificationType;
const pool: IOmniThreadPool);
var
iObserver: integer;
meth : TMethod;
begin
if not assigned(Self) then
Exit; //finalization
pnList.EnterReadLock;
try
iObserver := 0;
while iObserver < pnList.Count do begin
if pnList[iObserver] = nil then
TPoolNotificationProc(pnList[iObserver+1])(notifyType, pool)
else begin
meth.Data := pnList[iObserver];
meth.Code := pnList[iObserver+1];
TPoolNotificationMeth(meth)(notifyType, pool);
end;
Inc(iObserver, 2);
end;
finally pnList.ExitReadLock; end;
end; { TPoolNotifications.Notify }
procedure TPoolNotifications.Register(notifyProc: TPoolNotificationProc);
begin
pnList.Add(pointer(@notifyProc));
end; { TPoolNotifications.Register }
procedure TPoolNotifications.Register(notifyMethod: TPoolNotificationMeth);
begin
pnList.Add(TMethod(notifyMethod));
end; { TPoolNotifications.Register }
procedure TPoolNotifications.Unregister(notifyProc: TPoolNotificationProc);
begin
pnList.Remove(pointer(@notifyProc));
end; { TPoolNotifications.Unregister }
procedure TPoolNotifications.Unregister(notifyMethod: TPoolNotificationMeth);
begin
pnList.Remove(TMethod(notifyMethod));
end; { TPoolNotifications.Unregister }
{ TExceptionFilters }
constructor TExceptionFilters.Create;
begin
inherited Create;
efList := TProcMethodList.Create;
end; { TExceptionFilters.Create }
destructor TExceptionFilters.Destroy;
begin
FreeAndNil(efList);
inherited;
end; { TExceptionFilters.Destroy }
procedure TExceptionFilters.Filter(var e: Exception);
var
continueProcessing: boolean;
iObserver : integer;
meth : TMethod;
begin
efList.EnterReadLock;
try
iObserver := 0;
continueProcessing := true;
while continueProcessing and (iObserver < efList.Count) do begin
if efList[iObserver] = nil then
TExceptionFilterProc(efList[iObserver+1])(e, continueProcessing)
else begin
meth.Data := efList[iObserver];
meth.Code := efList[iObserver+1];
TExceptionFilterMeth(meth)(e, continueProcessing);
end;
Inc(iObserver, 2);
end;
finally efList.ExitReadLock; end;
end; { TExceptionFilters.Filter }
procedure TExceptionFilters.Register(filterMethod: TExceptionFilterMeth);
begin
efList.Add(TMethod(filterMethod));
end; { TExceptionFilters.Register }
procedure TExceptionFilters.Register(filterProc: TExceptionFilterProc);
begin
efList.Add(pointer(@filterProc));
end; { TExceptionFilters.Register }
procedure TExceptionFilters.Unregister(filterMethod: TExceptionFilterMeth);
begin
efList.Remove(TMethod(filterMethod));
end; { TExceptionFilters.Unregister }
procedure TExceptionFilters.Unregister(filterProc: TExceptionFilterProc);
begin
efList.Remove(pointer(@filterProc));
end; { TExceptionFilters.Unregister }
initialization
GExceptionFilters := TExceptionFilters.Create;
GThreadNotifications := TThreadNotifications.Create;
GPoolNotifications := TPoolNotifications.Create;
finalization
FreeAndNil(GExceptionFilters);
FreeAndNil(GThreadNotifications);
FreeAndNil(GPoolNotifications);
end.