diff options
author | Benoit Germain <benoit.germain@ubisoft.com> | 2024-06-12 18:18:24 +0200 |
---|---|---|
committer | Benoit Germain <benoit.germain@ubisoft.com> | 2024-06-12 18:18:24 +0200 |
commit | dddc28153796f9c8eb256eddb335c8643226fd0b (patch) | |
tree | 641caa9a01933d0397a99f127cff249d3a77fdb5 /src/linda.cpp | |
parent | c305ff3ed1f51d86ced2bb83b10b5e0632cd98a3 (diff) | |
download | lanes-dddc28153796f9c8eb256eddb335c8643226fd0b.tar.gz lanes-dddc28153796f9c8eb256eddb335c8643226fd0b.tar.bz2 lanes-dddc28153796f9c8eb256eddb335c8643226fd0b.zip |
linda :get(), :set(), :limit() return value changes
Diffstat (limited to 'src/linda.cpp')
-rw-r--r-- | src/linda.cpp | 38 |
1 files changed, 19 insertions, 19 deletions
diff --git a/src/linda.cpp b/src/linda.cpp index 91138c5..316e917 100644 --- a/src/linda.cpp +++ b/src/linda.cpp | |||
@@ -393,7 +393,7 @@ LUAG_FUNC(linda_dump) | |||
393 | // ################################################################################################# | 393 | // ################################################################################################# |
394 | 394 | ||
395 | /* | 395 | /* |
396 | * [val [, ...]] = linda_get( linda_ud, key_num|str|bool|lightuserdata [, count = 1]) | 396 | * count, [val [, ...]]|nil,cancel_error = linda:get(key_num|str|bool|lightuserdata [, count = 1]) |
397 | * | 397 | * |
398 | * Get one or more values from Linda. | 398 | * Get one or more values from Linda. |
399 | */ | 399 | */ |
@@ -412,9 +412,10 @@ LUAG_FUNC(linda_get) | |||
412 | Keeper* const _keeper{ _linda->whichKeeper() }; | 412 | Keeper* const _keeper{ _linda->whichKeeper() }; |
413 | _pushed = keeper_call(_keeper->K, KEEPER_API(get), L_, _linda, 2); | 413 | _pushed = keeper_call(_keeper->K, KEEPER_API(get), L_, _linda, 2); |
414 | } else { // linda is cancelled | 414 | } else { // linda is cancelled |
415 | // do nothing and return lanes.cancel_error | 415 | // do nothing and return nil,lanes.cancel_error |
416 | lua_pushnil(L_); | ||
416 | kCancelError.pushKey(L_); | 417 | kCancelError.pushKey(L_); |
417 | _pushed.emplace(1); | 418 | _pushed.emplace(2); |
418 | } | 419 | } |
419 | // an error can be raised if we attempt to read an unregistered function | 420 | // an error can be raised if we attempt to read an unregistered function |
420 | return OptionalValue(_pushed, L_, "tried to copy unsupported types"); | 421 | return OptionalValue(_pushed, L_, "tried to copy unsupported types"); |
@@ -425,7 +426,7 @@ LUAG_FUNC(linda_get) | |||
425 | // ################################################################################################# | 426 | // ################################################################################################# |
426 | 427 | ||
427 | /* | 428 | /* |
428 | * [true] = linda_limit( linda_ud, key_num|str|bool|lightuserdata, [int]) | 429 | * [bool]|nil,cancel_error = linda:limit(key_num|str|bool|lightuserdata, [int]) |
429 | * | 430 | * |
430 | * Set limit to 1 Linda keys. | 431 | * Set limit to 1 Linda keys. |
431 | * Optionally wake threads waiting to write on the linda, in case the limit enables them to do so | 432 | * Optionally wake threads waiting to write on the linda, in case the limit enables them to do so |
@@ -450,15 +451,15 @@ LUAG_FUNC(linda_limit) | |||
450 | if (_linda->cancelRequest == CancelRequest::None) { | 451 | if (_linda->cancelRequest == CancelRequest::None) { |
451 | Keeper* const _keeper{ _linda->whichKeeper() }; | 452 | Keeper* const _keeper{ _linda->whichKeeper() }; |
452 | _pushed = keeper_call(_keeper->K, KEEPER_API(limit), L_, _linda, 2); | 453 | _pushed = keeper_call(_keeper->K, KEEPER_API(limit), L_, _linda, 2); |
453 | LUA_ASSERT(L_, _pushed.has_value() && (_pushed.value() == 0 || _pushed.value() == 1)); // no error, optional boolean value saying if we should wake blocked writer threads | 454 | LUA_ASSERT(L_, _pushed.has_value() && (_pushed.value() == 1) && luaG_type(L_, -1) == LuaType::BOOLEAN); // no error, boolean value saying if we should wake blocked writer threads |
454 | if (_pushed.value() == 1) { | 455 | if (lua_toboolean(L_, -1)) { |
455 | LUA_ASSERT(L_, luaG_type(L_, -1) == LuaType::BOOLEAN && lua_toboolean(L_, -1) == 1); | ||
456 | _linda->readHappened.notify_all(); // To be done from within the 'K' locking area | 456 | _linda->readHappened.notify_all(); // To be done from within the 'K' locking area |
457 | } | 457 | } |
458 | } else { // linda is cancelled | 458 | } else { // linda is cancelled |
459 | // do nothing and return lanes.cancel_error | 459 | // do nothing and return nil,lanes.cancel_error |
460 | lua_pushnil(L_); | ||
460 | kCancelError.pushKey(L_); | 461 | kCancelError.pushKey(L_); |
461 | _pushed.emplace(1); | 462 | _pushed.emplace(2); |
462 | } | 463 | } |
463 | // propagate pushed boolean if any | 464 | // propagate pushed boolean if any |
464 | return _pushed.value(); | 465 | return _pushed.value(); |
@@ -470,14 +471,13 @@ LUAG_FUNC(linda_limit) | |||
470 | 471 | ||
471 | /* | 472 | /* |
472 | * 2 modes of operation | 473 | * 2 modes of operation |
473 | * [val, key]= linda_receive( linda_ud, [timeout_secs_num=nil], key_num|str|bool|lightuserdata [, ...] ) | 474 | * [val, key]= linda:receive([timeout_secs_num=nil], key_num|str|bool|lightuserdata [, ...] ) |
474 | * Consumes a single value from the Linda, in any key. | 475 | * Consumes a single value from the Linda, in any key. |
475 | * Returns: received value (which is consumed from the slot), and the key which had it | 476 | * Returns: received value (which is consumed from the slot), and the key which had it |
476 | 477 | ||
477 | * [val1, ... valCOUNT]= linda_receive( linda_ud, [timeout_secs_num=-1], linda.batched, key_num|str|bool|lightuserdata, min_COUNT[, max_COUNT]) | 478 | * [val1, ... valCOUNT]= linda_receive( linda_ud, [timeout_secs_num=-1], linda.batched, key_num|str|bool|lightuserdata, min_COUNT[, max_COUNT]) |
478 | * Consumes between min_COUNT and max_COUNT values from the linda, from a single key. | 479 | * Consumes between min_COUNT and max_COUNT values from the linda, from a single key. |
479 | * returns the actual consumed values, or nil if there weren't enough values to consume | 480 | * returns the actual consumed values, or nil if there weren't enough values to consume |
480 | * | ||
481 | */ | 481 | */ |
482 | LUAG_FUNC(linda_receive) | 482 | LUAG_FUNC(linda_receive) |
483 | { | 483 | { |
@@ -763,7 +763,7 @@ LUAG_FUNC(linda_send) | |||
763 | // ################################################################################################# | 763 | // ################################################################################################# |
764 | 764 | ||
765 | /* | 765 | /* |
766 | * [true|lanes.cancel_error] = linda_set( linda_ud, key_num|str|bool|lightuserdata [, value [, ...]]) | 766 | * [true|nil,lanes.cancel_error] = linda:set(key_num|str|bool|lightuserdata [, value [, ...]]) |
767 | * | 767 | * |
768 | * Set one or more value to Linda. Ignores limits. | 768 | * Set one or more value to Linda. Ignores limits. |
769 | * | 769 | * |
@@ -773,7 +773,7 @@ LUAG_FUNC(linda_set) | |||
773 | { | 773 | { |
774 | auto set = [](lua_State* L_) { | 774 | auto set = [](lua_State* L_) { |
775 | Linda* const _linda{ ToLinda<false>(L_, 1) }; | 775 | Linda* const _linda{ ToLinda<false>(L_, 1) }; |
776 | bool const _has_value{ lua_gettop(L_) > 2 }; | 776 | bool const _has_data{ lua_gettop(L_) > 2 }; |
777 | // make sure the key is of a valid type (throws an error if not the case) | 777 | // make sure the key is of a valid type (throws an error if not the case) |
778 | check_key_types(L_, 2, 2); | 778 | check_key_types(L_, 2, 2); |
779 | 779 | ||
@@ -782,22 +782,22 @@ LUAG_FUNC(linda_set) | |||
782 | if (_linda->cancelRequest == CancelRequest::None) { | 782 | if (_linda->cancelRequest == CancelRequest::None) { |
783 | _pushed = keeper_call(_keeper->K, KEEPER_API(set), L_, _linda, 2); | 783 | _pushed = keeper_call(_keeper->K, KEEPER_API(set), L_, _linda, 2); |
784 | if (_pushed.has_value()) { // no error? | 784 | if (_pushed.has_value()) { // no error? |
785 | LUA_ASSERT(L_, _pushed.value() == 0 || _pushed.value() == 1); | 785 | LUA_ASSERT(L_, _pushed.value() == 1 && luaG_type(L_, -1) == LuaType::BOOLEAN); |
786 | 786 | ||
787 | if (_has_value) { | 787 | if (_has_data) { |
788 | // we put some data in the slot, tell readers that they should wake | 788 | // we put some data in the slot, tell readers that they should wake |
789 | _linda->writeHappened.notify_all(); // To be done from within the 'K' locking area | 789 | _linda->writeHappened.notify_all(); // To be done from within the 'K' locking area |
790 | } | 790 | } |
791 | if (_pushed.value() == 1) { | 791 | if (lua_toboolean(L_, -1)) { |
792 | // the key was full, but it is no longer the case, tell writers they should wake | 792 | // the key was full, but it is no longer the case, tell writers they should wake |
793 | LUA_ASSERT(L_, luaG_type(L_, -1) == LuaType::BOOLEAN && lua_toboolean(L_, -1) == 1); | ||
794 | _linda->readHappened.notify_all(); // To be done from within the 'K' locking area | 793 | _linda->readHappened.notify_all(); // To be done from within the 'K' locking area |
795 | } | 794 | } |
796 | } | 795 | } |
797 | } else { // linda is cancelled | 796 | } else { // linda is cancelled |
798 | // do nothing and return lanes.cancel_error | 797 | // do nothing and return nil,lanes.cancel_error |
798 | lua_pushnil(L_); | ||
799 | kCancelError.pushKey(L_); | 799 | kCancelError.pushKey(L_); |
800 | _pushed.emplace(1); | 800 | _pushed.emplace(2); |
801 | } | 801 | } |
802 | 802 | ||
803 | // must trigger any error after keeper state has been released | 803 | // must trigger any error after keeper state has been released |