privacy-scaling-explorations / mpz

Multi-party computation libraries written in Rust 🦀
182 stars 39 forks source link

feat(mpz-common): Context::blocking #141

Closed sinui0 closed 3 months ago

sinui0 commented 3 months ago

This PR adds the Context::blocking method which supports temporarily moving a context to a separate thread to perform blocking computation. I've also moved the CPU backend shim abstraction into mpz-common.

This method is needed to avoid deadlocks on single threaded machines. The alternative approach is to use an mpsc channel between the worker thread and the calling context, but that doesn't work if single-threaded.

sinui0 commented 3 months ago

What is a single-threaded machine and why do we need to support single-threaded machines? Or do you mean a machine with a single CPU core?

Yes, I'm referring to single-core CPUs, and generally environments without threading. Not to be confused with our logical thread abstraction in mpz-common.

Why doesn't this work?

It does work for CpuBackend::blocking_async but not for CpuBackend::blocking. Hard to point out exactly why, but it leads to a deadlock.


The primary purpose of the Context::blocking method is that it handles moving a context to another thread in multi-threaded case. This isn't otherwise possible because most callers are going to be working with a &mut impl Context which obviously isn't 'static.