-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCancelToken.pas
More file actions
50 lines (41 loc) · 930 Bytes
/
Copy pathCancelToken.pas
File metadata and controls
50 lines (41 loc) · 930 Bytes
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
unit CancelToken;
interface
uses
{$IFDEF FPC}
Classes, SysUtils;
{$ELSE}
System.Classes, System.SyncObjs, System.SysUtils;
{$ENDIF}
Type
iCancelToken = Interface
procedure Cancel;
function GetCanceled: Boolean;
property Canceled: Boolean read GetCanceled;
end;
TCancelToken = class(TInterfacedObject, iCancelToken)
private
fCanceled: Integer;
function GetCanceled: Boolean;
public
procedure Cancel;
property Canceled: Boolean read GetCanceled;
end;
implementation
{ TCancelToken }
procedure TCancelToken.Cancel;
begin
{$IFDEF FPC}
System.InterlockedExchange(fCanceled, 1);
{$ELSE}
TInterlocked.Exchange(fCanceled, 1);
{$ENDIF}
end;
function TCancelToken.GetCanceled: Boolean;
begin
{$IFDEF FPC}
Result := System.InterlockedCompareExchange(fCanceled, 0, 0) <> 0;
{$ELSE}
Result := TInterlocked.CompareExchange(fCanceled, 0, 0) <> 0;
{$ENDIF}
end;
end.