diff options
| author | Benoit Germain <bnt.germain@gmail.com> | 2025-09-20 14:30:57 +0200 |
|---|---|---|
| committer | Benoit Germain <bnt.germain@gmail.com> | 2025-09-20 14:30:57 +0200 |
| commit | c52571736d852d2636bd285d19c613be5c706cff (patch) | |
| tree | 50a1fc9b867197faa695c728008c8d0c7498b756 /src/lanes.lua | |
| parent | dc3de6ef8d4bb1a8ce7b2932515a0f6287ab1e76 (diff) | |
| download | lanes-c52571736d852d2636bd285d19c613be5c706cff.tar.gz lanes-c52571736d852d2636bd285d19c613be5c706cff.tar.bz2 lanes-c52571736d852d2636bd285d19c613be5c706cff.zip | |
Improve table and userdata conversions
* add convert_fallback and convert_max_attempts to global settings
* if no __lanesconvert is available, use convert_fallback (can be useful for externally provided full userdata with fixed metatables)
* only try conversion on non-deep and non-clonable userdata
* conversion can be applied recursively, up to convert_max_attempts times
* plus all the relevant unit tests of course
Diffstat (limited to 'src/lanes.lua')
| -rw-r--r-- | src/lanes.lua | 28 |
1 files changed, 23 insertions, 5 deletions
diff --git a/src/lanes.lua b/src/lanes.lua index c5b3315..43ebfd5 100644 --- a/src/lanes.lua +++ b/src/lanes.lua | |||
| @@ -93,6 +93,8 @@ local default_params = | |||
| 93 | { | 93 | { |
| 94 | -- LuaJIT provides a thread-unsafe allocator by default, so we need to protect it when used in parallel lanes | 94 | -- LuaJIT provides a thread-unsafe allocator by default, so we need to protect it when used in parallel lanes |
| 95 | allocator = isLuaJIT and "protected" or nil, | 95 | allocator = isLuaJIT and "protected" or nil, |
| 96 | convert_fallback = nil, | ||
| 97 | convert_max_attempts = 1, | ||
| 96 | -- it looks also like LuaJIT allocator may not appreciate direct use of its allocator for other purposes than the VM operation | 98 | -- it looks also like LuaJIT allocator may not appreciate direct use of its allocator for other purposes than the VM operation |
| 97 | internal_allocator = isLuaJIT and "libc" or "allocator", | 99 | internal_allocator = isLuaJIT and "libc" or "allocator", |
| 98 | keepers_gc_threshold = -1, | 100 | keepers_gc_threshold = -1, |
| @@ -125,6 +127,23 @@ local param_checkers = | |||
| 125 | end | 127 | end |
| 126 | return true | 128 | return true |
| 127 | end, | 129 | end, |
| 130 | convert_fallback = function(val_) | ||
| 131 | -- convert_fallback should be nil, lanes.null or 'decay' | ||
| 132 | if val_ == nil or val_ == core.null or val_ == 'decay' then | ||
| 133 | return true | ||
| 134 | end | ||
| 135 | return nil, "must be nil, lanes.null or 'decay'" | ||
| 136 | end, -- convert_fallback | ||
| 137 | convert_max_attempts = function(val_) | ||
| 138 | -- convert_max_attempts should be a number | ||
| 139 | if type(val_) ~= "number" then | ||
| 140 | return nil, "not a number" | ||
| 141 | end | ||
| 142 | if val_ <= 0 or val_ >= 10 then | ||
| 143 | return nil, "value out of range" | ||
| 144 | end | ||
| 145 | return true | ||
| 146 | end, -- convert_fallback | ||
| 128 | internal_allocator = function(val_) | 147 | internal_allocator = function(val_) |
| 129 | -- can be "libc" or "allocator" | 148 | -- can be "libc" or "allocator" |
| 130 | if type(val_) ~= "string" then | 149 | if type(val_) ~= "string" then |
| @@ -850,12 +869,8 @@ local configure = function(settings_) | |||
| 850 | lanes.configure = function() return lanes end -- no need to configure anything again | 869 | lanes.configure = function() return lanes end -- no need to configure anything again |
| 851 | 870 | ||
| 852 | -- now we can configure Lanes core | 871 | -- now we can configure Lanes core |
| 853 | |||
| 854 | |||
| 855 | |||
| 856 | |||
| 857 | |||
| 858 | local settings = core.configure and core.configure(params_checker(settings_)) or core.settings | 872 | local settings = core.configure and core.configure(params_checker(settings_)) or core.settings |
| 873 | assert(type(settings) == 'table') | ||
| 859 | 874 | ||
| 860 | -- | 875 | -- |
| 861 | lanes.ABOUT = | 876 | lanes.ABOUT = |
| @@ -912,7 +927,10 @@ lanesMeta.__index = function(lanes_, k_) | |||
| 912 | -- Access the required key | 927 | -- Access the required key |
| 913 | return lanes_[k_] | 928 | return lanes_[k_] |
| 914 | end | 929 | end |
| 930 | |||
| 915 | lanes.configure = configure | 931 | lanes.configure = configure |
| 932 | -- lanes.null can be used for some configure settings, expose it now | ||
| 933 | lanes.null = assert(core.null) | ||
| 916 | setmetatable(lanes, lanesMeta) | 934 | setmetatable(lanes, lanesMeta) |
| 917 | 935 | ||
| 918 | -- ################################################################################################# | 936 | -- ################################################################################################# |
