| // Licensed to the Apache Software Foundation (ASF) under one |
| // or more contributor license agreements. See the NOTICE file |
| // distributed with this work for additional information |
| // regarding copyright ownership. The ASF licenses this file |
| // to you under the Apache License, Version 2.0 (the |
| // "License"); you may not use this file except in compliance |
| // with the License. You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, |
| // software distributed under the License is distributed on an |
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| // KIND, either express or implied. See the License for the |
| // specific language governing permissions and limitations |
| // under the License.. |
| |
| use core::fmt; |
| use crate::sync::{SgxCondvar, SgxMutex}; |
| |
| /// A barrier enables multiple threads to synchronize the beginning |
| /// of some computation. |
| /// |
| pub struct Barrier { |
| lock: SgxMutex<BarrierState>, |
| cvar: SgxCondvar, |
| num_threads: usize, |
| } |
| |
| // The inner state of a double barrier |
| struct BarrierState { |
| count: usize, |
| generation_id: usize, |
| } |
| |
| /// A `BarrierWaitResult` is returned by [`wait`] when all threads in the [`Barrier`] |
| /// have rendezvoused. |
| /// |
| /// [`wait`]: struct.Barrier.html#method.wait |
| /// [`Barrier`]: struct.Barrier.html |
| /// |
| pub struct BarrierWaitResult(bool); |
| |
| impl fmt::Debug for Barrier { |
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| f.pad("Barrier { .. }") |
| } |
| } |
| |
| impl Barrier { |
| /// Creates a new barrier that can block a given number of threads. |
| /// |
| /// A barrier will block `n`-1 threads which call [`wait`] and then wake up |
| /// all threads at once when the `n`th thread calls [`wait`]. |
| /// |
| /// [`wait`]: #method.wait |
| /// |
| pub fn new(n: usize) -> Barrier { |
| Barrier { |
| lock: SgxMutex::new(BarrierState { count: 0, generation_id: 0 }), |
| cvar: SgxCondvar::new(), |
| num_threads: n, |
| } |
| } |
| |
| /// Blocks the current thread until all threads have rendezvoused here. |
| /// |
| /// Barriers are re-usable after all threads have rendezvoused once, and can |
| /// be used continuously. |
| /// |
| /// A single (arbitrary) thread will receive a [`BarrierWaitResult`] that |
| /// returns `true` from [`is_leader`] when returning from this function, and |
| /// all other threads will receive a result that will return `false` from |
| /// [`is_leader`]. |
| /// |
| /// [`BarrierWaitResult`]: struct.BarrierWaitResult.html |
| /// [`is_leader`]: struct.BarrierWaitResult.html#method.is_leader |
| /// |
| pub fn wait(&self) -> BarrierWaitResult { |
| let mut lock = self.lock.lock().unwrap(); |
| let local_gen = lock.generation_id; |
| lock.count += 1; |
| if lock.count < self.num_threads { |
| // We need a while loop to guard against spurious wakeups. |
| // http://en.wikipedia.org/wiki/Spurious_wakeup |
| while local_gen == lock.generation_id && lock.count < self.num_threads { |
| lock = self.cvar.wait(lock).unwrap(); |
| } |
| BarrierWaitResult(false) |
| } else { |
| lock.count = 0; |
| lock.generation_id = lock.generation_id.wrapping_add(1); |
| self.cvar.notify_all(); |
| BarrierWaitResult(true) |
| } |
| } |
| } |
| |
| impl fmt::Debug for BarrierWaitResult { |
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| f.debug_struct("BarrierWaitResult").field("is_leader", &self.is_leader()).finish() |
| } |
| } |
| |
| impl BarrierWaitResult { |
| /// Returns `true` if this thread from [`wait`] is the "leader thread". |
| /// |
| /// Only one thread will have `true` returned from their result, all other |
| /// threads will have `false` returned. |
| /// |
| /// [`wait`]: struct.Barrier.html#method.wait |
| /// |
| pub fn is_leader(&self) -> bool { |
| self.0 |
| } |
| } |