2
0

mtpcpu.pas 2.6 KB

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