Skip to content

Commit

Permalink
new NODIRECTTHREADMANAGER conditional
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaud Bouchez committed Oct 30, 2023
1 parent 32ed8d3 commit e3bc8a8
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 33 deletions.
8 changes: 6 additions & 2 deletions src/core/mormot.core.os.pas
Expand Up @@ -2723,7 +2723,11 @@ procedure DeleteCriticalSection(var cs : TRTLCriticalSection);

{$ifdef OSPOSIX}

{$ifdef OSDARWIN} // try to stabilize MacOS pthreads API calls
{$ifndef OSLINUX} // try to stabilize MacOS/BSD pthreads API calls
{$define NODIRECTTHREADMANAGER}
{$endif OSLINUX}

{$ifdef NODIRECTTHREADMANAGER} // try to stabilize MacOS pthreads API calls
function GetCurrentThreadId: TThreadID; inline;
function TryEnterCriticalSection(var cs: TRTLCriticalSection): integer; inline;
procedure EnterCriticalSection(var cs: TRTLCriticalSection); inline;
Expand All @@ -2747,7 +2751,7 @@ procedure LeaveCriticalSection(var cs: TRTLCriticalSection); inline;
// - defined in mormot.core.os for inlined FpcCurrentThreadManager call
var TryEnterCriticalSection: function(var cs: TRTLCriticalSection): integer;

{$endif OSDARWIN}
{$endif NODIRECTTHREADMANAGER}

{$endif OSPOSIX}

Expand Down
65 changes: 35 additions & 30 deletions src/core/mormot.core.os.posix.inc
Expand Up @@ -560,6 +560,31 @@ begin
result := 0; // fixed result on a window-abstracted system
end;


{$ifdef NODIRECTTHREADMANAGER} // try to stabilize MacOS/BSD pthreads API calls

function GetCurrentThreadId: TThreadID;
begin
result := system.GetCurrentThreadID();
end;

function TryEnterCriticalSection(var cs: TRTLCriticalSection): integer;
begin
result := system.TryEnterCriticalSection(cs);
end;

procedure EnterCriticalSection(var cs: TRTLCriticalSection);
begin
system.EnterCriticalSection(cs);
end;

procedure LeaveCriticalSection(var cs: TRTLCriticalSection);
begin
system.LeaveCriticalSection(cs);
end;

{$endif NODIRECTTHREADMANAGER}

const // Date Translation - see http://en.wikipedia.org/wiki/Julian_day
HoursPerDay = 24;
MinsPerHour = 60;
Expand Down Expand Up @@ -695,28 +720,6 @@ begin
result.MilliSecond := tz.tv_usec div MicroSecsPerMilliSec;
end;

// try to stabilize MacOS pthreads API calls

function GetCurrentThreadId: TThreadID;
begin
result := system.GetCurrentThreadID();
end;

function TryEnterCriticalSection(var cs: TRTLCriticalSection): integer;
begin
result := system.TryEnterCriticalSection(cs);
end;

procedure EnterCriticalSection(var cs: TRTLCriticalSection);
begin
system.EnterCriticalSection(cs);
end;

procedure LeaveCriticalSection(var cs: TRTLCriticalSection);
begin
system.LeaveCriticalSection(cs);
end;

{$else}


Expand Down Expand Up @@ -3743,9 +3746,9 @@ var
SR: TSearchRec;
{$endif OSLINUX}
{$endif OSBSDDARWIN}
{$ifndef OSDARWIN}
{$ifndef NODIRECTTHREADMANAGER}
tm: TThreadManager;
{$endif OSDARWIN}
{$endif NODIRECTTHREADMANAGER}
begin
// retrieve Kernel and Hardware information
StdOutIsTTY := not IsDebuggerPresent and
Expand Down Expand Up @@ -3975,18 +3978,20 @@ begin
[SystemInfo.dwNumberOfProcessors, modname, CpuInfoText]);
// intialize supported APIs
TimeZoneLocalBias := -GetLocalTimeOffset;
{$ifdef OSDARWIN}
mach_timebase_info(mach_timeinfo);
mach_timecoeff := mach_timeinfo.Numer / mach_timeinfo.Denom;
mach_timenanosecond := (mach_timeinfo.Numer = 1) and
(mach_timeinfo.Denom = 1);
{$else}
{$ifndef NODIRECTTHREADMANAGER}
// for inlined RTL calls (avoid one level of redirection)
GetThreadManager(tm);
@GetCurrentThreadId := @tm.GetCurrentThreadId;
@TryEnterCriticalSection := @tm.TryEnterCriticalSection;
@EnterCriticalSection := @tm.EnterCriticalSection;
@LeaveCriticalSection := @tm.LeaveCriticalSection;
{$endif NODIRECTTHREADMANAGER}
{$ifdef OSDARWIN}
mach_timebase_info(mach_timeinfo);
mach_timecoeff := mach_timeinfo.Numer / mach_timeinfo.Denom;
mach_timenanosecond := (mach_timeinfo.Numer = 1) and
(mach_timeinfo.Denom = 1);
{$else}
// try Linux kernel 2.6.32+ or FreeBSD 8.1+ fastest clocks
if clock_gettime(CLOCK_REALTIME_COARSE, @tp) = 0 then
CLOCK_REALTIME_FAST := CLOCK_REALTIME_COARSE;
Expand Down
2 changes: 1 addition & 1 deletion src/mormot.commit.inc
@@ -1 +1 @@
'2.1.6231'
'2.1.6232'
3 changes: 3 additions & 0 deletions src/mormot.defines.inc
Expand Up @@ -40,6 +40,9 @@
// if defined, SetThreadName() would not raise the exception used to set the
// thread name: to be defined if you have issues when debugging your application

{.$define NODIRECTTHREADMANAGER}
// on POSIX, omit direct GetThreadManager() API calls and just use RTL functions

{.$define NOEXCEPTIONINTERCEPT}
// if defined, exceptions shall not be intercepted nor logged

Expand Down

0 comments on commit e3bc8a8

Please sign in to comment.