Hi,
Is cwstring threadsave?
I suspect there is a problem.
Martin

Mailing lists / fpc-devel / 2006-04
7 messages · 3 people · –
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
Hi,
Is cwstring threadsave?
I suspect there is a problem.
Martin
The only problem I could imagine is that the caching of of the iconv_t handles causes problem, since they could be accessed multiple times without lock.
I'll try to implement locking and check if my problems are gone.
Martin
The only problem I could imagine is that the caching of of the iconv_t handles causes…
The problems are gone with locking.
Martin
Index: cwstring.pp
=================================================================== --- cwstring.pp (revision 3190)
+++ cwstring.pp (working copy)
@@ -109,7 +109,25 @@
iconv_ucs42ansi,
iconv_ansi2wide,
iconv_wide2ansi : iconv_t;+
+ lock_ansi2ucs4,
+ lock_ucs42ansi,
+ lock_ansi2wide,
+ lock_wide2ansi : integer;
+procedure lockiconv(var lockcount: integer);
+begin
+ while interlockedincrement(lockcount) > 1 do begin
+ interlockeddecrement(lockcount);
+ sleep(0);
+ end;
+end;
+
+procedure unlockiconv(var lockcount: integer);
+begin
+ interlockeddecrement(lockcount);
+end;
+
procedure Wide2AnsiMove(source:pwidechar;var dest:ansistring;len:SizeInt);
var
outlength,
@@ -130,6 +148,7 @@
srcpos:=source;
destpos:=pchar(dest);
outleft:=outlength;
+ lockiconv(lock_wide2ansi);
while iconv(iconv_wide2ansi,@srcpos,@srclen,@destpos,@outleft)=size_t(-1)
do
begin
case fpgetCerrno of
@@ -158,6 +177,7 @@
raise EConvertError.Create('iconv error '+IntToStr(fpgetCerrno));
end;
end;
+ unlockiconv(lock_wide2ansi);
// truncate string
setlength(dest,length(dest)-outleft);
end;
@@ -182,6 +202,7 @@
srcpos:=source;
destpos:=pchar(dest);
outleft:=outlength*2;
+ lockiconv(lock_ansi2wide);
while iconv(iconv_ansi2wide,@srcpos,@len,@destpos,@outleft)=size_t(-1) do
begin
case fpgetCerrno of
@@ -209,6 +230,7 @@
raise EConvertError.Create('iconv error '+IntToStr(fpgetCerrno));
end;
end;
+ unlockiconv(lock_ansi2wide);
// truncate string
setlength(dest,length(dest)-outleft div 2);
end;
@@ -253,6 +275,7 @@
srcpos:=source;
destpos:=pchar(dest);
outleft:=outlength*4;
+ lockiconv(lock_ansi2ucs4);
while iconv(iconv_ansi2ucs4,@srcpos,@len,@destpos,@outleft)=size_t(-1) do
begin
case fpgetCerrno of
@@ -270,6 +293,7 @@
raise EConvertError.Create('iconv error '+IntToStr(fpgetCerrno));
end;
end;
+ unlockiconv(lock_ansi2ucs4);
// truncate string
setlength(dest,length(dest)-outleft div 4);
end;Locking or creating this handles locally might have a heavy impact on performance. Anbody a good idea?
The problems are gone with locking. Martin Index: cwstring.pp…
Martin Schreiber wroteThe problems are gone with locking.
Attached optimized Version.
Martin
Locking or creating this handles locally might have a heavy impact on performance. Anbody…
Since cwstring afaik is alreay pluggable, have a threadsafe manager and a non-threadsafe mgr ?
cwstring and cwstring_r ? (:-)