aboutsummaryrefslogtreecommitdiff
path: root/src/keeper.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/keeper.cpp')
-rw-r--r--src/keeper.cpp47
1 files changed, 13 insertions, 34 deletions
diff --git a/src/keeper.cpp b/src/keeper.cpp
index c5bbb9d..c306d58 100644
--- a/src/keeper.cpp
+++ b/src/keeper.cpp
@@ -40,6 +40,7 @@
40#include "keeper.h" 40#include "keeper.h"
41 41
42#include "compat.h" 42#include "compat.h"
43#include "linda.h"
43#include "state.h" 44#include "state.h"
44#include "tools.h" 45#include "tools.h"
45#include "uniquekey.h" 46#include "uniquekey.h"
@@ -207,16 +208,17 @@ static void push_table(lua_State* L, int idx_)
207 208
208// ################################################################################################# 209// #################################################################################################
209 210
210int keeper_push_linda_storage(Universe* U, DestState L, void* ptr_, uintptr_t magic_) 211// only used by linda:dump() and linda:__towatch() for debugging purposes
212int keeper_push_linda_storage(Linda& linda_, DestState L)
211{ 213{
212 Keeper* const K{ which_keeper(U->keepers, magic_) }; 214 Keeper* const K{ linda_.whichKeeper() };
213 SourceState const KL{ K ? K->L : nullptr }; 215 SourceState const KL{ K ? K->L : nullptr };
214 if (KL == nullptr) 216 if (KL == nullptr)
215 return 0; 217 return 0;
216 STACK_GROW(KL, 4); // KEEPER MAIN 218 STACK_GROW(KL, 4); // KEEPER MAIN
217 STACK_CHECK_START_REL(KL, 0); 219 STACK_CHECK_START_REL(KL, 0);
218 FIFOS_KEY.pushValue(KL); // fifos 220 FIFOS_KEY.pushValue(KL); // fifos
219 lua_pushlightuserdata(KL, ptr_); // fifos ud 221 lua_pushlightuserdata(KL, &linda_); // fifos ud
220 lua_rawget(KL, -2); // fifos storage 222 lua_rawget(KL, -2); // fifos storage
221 lua_remove(KL, -2); // storage 223 lua_remove(KL, -2); // storage
222 if (!lua_istable(KL, -1)) 224 if (!lua_istable(KL, -1))
@@ -229,7 +231,7 @@ int keeper_push_linda_storage(Universe* U, DestState L, void* ptr_, uintptr_t ma
229 STACK_GROW(L, 5); 231 STACK_GROW(L, 5);
230 STACK_CHECK_START_REL(L, 0); 232 STACK_CHECK_START_REL(L, 0);
231 lua_newtable(L); // out 233 lua_newtable(L); // out
232 InterCopyContext c{ U, L, KL, {}, {}, {}, LookupMode::FromKeeper, {} }; 234 InterCopyContext c{ linda_.U, L, KL, {}, {}, {}, LookupMode::FromKeeper, {} };
233 lua_pushnil(KL); // storage nil 235 lua_pushnil(KL); // storage nil
234 while (lua_next(KL, -2)) // storage key fifo 236 while (lua_next(KL, -2)) // storage key fifo
235 { 237 {
@@ -748,37 +750,14 @@ void init_keepers(Universe* U, lua_State* L)
748 750
749// ################################################################################################# 751// #################################################################################################
750 752
751// should be called only when inside a keeper_acquire/keeper_release pair (see Linda::ProtectedCall) 753Keeper* Linda::acquireKeeper() const
752Keeper* which_keeper(Keepers* keepers_, uintptr_t magic_)
753{ 754{
754 int const nbKeepers{ keepers_->nb_keepers }; 755 int const nbKeepers{ U->keepers->nb_keepers };
755 if (nbKeepers)
756 {
757 unsigned int i = (unsigned int) ((magic_ >> KEEPER_MAGIC_SHIFT) % nbKeepers);
758 return &keepers_->keeper_array[i];
759 }
760 return nullptr;
761}
762
763// #################################################################################################
764
765Keeper* keeper_acquire(Keepers* keepers_, uintptr_t magic_)
766{
767 int const nbKeepers{ keepers_->nb_keepers };
768 // can be 0 if this happens during main state shutdown (lanes is being GC'ed -> no keepers) 756 // can be 0 if this happens during main state shutdown (lanes is being GC'ed -> no keepers)
769 if (nbKeepers) 757 if (nbKeepers)
770 { 758 {
771 /* 759 Keeper* const K{ &U->keepers->keeper_array[m_keeper_index] };
772 * Any hashing will do that maps pointers to 0..GNbKeepers-1
773 * consistently.
774 *
775 * Pointers are often aligned by 8 or so - ignore the low order bits
776 * have to cast to unsigned long to avoid compilation warnings about loss of data when converting pointer-to-integer
777 */
778 unsigned int i = (unsigned int)((magic_ >> KEEPER_MAGIC_SHIFT) % nbKeepers);
779 Keeper* K = &keepers_->keeper_array[i];
780 K->m_mutex.lock(); 760 K->m_mutex.lock();
781 //++ K->count;
782 return K; 761 return K;
783 } 762 }
784 return nullptr; 763 return nullptr;
@@ -786,12 +765,12 @@ Keeper* keeper_acquire(Keepers* keepers_, uintptr_t magic_)
786 765
787// ################################################################################################# 766// #################################################################################################
788 767
789void keeper_release(Keeper* K) 768void Linda::releaseKeeper(Keeper* K_) const
790{ 769{
791 //-- K->count; 770 if (K_) // can be nullptr if we tried to acquire during shutdown
792 if (K)
793 { 771 {
794 K->m_mutex.unlock(); 772 assert(K_ == &U->keepers->keeper_array[m_keeper_index]);
773 K_->m_mutex.unlock();
795 } 774 }
796} 775}
797 776