diff options
Diffstat (limited to 'src/keeper.cpp')
-rw-r--r-- | src/keeper.cpp | 60 |
1 files changed, 30 insertions, 30 deletions
diff --git a/src/keeper.cpp b/src/keeper.cpp index 3e26d5e..5d94944 100644 --- a/src/keeper.cpp +++ b/src/keeper.cpp | |||
@@ -48,9 +48,9 @@ | |||
48 | #include <algorithm> | 48 | #include <algorithm> |
49 | #include <cassert> | 49 | #include <cassert> |
50 | 50 | ||
51 | // ################################################################################### | 51 | // ################################################################################################# |
52 | // Keeper implementation | 52 | // Keeper implementation |
53 | // ################################################################################### | 53 | // ################################################################################################# |
54 | 54 | ||
55 | class keeper_fifo | 55 | class keeper_fifo |
56 | { | 56 | { |
@@ -74,7 +74,7 @@ class keeper_fifo | |||
74 | 74 | ||
75 | static constexpr int CONTENTS_TABLE{ 1 }; | 75 | static constexpr int CONTENTS_TABLE{ 1 }; |
76 | 76 | ||
77 | // ################################################################################################## | 77 | // ################################################################################################# |
78 | 78 | ||
79 | // replaces the fifo ud by its uservalue on the stack | 79 | // replaces the fifo ud by its uservalue on the stack |
80 | [[nodiscard]] static keeper_fifo* prepare_fifo_access(lua_State* L, int idx_) | 80 | [[nodiscard]] static keeper_fifo* prepare_fifo_access(lua_State* L, int idx_) |
@@ -91,7 +91,7 @@ static constexpr int CONTENTS_TABLE{ 1 }; | |||
91 | return fifo; | 91 | return fifo; |
92 | } | 92 | } |
93 | 93 | ||
94 | // ################################################################################################## | 94 | // ################################################################################################# |
95 | 95 | ||
96 | // in: nothing | 96 | // in: nothing |
97 | // out: { first = 1, count = 0, limit = -1} | 97 | // out: { first = 1, count = 0, limit = -1} |
@@ -107,7 +107,7 @@ static constexpr int CONTENTS_TABLE{ 1 }; | |||
107 | return fifo; | 107 | return fifo; |
108 | } | 108 | } |
109 | 109 | ||
110 | // ################################################################################################## | 110 | // ################################################################################################# |
111 | 111 | ||
112 | // in: expect fifo ... on top of the stack | 112 | // in: expect fifo ... on top of the stack |
113 | // out: nothing, removes all pushed values from the stack | 113 | // out: nothing, removes all pushed values from the stack |
@@ -124,7 +124,7 @@ static void fifo_push(lua_State* L, keeper_fifo* fifo_, int count_) | |||
124 | fifo_->count += count_; | 124 | fifo_->count += count_; |
125 | } | 125 | } |
126 | 126 | ||
127 | // ################################################################################################## | 127 | // ################################################################################################# |
128 | 128 | ||
129 | // in: fifo | 129 | // in: fifo |
130 | // out: ...|nothing | 130 | // out: ...|nothing |
@@ -140,7 +140,7 @@ static void fifo_peek(lua_State* L, keeper_fifo* fifo_, int count_) | |||
140 | } | 140 | } |
141 | } | 141 | } |
142 | 142 | ||
143 | // ################################################################################################## | 143 | // ################################################################################################# |
144 | 144 | ||
145 | // in: fifo | 145 | // in: fifo |
146 | // out: remove the fifo from the stack, push as many items as required on the stack (function assumes they exist in sufficient number) | 146 | // out: remove the fifo from the stack, push as many items as required on the stack (function assumes they exist in sufficient number) |
@@ -177,7 +177,7 @@ static void fifo_pop( lua_State* L, keeper_fifo* fifo_, int count_) | |||
177 | } | 177 | } |
178 | } | 178 | } |
179 | 179 | ||
180 | // ################################################################################################## | 180 | // ################################################################################################# |
181 | 181 | ||
182 | // in: linda_ud expected at stack slot idx | 182 | // in: linda_ud expected at stack slot idx |
183 | // out: fifos[ud] | 183 | // out: fifos[ud] |
@@ -205,7 +205,7 @@ static void push_table(lua_State* L, int idx_) | |||
205 | STACK_CHECK(L, 1); | 205 | STACK_CHECK(L, 1); |
206 | } | 206 | } |
207 | 207 | ||
208 | // ################################################################################################## | 208 | // ################################################################################################# |
209 | 209 | ||
210 | int keeper_push_linda_storage(Universe* U, Dest L, void* ptr_, uintptr_t magic_) | 210 | int keeper_push_linda_storage(Universe* U, Dest L, void* ptr_, uintptr_t magic_) |
211 | { | 211 | { |
@@ -258,7 +258,7 @@ int keeper_push_linda_storage(Universe* U, Dest L, void* ptr_, uintptr_t magic_) | |||
258 | return 1; | 258 | return 1; |
259 | } | 259 | } |
260 | 260 | ||
261 | // ################################################################################################## | 261 | // ################################################################################################# |
262 | 262 | ||
263 | // in: linda_ud | 263 | // in: linda_ud |
264 | int keepercall_clear(lua_State* L) | 264 | int keepercall_clear(lua_State* L) |
@@ -274,7 +274,7 @@ int keepercall_clear(lua_State* L) | |||
274 | return 0; | 274 | return 0; |
275 | } | 275 | } |
276 | 276 | ||
277 | // ################################################################################################## | 277 | // ################################################################################################# |
278 | 278 | ||
279 | // in: linda_ud, key, ... | 279 | // in: linda_ud, key, ... |
280 | // out: true|false | 280 | // out: true|false |
@@ -285,7 +285,7 @@ int keepercall_send(lua_State* L) | |||
285 | // get the fifo associated to this key in this linda, create it if it doesn't exist | 285 | // get the fifo associated to this key in this linda, create it if it doesn't exist |
286 | lua_pushvalue(L, 2); // ud key ... fifos key | 286 | lua_pushvalue(L, 2); // ud key ... fifos key |
287 | lua_rawget(L, -2); // ud key ... fifos fifo | 287 | lua_rawget(L, -2); // ud key ... fifos fifo |
288 | if( lua_isnil(L, -1)) | 288 | if (lua_isnil(L, -1)) |
289 | { | 289 | { |
290 | lua_pop(L, 1); // ud key ... fifos | 290 | lua_pop(L, 1); // ud key ... fifos |
291 | std::ignore = fifo_new(L); // ud key ... fifos fifo | 291 | std::ignore = fifo_new(L); // ud key ... fifos fifo |
@@ -311,7 +311,7 @@ int keepercall_send(lua_State* L) | |||
311 | return 1; | 311 | return 1; |
312 | } | 312 | } |
313 | 313 | ||
314 | // ################################################################################################## | 314 | // ################################################################################################# |
315 | 315 | ||
316 | // in: linda_ud, key [, key]? | 316 | // in: linda_ud, key [, key]? |
317 | // out: (key, val) or nothing | 317 | // out: (key, val) or nothing |
@@ -347,13 +347,13 @@ int keepercall_receive(lua_State* L) | |||
347 | return 0; | 347 | return 0; |
348 | } | 348 | } |
349 | 349 | ||
350 | // ################################################################################################## | 350 | // ################################################################################################# |
351 | 351 | ||
352 | // in: linda_ud key mincount [maxcount] | 352 | // in: linda_ud key mincount [maxcount] |
353 | int keepercall_receive_batched(lua_State* L) | 353 | int keepercall_receive_batched(lua_State* L) |
354 | { | 354 | { |
355 | int const min_count{ static_cast<int>(lua_tointeger(L, 3)) }; | 355 | int const min_count{ static_cast<int>(lua_tointeger(L, 3)) }; |
356 | if( min_count > 0) | 356 | if (min_count > 0) |
357 | { | 357 | { |
358 | int const max_count{ static_cast<int>(luaL_optinteger(L, 4, min_count)) }; | 358 | int const max_count{ static_cast<int>(luaL_optinteger(L, 4, min_count)) }; |
359 | lua_settop(L, 2); // ud key | 359 | lua_settop(L, 2); // ud key |
@@ -364,7 +364,7 @@ int keepercall_receive_batched(lua_State* L) | |||
364 | lua_rawget(L, 2); // key fifos fifo | 364 | lua_rawget(L, 2); // key fifos fifo |
365 | lua_remove(L, 2); // key fifo | 365 | lua_remove(L, 2); // key fifo |
366 | keeper_fifo* const fifo{ prepare_fifo_access(L, 2) }; // key fifotbl | 366 | keeper_fifo* const fifo{ prepare_fifo_access(L, 2) }; // key fifotbl |
367 | if( fifo != nullptr && fifo->count >= min_count) | 367 | if (fifo != nullptr && fifo->count >= min_count) |
368 | { | 368 | { |
369 | fifo_pop(L, fifo, std::min( max_count, fifo->count)); // key ... | 369 | fifo_pop(L, fifo, std::min( max_count, fifo->count)); // key ... |
370 | } | 370 | } |
@@ -380,7 +380,7 @@ int keepercall_receive_batched(lua_State* L) | |||
380 | } | 380 | } |
381 | } | 381 | } |
382 | 382 | ||
383 | // ################################################################################################## | 383 | // ################################################################################################# |
384 | 384 | ||
385 | // in: linda_ud key n | 385 | // in: linda_ud key n |
386 | // out: true or nil | 386 | // out: true or nil |
@@ -403,7 +403,7 @@ int keepercall_limit(lua_State* L) | |||
403 | lua_settop(L, 0); | 403 | lua_settop(L, 0); |
404 | // return true if we decide that blocked threads waiting to write on that key should be awakened | 404 | // return true if we decide that blocked threads waiting to write on that key should be awakened |
405 | // this is the case if we detect the key was full but it is no longer the case | 405 | // this is the case if we detect the key was full but it is no longer the case |
406 | if( | 406 | if ( |
407 | ((fifo->limit >= 0) && (fifo->count >= fifo->limit)) // the key was full if limited and count exceeded the previous limit | 407 | ((fifo->limit >= 0) && (fifo->count >= fifo->limit)) // the key was full if limited and count exceeded the previous limit |
408 | && ((limit < 0) || (fifo->count < limit)) // the key is not full if unlimited or count is lower than the new limit | 408 | && ((limit < 0) || (fifo->count < limit)) // the key is not full if unlimited or count is lower than the new limit |
409 | ) | 409 | ) |
@@ -416,7 +416,7 @@ int keepercall_limit(lua_State* L) | |||
416 | return lua_gettop(L); | 416 | return lua_gettop(L); |
417 | } | 417 | } |
418 | 418 | ||
419 | // ################################################################################################## | 419 | // ################################################################################################# |
420 | 420 | ||
421 | // in: linda_ud key [[val] ...] | 421 | // in: linda_ud key [[val] ...] |
422 | //out: true if the linda was full but it's no longer the case, else nothing | 422 | //out: true if the linda was full but it's no longer the case, else nothing |
@@ -462,7 +462,7 @@ int keepercall_set(lua_State* L) | |||
462 | lua_pushvalue(L, 2); // fifos key [val [, ...]] key | 462 | lua_pushvalue(L, 2); // fifos key [val [, ...]] key |
463 | lua_rawget(L, 1); // fifos key [val [, ...]] fifo|nil | 463 | lua_rawget(L, 1); // fifos key [val [, ...]] fifo|nil |
464 | keeper_fifo* fifo{ keeper_fifo::getPtr(L, -1) }; | 464 | keeper_fifo* fifo{ keeper_fifo::getPtr(L, -1) }; |
465 | if( fifo == nullptr) // can be nullptr if we store a value at a new key | 465 | if (fifo == nullptr) // can be nullptr if we store a value at a new key |
466 | { // fifos key [val [, ...]] nil | 466 | { // fifos key [val [, ...]] nil |
467 | // no need to wake writers in that case, because a writer can't wait on an inexistent key | 467 | // no need to wake writers in that case, because a writer can't wait on an inexistent key |
468 | lua_pop(L, 1); // fifos key [val [, ...]] | 468 | lua_pop(L, 1); // fifos key [val [, ...]] |
@@ -489,7 +489,7 @@ int keepercall_set(lua_State* L) | |||
489 | return should_wake_writers ? (lua_pushboolean(L, 1), 1) : 0; | 489 | return should_wake_writers ? (lua_pushboolean(L, 1), 1) : 0; |
490 | } | 490 | } |
491 | 491 | ||
492 | // ################################################################################################## | 492 | // ################################################################################################# |
493 | 493 | ||
494 | // in: linda_ud key [count] | 494 | // in: linda_ud key [count] |
495 | // out: at most <count> values | 495 | // out: at most <count> values |
@@ -517,7 +517,7 @@ int keepercall_get(lua_State* L) | |||
517 | return 0; | 517 | return 0; |
518 | } | 518 | } |
519 | 519 | ||
520 | // ################################################################################################## | 520 | // ################################################################################################# |
521 | 521 | ||
522 | // in: linda_ud [, key [, ...]] | 522 | // in: linda_ud [, key [, ...]] |
523 | int keepercall_count(lua_State* L) | 523 | int keepercall_count(lua_State* L) |
@@ -586,9 +586,9 @@ int keepercall_count(lua_State* L) | |||
586 | return 1; | 586 | return 1; |
587 | } | 587 | } |
588 | 588 | ||
589 | //################################################################################### | 589 | // ################################################################################################# |
590 | // Keeper API, accessed from linda methods | 590 | // Keeper API, accessed from linda methods |
591 | //################################################################################### | 591 | // ################################################################################################# |
592 | 592 | ||
593 | /*---=== Keeper states ===--- | 593 | /*---=== Keeper states ===--- |
594 | */ | 594 | */ |
@@ -636,7 +636,7 @@ void close_keepers(Universe* U) | |||
636 | } | 636 | } |
637 | } | 637 | } |
638 | 638 | ||
639 | // ################################################################################################## | 639 | // ################################################################################################# |
640 | 640 | ||
641 | /* | 641 | /* |
642 | * Initialize keeper states | 642 | * Initialize keeper states |
@@ -746,7 +746,7 @@ void init_keepers(Universe* U, lua_State* L) | |||
746 | STACK_CHECK(L, 0); | 746 | STACK_CHECK(L, 0); |
747 | } | 747 | } |
748 | 748 | ||
749 | // ################################################################################################## | 749 | // ################################################################################################# |
750 | 750 | ||
751 | // should be called only when inside a keeper_acquire/keeper_release pair (see Linda::ProtectedCall) | 751 | // should be called only when inside a keeper_acquire/keeper_release pair (see Linda::ProtectedCall) |
752 | Keeper* which_keeper(Keepers* keepers_, uintptr_t magic_) | 752 | Keeper* which_keeper(Keepers* keepers_, uintptr_t magic_) |
@@ -760,7 +760,7 @@ Keeper* which_keeper(Keepers* keepers_, uintptr_t magic_) | |||
760 | return nullptr; | 760 | return nullptr; |
761 | } | 761 | } |
762 | 762 | ||
763 | // ################################################################################################## | 763 | // ################################################################################################# |
764 | 764 | ||
765 | Keeper* keeper_acquire(Keepers* keepers_, uintptr_t magic_) | 765 | Keeper* keeper_acquire(Keepers* keepers_, uintptr_t magic_) |
766 | { | 766 | { |
@@ -784,7 +784,7 @@ Keeper* keeper_acquire(Keepers* keepers_, uintptr_t magic_) | |||
784 | return nullptr; | 784 | return nullptr; |
785 | } | 785 | } |
786 | 786 | ||
787 | // ################################################################################################## | 787 | // ################################################################################################# |
788 | 788 | ||
789 | void keeper_release(Keeper* K) | 789 | void keeper_release(Keeper* K) |
790 | { | 790 | { |
@@ -795,7 +795,7 @@ void keeper_release(Keeper* K) | |||
795 | } | 795 | } |
796 | } | 796 | } |
797 | 797 | ||
798 | // ################################################################################################## | 798 | // ################################################################################################# |
799 | 799 | ||
800 | void keeper_toggle_nil_sentinels(lua_State* L, int val_i_, LookupMode const mode_) | 800 | void keeper_toggle_nil_sentinels(lua_State* L, int val_i_, LookupMode const mode_) |
801 | { | 801 | { |
@@ -821,7 +821,7 @@ void keeper_toggle_nil_sentinels(lua_State* L, int val_i_, LookupMode const mode | |||
821 | } | 821 | } |
822 | } | 822 | } |
823 | 823 | ||
824 | // ################################################################################################## | 824 | // ################################################################################################# |
825 | 825 | ||
826 | /* | 826 | /* |
827 | * Call a function ('func_name') in the keeper state, and pass on the returned | 827 | * Call a function ('func_name') in the keeper state, and pass on the returned |