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进行了艰苦卓绝的巨大修改,这个工程无疑是浩大的。