Skip to content

Commit

Permalink
feat(mpz-garble): add getters for thread in pool (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
sinui0 authored Mar 11, 2024
1 parent 5d313cb commit 9f7403b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
8 changes: 8 additions & 0 deletions garble/mpz-garble/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ pub enum VmError {
VerifyError(#[from] VerifyError),
#[error(transparent)]
DecodeError(#[from] DecodeError),
#[error("thread pools must have at least one thread")]
ThreadPoolEmpty,
#[error("thread already exists: {0}")]
ThreadAlreadyExists(String),
#[error("vm is shutdown")]
Expand Down Expand Up @@ -161,11 +163,17 @@ pub trait Vm {
async fn new_thread(&mut self, id: &str) -> Result<Self::Thread, VmError>;

/// Creates a new thread pool.
///
/// A thread pool must have at least one thread.
async fn new_thread_pool(
&mut self,
id: &str,
thread_count: usize,
) -> Result<ThreadPool<Self::Thread>, VmError> {
if thread_count == 0 {
return Err(VmError::ThreadPoolEmpty);
}

let mut id = NestedId::new(id).append_counter();
let mut threads = Vec::with_capacity(thread_count);
for _ in 0..thread_count {
Expand Down
18 changes: 18 additions & 0 deletions garble/mpz-garble/src/threadpool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,28 @@ where
T: Thread + 'static,
{
/// Creates a new thread pool.
///
/// # Panics
///
/// Panics if the number of threads is zero.
pub(crate) fn new(threads: Vec<T>) -> Self {
assert!(
!threads.is_empty(),
"thread pool must have at least one thread"
);
Self { threads }
}

/// Returns a reference to the first thread in the pool.
pub fn get(&self) -> &T {
&self.threads[0]
}

/// Returns a mutable reference to the first thread in the pool.
pub fn get_mut(&mut self) -> &mut T {
&mut self.threads[0]
}

/// Returns a new thread pool scope.
pub fn new_scope<R>(&mut self) -> Scope<'_, T, R> {
Scope {
Expand Down

0 comments on commit 9f7403b

Please sign in to comment.