Skip to content

Alternative to pattern which would need double mutable borrow? #217

Answered by jerome-trc
jerome-trc asked this question in Q&A
Discussion options

You must be logged in to vote

I think I figured out what I needed:

  • Initialize closures for script's API in advance using Arc<Mutex<>>.
  • Lock a mutex around all game state.
  • Collect all indices for something that needs to be iterator over into a pre-reserved Vec.
  • Iterate over those indices. If Lua needs to run a script for something, drop the mutex guard.
  • Lua-owned closure acquires mutex guard, runs some Rust code, drops the guard.
  • Overlying loop re-acquires the guard and continues.
    It's not ideal performance-wise - I was hoping to be able to avoid running a lock for every native call in what could potentially be thousands of native scripts, and I'd prefer not to have to do so much iterator-collecting - but parking_lot'…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by jerome-trc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants