lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Roberto Ierusalimschy wrote:
> > 
> > Do the new setlocale calls in ldo.c change thread safety assumptions
> > Lua?  http://stackoverflow.com/questions/4057319/is-setlocale-thread-safe-function
> 
> It seems so :(
> 
> Suggestions?

If I got it right, setlocale was added mainly to support EBCDIC systems.

Some suggestions:

 a) Declare the character set/encoding for Lua to be ASCII.
    Let the EBCDIC users deal with the conversion.

 b) Create the ctype table on the fly when building Lua.
    Maybe become difficult when cross-compiling (HOST_CC needed
    which uses the same encoding as the TARGET_CC).

 c) Provide a dedicated EBCDIC ctype table selected via some
    "#ifdef EBCDIC".

 d) You could provide hooks to let EBCDIC/strange-encoding users
    provide conversion routines.  Maybe a simple LOCAL_TO_ASCII(c)
    macro suffices.  Whether that macro should be used for ranges
    in Lua patterns (i.e. [k-p]) I don't know.
    Afaik, newer gcc uses iconv-lib and "-finput/exec-charset"
    to convert the charset on the fly...


Btw, setlocale is not really cheap.  For example:

|# time src/lua -e 'os.setlocale"de_DE" for i=1,100000 do loadstring"a=1" end'
|real	0m9.738s
|user	0m9.630s
|sys	0m0.090s
|
|# time src/lua-no-setlocale -e 'os.setlocale"de_DE" for i=1,100000 do loadstring"a=1" end'
|real	0m3.984s
|user	0m3.980s
|sys	0m0.010s

The lua-no-setlocale has only the two setlocale("C"/savedlocale) calls
in ldo.c disabled.

I think I even saw some trivial implementations that loaded the tables
from disk each time setlocale was called.


And the newlocale stuff is rather new (introduced in POSIX 2008) and probably
not available on EBCDIC systems (not even on one of my older Linux systems).

Ciao, ET.