FPC 3.0和InterlockedCompareExchange

时间:2016-04-05 05:14:45

标签: delphi lazarus freepascal fpc

我在Windows 10(64位)上安装了Lazarus 1.6 / FPC 3.0,64位,并移植了包含InterlockedCompareExchangePointer函数的Delphi代码。

FPC 3.0不包含InterlockedCompareExchangePointer声明;相反,它可以在InterlockedCompareExchange

中看到“重载”systemh.inc
function InterlockedCompareExchange(var Target: longint; NewValue: longint; Comperand: longint): longint; public name 'FPC_INTERLOCKEDCOMPAREEXCHANGE';
{$ifdef cpu64}
function InterlockedCompareExchange64(var Target: int64; NewValue: int64; Comperand: int64): int64; public name 'FPC_INTERLOCKEDCOMPAREEXCHANGE64';
function InterlockedCompareExchange(var Target: Pointer; NewValue: Pointer; Comperand: Pointer): Pointer; external name 'FPC_INTERLOCKEDCOMPAREEXCHANGE64';
{$else cpu64}
function InterlockedCompareExchange(var Target: Pointer; NewValue: Pointer; Comperand: Pointer): Pointer; external name 'FPC_INTERLOCKEDCOMPAREEXCHANGE';
{$endif cpu64}

现在我正在尝试使用InterlockedCompareExchange指针:

program Project1;

uses Windows;

function Test: Boolean;
var
  P1, P2: Pointer;

begin
  Result:= InterlockedCompareExchange(P1, P2, nil) <> nil
end;

begin
  Test;
end.

并且不会使用消息

进行编译
  

project1.lpr(10,50)错误:arg no的不兼容类型。 3:得到了   “指针”,预计“LongInt”

显然“过载”不起作用。如何解决?

我在默认(64位)目标上使用64位FPC 3.0。

2 个答案:

答案 0 :(得分:2)

以下解决方法

{$ifdef fpc}
function InterlockedCompareExchangePointer(var Target: Pointer; NewValue: Pointer; Comperand: Pointer): Pointer;
begin
{$ifdef cpu64}
  Result:= Pointer(InterlockedCompareExchange64(int64(Target), int64(NewValue), int64(Comperand)));
{$else cpu64}
  Result:= Pointer(InterlockedCompareExchange(LongInt(Target), LongInt(NewValue), LongInt(Comperand)));
{$endif cpu64}
end;
{$endif fpc}

编译并确保Delphi / FPC兼容性;看起来缺少InterlockedCompareExchangePointer声明是FPC错误,应该修复。

答案 1 :(得分:0)

在FPC 3.1.1(r34599)中添加了InterlockedCompareExchangePointer:

http://bugs.freepascal.org/view.php?id=29965