2016-04-05 6 views
1

J'ai installé Lazarus 1.6/FPC 3.0, 64 bits sur Windows 10 (64 bits) et le portage du code Delphi contenant la fonction InterlockedCompareExchangePointer.FPC 3.0 et InterlockedCompareExchange

FPC 3.0 n'inclut pas la déclaration InterlockedCompareExchangePointer; Au contraire, elle "surcharge" InterlockedCompareExchange comme on peut le voir dans 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} 

Maintenant, je suis en train d'utiliser InterlockedCompareExchange avec des pointeurs:

program Project1; 

uses Windows; 

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

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

begin 
    Test; 
end. 

et il ne compilent pas avec le message

project1.lpr(10,50) Error: Incompatible type for arg no. 3: Got "Pointer", expected "LongInt"

si évidemment la "surcharge" ne fonctionne pas. Comment le réparer? J'utilise le FPC 3.0 64 bits sur la cible par défaut (64 bits).

+0

Essayez d'utiliser la fonction '' InterlockedCompareExchangePointer' de jwawinbase' unité. Ou supprimez l'unité 'windows' de la clause' uses' ou appelez la fonction comme 'System.InterlockedCompareExchange' car il s'agit d'une plateforme croisée. – Abelisto

+0

Ceux-ci devraient tous être des intrinsèques du compilateur dans un monde idéal –

Répondre

2

la solution suivante

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

compile et assure la compatibilité Delphi/FPC; ressemble à l'absence de déclaration InterlockedCompareExchangePointer est un bug FPC qui devrait être corrigé.

+0

J'ai ajouté: http://bugs.freepascal.org/view.php?id=29965 – HNB