ldecNumber

Check-in [55d4fc73d1]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Make context table weak on keys so threads are GC'd if cache off. Cache is off by default. Add test for thread memory leak.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:55d4fc73d18d4c6b05fe89ac42e4679b6e126ef6
User & Date: e@6e5be3b1-1950-f047-a965-c680c9cf6ecc 2007-06-18 21:32:44
Context
2007-08-07
22:26
Update to decnumber 3.41. Use dectest 2.55. check-in: 3cc84d46a5 user: e@6e5be3b1-1950-f047-a965-c680c9cf6ecc tags: trunk
2007-06-18
21:32
Make context table weak on keys so threads are GC'd if cache off. Cache is off by default. Add test for thread memory leak. check-in: 55d4fc73d1 user: e@6e5be3b1-1950-f047-a965-c680c9cf6ecc tags: trunk
2007-01-17
04:55
Update to decNumber 3.37 Add random number generator Add pod documentation check-in: acd5876087 user: e@6e5be3b1-1950-f047-a965-c680c9cf6ecc tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to ldecNumber.c.

39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
...
961
962
963
964
965
966
967
968








969
970
971
972
973
974
975
976

// #define LDN_SEAL_TABLES // to make tables and metatables read only

// testing
// #define LDN_CACHE_TEST 1

#ifndef LDN_ENABLE_CACHE
#define LDN_ENABLE_CACHE 1
#endif

#ifndef LDN_ENABLE_RANDOM
#define LDN_ENABLE_RANDOM 1
#endif

#ifndef LDN_CONTEXT_DEFAULT
................................................................................
}

LUALIB_API int luaopen_ldecNumber(lua_State *L)
{
    /* initialize constants */
    dn_dnc_init ();
    /* create a shared environment for the decNumber functions */
    lua_createtable (L, 0, 5);








    lua_replace(L, LUA_ENVIRONINDEX);
    /* create decNumber global table and register decNumber functions */
    luaL_register (L, DN_NAME, dn_lib);
    {
        int i = 0;
        /* add constants to global table */
        while (dn_constants[i].name)
        {







|







 







|
>
>
>
>
>
>
>
>
|







39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
...
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984

// #define LDN_SEAL_TABLES // to make tables and metatables read only

// testing
// #define LDN_CACHE_TEST 1

#ifndef LDN_ENABLE_CACHE
#define LDN_ENABLE_CACHE 0
#endif

#ifndef LDN_ENABLE_RANDOM
#define LDN_ENABLE_RANDOM 1
#endif

#ifndef LDN_CONTEXT_DEFAULT
................................................................................
}

LUALIB_API int luaopen_ldecNumber(lua_State *L)
{
    /* initialize constants */
    dn_dnc_init ();
    /* create a shared environment for the decNumber functions */
    lua_createtable (L, 0, 5);     /* the shared environment */
#if !LDN_ENABLE_CACHE
    /* when cache is off (default: off), make keys weak so threads are GC'd */
    lua_createtable (L, 0, 1);     /* its metatable, which is */
    lua_pushliteral (L, "__mode"); /* used to make environment */
    lua_pushliteral (L, "k");      /* weak in the keys */
    lua_rawset (L, -3);            /* metatable.__mode = "k" */
    lua_setmetatable (L, -2);      /* set the environment metatable */
#endif
    lua_replace (L, LUA_ENVIRONINDEX); /* the new c function environment */
    /* create decNumber global table and register decNumber functions */
    luaL_register (L, DN_NAME, dn_lib);
    {
        int i = 0;
        /* add constants to global table */
        while (dn_constants[i].name)
        {

Changes to test/ldecNumberPerf.lua.

67
68
69
70
71
72
73


74

75











tm = pt:lap()

print(string.format("Math loop: %s s", tm))

print(n1,n2,n3,n4)























>
>

>

>
>
>
>
>
>
>
>
>
>
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

tm = pt:lap()

print(string.format("Math loop: %s s", tm))

print(n1,n2,n3,n4)

n2 = decNumber.tonumber "3"
n3 = decNumber.tonumber "5"

tm = pt:lap()

for i = 1,100000
do
    n1 = n2+n3
end

tm = pt:lap()

print(string.format("Add loop: %s s", tm))

print(n1)

Added test/ldecNumberThreadsGcTest.lua.

















































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
--[[ ldecNumberThreadsTest.lua
*  Lua wrapper for decNumber -- thread context testing
*  created September 7, 2006 by e
*
* Copyright (c) 2006 Doug Currie, Londonderry, NH
* All rights reserved.
* 
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, and/or sell copies of the Software, and to permit persons
to whom the Software is furnished to do so, provided that the above
copyright notice(s) and this permission notice appear in all copies of
the Software and that both the above copyright notice(s) and this
permission notice appear in supporting documentation.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*********************************************************************]]--

require "ldecNumber"
-- require "lunit" -- http://www.nessie.de/mroth/lunit/  -- using 0.4a pre
-- for now, visual inspection is necessary

-- the test is to be sure each thread has an independent context
-- each thread uses a different rounding mode

ctx = decNumber.getcontext()
ctx:setdefault (decNumber.INIT_DECIMAL128)
ctx:setdigits (69)

local make_thread = function (mode)
    local n1 = decNumber.tonumber(335)
    local n2 = decNumber.tonumber(10)
    return function ()
        local ctx = decNumber.getcontext()
        ctx:setdefault (decNumber.INIT_DECIMAL32)
        ctx:setround(mode)
        while (true)
        do
            --print (string.format ("t1 %s / %s = %s",
            --        n1:tostring(), n2:tostring(), (n1/n2):tointegralvalue():tostring()))
            n1 = n1 + 10
            coroutine.yield()
        end
    end
end

local c1 = coroutine.create(make_thread(decNumber.ROUND_HALF_DOWN))
local c2 = coroutine.create(make_thread(decNumber.ROUND_HALF_EVEN))

print (coroutine.status(c1))
print (coroutine.status(c1))

print (coroutine.resume(c1))
print (coroutine.resume(c2))

local c1_threads = 1
local c2_threads = 1

for i = 1,30000
do
    if (math.random() >= 0.5)
    then if (math.random() >= 0.75)
         then coroutine.resume (c1)
         else c1 = coroutine.create(make_thread(decNumber.ROUND_HALF_DOWN))
              c1_threads = c1_threads + 1
         end
    else if (math.random() >= 0.25)
         then coroutine.resume (c2)
         else c2 = coroutine.create(make_thread(decNumber.ROUND_HALF_EVEN))
              c2_threads = c2_threads + 1
         end
    end
    if i % 100 == 0 
    then collectgarbage"collect"; print("Memory: ", collectgarbage"count") 
    end
end

print(c1_threads,c2_threads)