aboutsummaryrefslogtreecommitdiff
path: root/src/lanes.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/lanes.cpp')
-rw-r--r--src/lanes.cpp62
1 files changed, 41 insertions, 21 deletions
diff --git a/src/lanes.cpp b/src/lanes.cpp
index d1a353b..0eaeb3e 100644
--- a/src/lanes.cpp
+++ b/src/lanes.cpp
@@ -137,13 +137,15 @@ LUAG_FUNC(set_singlethreaded)
137LUAG_FUNC(set_thread_priority) 137LUAG_FUNC(set_thread_priority)
138{ 138{
139 lua_Integer const _prio{ luaL_checkinteger(L_, 1) }; 139 lua_Integer const _prio{ luaL_checkinteger(L_, 1) };
140 NativePrioFlag const _native{ std::string_view{ "native" } == luaL_optstring(L_, 2, "mapped") };
140 // public Lanes API accepts a generic range -3/+3 141 // public Lanes API accepts a generic range -3/+3
141 // that will be remapped into the platform-specific scheduler priority scheme 142 // that will be remapped into the platform-specific scheduler priority scheme
142 // On some platforms, -3 is equivalent to -2 and +3 to +2 143 // On some platforms, -3 is equivalent to -2 and +3 to +2
143 if (_prio < kThreadPrioMin || _prio > kThreadPrioMax) { 144 if (!_native && (_prio < kThreadPrioMin || _prio > kThreadPrioMax)) {
144 raise_luaL_error(L_, "priority out of range: %d..+%d (%d)", kThreadPrioMin, kThreadPrioMax, _prio); 145 raise_luaL_error(L_, "priority out of range: %d..+%d (%d)", kThreadPrioMin, kThreadPrioMax, _prio);
145 } 146 }
146 THREAD_SET_PRIORITY(static_cast<int>(_prio), Universe::Get(L_)->sudo); 147
148 THREAD_SET_PRIORITY(L_, static_cast<int>(_prio), _native, Universe::Get(L_)->sudo);
147 return 0; 149 return 0;
148} 150}
149 151
@@ -155,7 +157,8 @@ LUAG_FUNC(set_thread_affinity)
155 if (_affinity <= 0) { 157 if (_affinity <= 0) {
156 raise_luaL_error(L_, "invalid affinity (%d)", _affinity); 158 raise_luaL_error(L_, "invalid affinity (%d)", _affinity);
157 } 159 }
158 THREAD_SET_AFFINITY(static_cast<unsigned int>(_affinity)); 160
161 THREAD_SET_AFFINITY(L_, static_cast<unsigned int>(_affinity));
159 return 0; 162 return 0;
160} 163}
161 164
@@ -236,9 +239,26 @@ int lanes_register(lua_State* const L_)
236 239
237// ################################################################################################# 240// #################################################################################################
238 241
242LUAG_FUNC(thread_priority_range)
243{
244 NativePrioFlag const _native{ std::string_view{ "native" } == luaL_optstring(L_, 1, "mapped") };
245 if (_native) {
246 auto const [_prio_min, _prio_max] = THREAD_NATIVE_PRIOS();
247 lua_pushinteger(L_, _prio_min);
248 lua_pushinteger(L_, _prio_max);
249 } else {
250 lua_pushinteger(L_, kThreadPrioMin);
251 lua_pushinteger(L_, kThreadPrioMax);
252 }
253 return 2;
254}
255
256// #################################################################################################
257
239//--- [] means can be nil 258//--- [] means can be nil
240// lane_ud = lane_new( function 259// lane_ud = lane_new( function
241// , [libs_str] 260// , [libs_str]
261// , [prio_is_native_bool]
242// , [priority_int] 262// , [priority_int]
243// , [globals_tbl] 263// , [globals_tbl]
244// , [package_tbl] 264// , [package_tbl]
@@ -255,15 +275,16 @@ LUAG_FUNC(lane_new)
255{ 275{
256 static constexpr StackIndex kFuncIdx{ 1 }; 276 static constexpr StackIndex kFuncIdx{ 1 };
257 static constexpr StackIndex kLibsIdx{ 2 }; 277 static constexpr StackIndex kLibsIdx{ 2 };
258 static constexpr StackIndex kPrioIdx{ 3 }; 278 static constexpr StackIndex kPrinIdx{ 3 };
259 static constexpr StackIndex kGlobIdx{ 4 }; 279 static constexpr StackIndex kPrioIdx{ 4 };
260 static constexpr StackIndex kPackIdx{ 5 }; 280 static constexpr StackIndex kGlobIdx{ 5 };
261 static constexpr StackIndex kRequIdx{ 6 }; 281 static constexpr StackIndex kPackIdx{ 6 };
262 static constexpr StackIndex kGcCbIdx{ 7 }; 282 static constexpr StackIndex kRequIdx{ 7 };
263 static constexpr StackIndex kNameIdx{ 8 }; 283 static constexpr StackIndex kGcCbIdx{ 8 };
264 static constexpr StackIndex kErTlIdx{ 9 }; 284 static constexpr StackIndex kNameIdx{ 9 };
265 static constexpr StackIndex kAsCoro{ 10 }; 285 static constexpr StackIndex kErTlIdx{ 10 };
266 static constexpr StackIndex kFixedArgsIdx{ 10 }; 286 static constexpr StackIndex kAsCoro{ 11 };
287 static constexpr StackIndex kFixedArgsIdx{ 11 };
267 288
268 int const _nargs{ lua_gettop(L_) - kFixedArgsIdx }; 289 int const _nargs{ lua_gettop(L_) - kFixedArgsIdx };
269 LUA_ASSERT(L_, _nargs >= 0); 290 LUA_ASSERT(L_, _nargs >= 0);
@@ -400,21 +421,22 @@ LUAG_FUNC(lane_new)
400 // public Lanes API accepts a generic range -3/+3 421 // public Lanes API accepts a generic range -3/+3
401 // that will be remapped into the platform-specific scheduler priority scheme 422 // that will be remapped into the platform-specific scheduler priority scheme
402 // On some platforms, -3 is equivalent to -2 and +3 to +2 423 // On some platforms, -3 is equivalent to -2 and +3 to +2
403 int const _priority{ 424 auto const [_priority, _native] {
404 std::invoke([L = L_]() { 425 std::invoke([L = L_]() {
426 NativePrioFlag const _native{ static_cast<bool>(lua_toboolean(L, kPrinIdx)) };
405 StackIndex const _prio_idx{ lua_isnoneornil(L, kPrioIdx) ? kIdxNone : kPrioIdx }; 427 StackIndex const _prio_idx{ lua_isnoneornil(L, kPrioIdx) ? kIdxNone : kPrioIdx };
406 if (_prio_idx == 0) { 428 if (_prio_idx == kIdxNone) {
407 return kThreadPrioDefault; 429 return std::make_pair(kThreadPrioDefault, _native);
408 } 430 }
409 int const _priority{ static_cast<int>(lua_tointeger(L, _prio_idx)) }; 431 int const _priority{ static_cast<int>(lua_tointeger(L, _prio_idx)) };
410 if ((_priority < kThreadPrioMin || _priority > kThreadPrioMax)) { 432 if (!_native && (_priority < kThreadPrioMin || _priority > kThreadPrioMax)) {
411 raise_luaL_error(L, "Priority out of range: %d..+%d (%d)", kThreadPrioMin, kThreadPrioMax, _priority); 433 raise_luaL_error(L, "Priority out of range: %d..+%d (%d)", kThreadPrioMin, kThreadPrioMax, _priority);
412 } 434 }
413 return _priority; 435 return std::make_pair(_priority, _native);
414 }) 436 })
415 }; 437 };
416 438
417 _lane->startThread(_priority); 439 _lane->startThread(L_, _priority, _native);
418 440
419 STACK_GROW(_L2, _nargs + 3); 441 STACK_GROW(_L2, _nargs + 3);
420 STACK_GROW(L_, 3); 442 STACK_GROW(L_, 3);
@@ -658,6 +680,7 @@ namespace {
658 { Universe::kFinally, Universe::InitializeFinalizer }, 680 { Universe::kFinally, Universe::InitializeFinalizer },
659 { "linda", LG_linda }, 681 { "linda", LG_linda },
660 { "nameof", LG_nameof }, 682 { "nameof", LG_nameof },
683 { "thread_priority_range", LG_thread_priority_range },
661 { "now_secs", LG_now_secs }, 684 { "now_secs", LG_now_secs },
662 { "register", lanes_register }, 685 { "register", lanes_register },
663 { "set_singlethreaded", LG_set_singlethreaded }, 686 { "set_singlethreaded", LG_set_singlethreaded },
@@ -753,9 +776,6 @@ LUAG_FUNC(configure)
753 ); // L_: settings M VERSION 776 ); // L_: settings M VERSION
754 lua_setfield(L_, -2, "version"); // L_: settings M 777 lua_setfield(L_, -2, "version"); // L_: settings M
755 778
756 lua_pushinteger(L_, kThreadPrioMax); // L_: settings M kThreadPrioMax
757 lua_setfield(L_, -2, "max_prio"); // L_: settings M
758
759 kCancelError.pushKey(L_); // L_: settings M kCancelError 779 kCancelError.pushKey(L_); // L_: settings M kCancelError
760 lua_setfield(L_, -2, "cancel_error"); // L_: settings M 780 lua_setfield(L_, -2, "cancel_error"); // L_: settings M
761 781