-
Notifications
You must be signed in to change notification settings - Fork 14
/
bclasses.pas
76 lines (63 loc) · 2.18 KB
/
bclasses.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
{==============================================================================|
| Project : Bauglir Library |
|==============================================================================|
| Content: Generic objects |
|==============================================================================|
| Copyright (c)2011-2012, Bronislav Klucka |
| All rights reserved. |
| Source code is licenced under original 4-clause BSD licence: |
| http://licence.bauglir.com/bsd4.php |
| |
| |
|==============================================================================|
|==============================================================================}
unit BClasses;
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$H+}
interface
uses
{$IFDEF UNIX}
cthreads,
{$ELSE UNIX}
windows,
{$ENDIF}
Classes, SysUtils, SyncObjs;
Type
{:abstract(Basic library aware thread)
See @BauglirInDll variable
}
TBThread = class(TThread)
protected
fSyncLock: TCriticalSection;
procedure Synchronize(AMethod: TThreadMethod);
public
constructor Create(CreateSuspended: Boolean);
destructor Destroy; override;
end;
var
{:If @TRUE, than method passed TBThread.Synchronize will be executed directly,
without synchronization, useful for libraries and cosole projects.
}
BauglirSynchronizeThreads: boolean = false;
implementation
{ TBThread }
constructor TBThread.Create(CreateSuspended: Boolean);
begin
inherited Create(CreateSuspended);
fSyncLock := TCriticalSection.Create;
end;
destructor TBThread.Destroy;
begin
fSyncLock.Free;
inherited;
end;
procedure TBThread.Synchronize(AMethod: TThreadMethod);
begin
//fSyncLock.Enter;
if (BauglirSynchronizeThreads) or (GetCurrentThreadID = MainThreadID) then aMethod
else inherited Synchronize(aMethod);
//fSyncLock.Leave;
end;
end.