Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2006-04

cwstring and threads

7 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Florian Klaempfl
to Martin Schreiber
Martin Schreiber wrote 5 lines
Hi, Is cwstring threadsave? I suspect there is a problem.

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.

Reply by email

Martin Schreiber
to Florian Klaempfl
Florian Klaempfl wrote 9 lines
Martin Schreiber wrote: > Hi, > > Is cwstring threadsave? > I suspect there is a problem. 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

Reply by email

Martin Schreiber
to Florian Klaempfl: The only problem I could imagine is that the caching of of the iconv_t handles causes…
Florian Klaempfl wrote 9 lines
Martin Schreiber wrote: > Hi, > > Is cwstring threadsave? > I suspect there is a problem. 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.

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;

Reply by email

Florian Klaempfl
to Martin Schreiber
Martin Schreiber wrote 11 lines
On Sunday 16 April 2006 10.51, Florian Klaempfl wrote: > Martin Schreiber wrote: >> Hi, >> >> Is cwstring threadsave? >> I suspect there is a problem. > 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. The problems are gone with locking.

Locking or creating this handles locally might have a heavy impact on performance. Anbody a good idea?

Show quoted text 86 lines
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; _______________________________________________ fpc-devel maillist - http://lists.freepascal.org/mailman/listinfo/fpc-devel

Reply by email

Marco van de Voort
to Florian Klaempfl: Locking or creating this handles locally might have a heavy impact on performance. Anbody…
Show quoted text 15 lines
Martin Schreiber wrote: > On Sunday 16 April 2006 10.51, Florian Klaempfl wrote: >> Martin Schreiber wrote: >>> Hi, >>> >>> Is cwstring threadsave? >>> I suspect there is a problem. >> 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. > > The problems are gone with locking. Locking or creating this handles locally might have a heavy impact on performance. Anbody a good idea?

Since cwstring afaik is alreay pluggable, have a threadsafe manager and a non-threadsafe mgr ?

cwstring and cwstring_r ? (:-)

Reply by email