| 1 |
{ |
| 2 |
********************************************************************** |
| 3 |
This file is part of the Free Pascal run time library. |
| 4 |
|
| 5 |
See the file COPYING.FPC, included in this distribution, |
| 6 |
for details about the license. |
| 7 |
********************************************************************** |
| 8 |
|
| 9 |
System depending code for light weight threads. |
| 10 |
|
| 11 |
Copyright (C) 2008 Mattias Gaertner [email protected] |
| 12 |
|
| 13 |
} |
| 14 |
unit MTPCPU; |
| 15 |
|
| 16 |
{$mode objfpc}{$H+} |
| 17 |
{$inline on} |
| 18 |
|
| 19 |
interface |
| 20 |
|
| 21 |
{$IF defined(windows)} |
| 22 |
uses Windows; |
| 23 |
{$ELSEIF defined(freebsd) or defined(darwin)} |
| 24 |
uses ctypes, sysctl; |
| 25 |
{$ELSEIF defined(linux)} |
| 26 |
{$linklib c} |
| 27 |
uses ctypes; |
| 28 |
{$ENDIF} |
| 29 |
|
| 30 |
function GetSystemThreadCount: integer; |
| 31 |
|
| 32 |
procedure CallLocalProc(AProc, Frame: Pointer; Param1: PtrInt; |
| 33 |
Param2, Param3: Pointer); inline; |
| 34 |
|
| 35 |
implementation |
| 36 |
|
| 37 |
{$IFDEF Linux} |
| 38 |
const |
| 39 |
_SC_NPROCESSORS_CONF = 83; |
| 40 |
_SC_NPROCESSORS_ONLN = 84; |
| 41 |
function sysconf(i: cint): clong; cdecl; external name 'sysconf'; |
| 42 |
{$ENDIF} |
| 43 |
|
| 44 |
function GetSystemThreadCount: integer; |
| 45 |
// returns a good default for the number of threads on this system |
| 46 |
{$IF defined(windows)} |
| 47 |
//returns total number of processors available to system including logical hyperthreaded processors |
| 48 |
var |
| 49 |
i: Integer; |
| 50 |
ProcessAffinityMask, SystemAffinityMask: DWORD_PTR; |
| 51 |
Mask: DWORD; |
| 52 |
SystemInfo: SYSTEM_INFO; |
| 53 |
begin |
| 54 |
if GetProcessAffinityMask(GetCurrentProcess, ProcessAffinityMask, SystemAffinityMask) |
| 55 |
then begin |
| 56 |
Result := 0; |
| 57 |
for i := 0 to 31 do begin |
| 58 |
Mask := DWord(1) shl i; |
| 59 |
if (ProcessAffinityMask and Mask)<>0 then |
| 60 |
inc(Result); |
| 61 |
end; |
| 62 |
end else begin |
| 63 |
//can't get the affinity mask so we just report the total number of processors |
| 64 |
GetSystemInfo(SystemInfo); |
| 65 |
Result := SystemInfo.dwNumberOfProcessors; |
| 66 |
end; |
| 67 |
end; |
| 68 |
{$ELSEIF defined(UNTESTEDsolaris)} |
| 69 |
begin |
| 70 |
t = sysconf(_SC_NPROC_ONLN); |
| 71 |
end; |
| 72 |
{$ELSEIF defined(freebsd) or defined(darwin)} |
| 73 |
type |
| 74 |
PSysCtl = {$IF FPC_FULLVERSION>=30200}pcint{$ELSE}pchar{$ENDIF}; |
| 75 |
var |
| 76 |
mib: array[0..1] of cint; |
| 77 |
len: csize_t; |
| 78 |
t: cint; |
| 79 |
begin |
| 80 |
mib[0] := CTL_HW; |
| 81 |
mib[1] := HW_NCPU; |
| 82 |
len := sizeof(t); |
| 83 |
fpsysctl(PSysCtl(@mib), 2, @t, @len, Nil, 0); |
| 84 |
Result:=t; |
| 85 |
end; |
| 86 |
{$ELSEIF defined(linux)} |
| 87 |
begin |
| 88 |
Result:=sysconf(_SC_NPROCESSORS_CONF); |
| 89 |
end; |
| 90 |
|
| 91 |
{$ELSE} |
| 92 |
begin |
| 93 |
Result:=1; |
| 94 |
end; |
| 95 |
{$ENDIF} |
| 96 |
|
| 97 |
procedure CallLocalProc(AProc, Frame: Pointer; Param1: PtrInt; |
| 98 |
Param2, Param3: Pointer); inline; |
| 99 |
type |
| 100 |
PointerLocal = procedure(_EBP: Pointer; Param1: PtrInt; |
| 101 |
Param2, Param3: Pointer); |
| 102 |
begin |
| 103 |
PointerLocal(AProc)(Frame, Param1, Param2, Param3); |
| 104 |
end; |
| 105 |
|
| 106 |
end. |
| 107 |
|