简体   繁体   English

FPC 3.0和InterlockedCompareExchange

[英]FPC 3.0 and InterlockedCompareExchange

I installed Lazarus 1.6/FPC 3.0, 64 bits on Windows 10 (64 bits) and porting Delphi code containing InterlockedCompareExchangePointer function. 我在Windows 10(64位)上安装了64位Lazarus 1.6 / FPC,并移植了包含InterlockedCompareExchangePointer函数的Delphi代码。

FPC 3.0 does not include InterlockedCompareExchangePointer declaration; FPC 3.0不包括InterlockedCompareExchangePointer声明; instead it "overloads" InterlockedCompareExchange as can be seen in systemh.inc : 相反,它“重载”了InterlockedCompareExchangesystemh.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}

Now I am trying to use InterlockedCompareExchange with pointers: 现在,我尝试将InterlockedCompareExchange与指针一起使用:

program Project1;

uses Windows;

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

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

begin
  Test;
end.

and it does not compile with the message 并且它不与消息一起编译

project1.lpr(10,50) Error: Incompatible type for arg no. project1.lpr(10,50)错误:arg号不兼容的类型。 3: Got "Pointer", expected "LongInt" 3:获得“指针”,预期获得“ LongInt”

so evidently the "overload" does not work. 因此显然“超载”不起作用。 How to fix it? 如何解决?

I am using 64-bit FPC 3.0 on default (64-bit) target. 我在默认(64位)目标上使用64位FPC 3.0。

The following workaround 以下解决方法

{$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}

compiles and ensures Delphi/FPC compatibility; 编译并确保Delphi / FPC兼容性; looks like the absence of InterlockedCompareExchangePointer declaration is FPC bug that should be fixed. 似乎缺少InterlockedCompareExchangePointer声明是应该解决的FPC错误。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM