[fpc-devel] threadlocalvar unLOCKed variables
A. Bouchez
abouchez at magic.fr
Sat Jan 9 09:50:31 CET 2010
I made a proposal in http://wiki.freepascal.org/Modernised_Pascal
20) threadlocalvar unLOCKed variables i.e. strings and dynamic arrays local to the current thread: reference-counted without the asm LOCK, "copy and write" only with other threadlocalvar, thread-local heap (without any asm LOCK either).
It will increase the speed a lot in multi-core architectures, which don't like asm LOCK (freezes all cores for safe multi-thread).
Note: this LOCK is implemented in the RTL in inclocked() and declocked() processor-specific functions.
function TServer.ComputePage: string;
threadlocalvar
tmp: string; // differs from var tmp: string
i: integer; // i is defined as threadlocalvar, but is the same as normal var
begin
for i := 0 to high(ListStr) do // ListStr[] is a dynamic array
tmp := tmp+ListStr[i]+#13#10; // very fast computation, without LOCK
result := tmp; // copy from local heap to global heap
end;
To implement this at the compiler and RTL level, we could use reference count < -2 for these variables (-1=const, -2=ref 0, -3=ref 1..). For example:
Procedure fpc_AnsiStr_Incr_Ref (S : Pointer); [Public,Alias:'FPC_ANSISTR_INCR_REF']; compilerproc; {$IFNDEF VER2_0} Inline; {$ENDIF}
Begin
If S<>Nil then
If PAnsiRec(S-FirstOff)^.Ref<0 then begin
If PAnsiRec(S-FirstOff)^.Ref<-1 then // -1 for const
dec(PAnsiRec(S-FirstOff)^.Ref); // threadlocalvar fast reference count
end else
inclocked(PAnsiRec(S-FirstOff)^.Ref); // threadsafe reference count
end;
And a local threadheap should be implemented for such threadlocalvar, with threadlocalgetmem() and such functions.
Another possibility should be do add a "threadlocal" attribute for types, to be used for local variables and properties:
TServer = class
public
ListStr: threadlocal array of string;
...
In this case, the method above should begin with:
function TServer.ComputePage: string;
var
tmp: threadlocal string; // differs from var tmp: string
....
But this syntax sound a bit not "pascalish", whereas the threadlocalvar does (it sounds like the treadvar feature) ... another idea?
More information about the fpc-devel
mailing list