大家好,又见面了,我是你们的朋友全栈君。
DELPHI是怎么实现跨平台的?
首先跨平台必须要兼容原来的语法,以线程的临界区对象为例:
TCriticalSection = class(TSynchroObject)
{$IFDEF POSIX}
private type
TCritSec = record
FSync: TObject;
procedure Initialize; inline;
procedure Free; inline;
procedure Enter; inline;
procedure Leave; inline;
function TryEnter: Boolean; inline;
end;
{$ENDIF POSIX}
protected
{$IFDEF MSWINDOWS}
FSection: TRTLCriticalSection;
{$ENDIF MSWINDOWS}
{$IFDEF POSIX}
FSection: TCritSec;
{$ENDIF POSIX}
public
constructor Create;
destructor Destroy; override;
procedure Acquire; override;
procedure Release; override;
function TryEnter: Boolean;
procedure Enter; inline;
procedure Leave; inline;
end;
可以看出,已经不单是原来的从WINDOWS OS临界封装,通过{$IFDEF POSIX}跨平台编译开关,增加了跨平台的封装。
下面再以线程类TThread为例:
TThread = class
private type
PSynchronizeRecord = ^TSynchronizeRecord;
TSynchronizeRecord = record
FThread: TObject;
FMethod: TThreadMethod;
FProcedure: TThreadProcedure;
FSynchronizeException: TObject;
end;
private class var
FProcessorCount: Integer;
private
FThreadID: TThreadID;
{$IF Defined(MSWINDOWS)}
FHandle: THandle platform;
{$ELSEIF Defined(POSIX)}
FCreateSuspendedMutex: pthread_mutex_t;
FInitialSuspendDone: Boolean;
{$ENDIF POSIX}
FStarted: Boolean;
FCreateSuspended: Boolean;
FTerminated: Boolean;
FSuspended: Boolean;
FFreeOnTerminate: Boolean;
FFinished: Boolean;
FReturnValue: Integer;
。。。。。。
也和临界区类一样用跨平台编译开关封装了跨平台代码进来。
从上面的2个例子可以看出,DELPHI为了跨平台,对RTL进行了艰苦卓绝的巨大修改,这个工程无疑是浩大的。
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/107081.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...