(self, f: F)
| 951 | /// has been granted membership on the thread-pool. |
| 952 | pub(crate) member_index: usize, |
| 953 | /// A reference to the `MemberData` table. |
| 954 | pub(crate) member_data: &'static MemberData, |
| 955 | } |
| 956 | |
| 957 | impl ThreadPool { |
| 958 | /// Returns member data, initializing it on the first call. |
| 959 | pub(crate) fn get_member_data(&'static self) -> &'static MemberData { |
| 960 | self.member_data.get() |
| 961 | } |
| 962 | |
| 963 | /// Waits for membership in the thread-pool. |
| 964 | /// |
| 965 | /// If the thread-pool is full (it has 32 members) this blocks. |
| 966 | pub fn enroll(&'static self) -> Membership { |
| 967 | loop { |
| 968 | match self.try_enroll() { |
| 969 | // If we receive a membership, break out of the loop |
| 970 | Some(membership) => return membership, |
| 971 | // If the thread-pool is full, wait for a membership to become |
| 972 | // free |
| 973 | None => atomic_wait::wait(&self.claimed_bitmask, u32::MAX), |
| 974 | } |
| 975 | } |
| 976 | } |
| 977 | |
| 978 | /// Requests membership in a thread-pool. |
| 979 | /// |
| 980 | /// If the thread-pool is full (it has 32 members) this returns `None`. |
| 981 | #[cold] |
| 982 | pub fn try_enroll(&'static self) -> Option<Membership> { |
| 983 | loop { |
| 984 | // This is only used to select a candidate seat; the seat may in |
| 985 | // fact be claimed by a concurrent call to `try_enroll` or |
| 986 | // `try_enroll_many`. |
| 987 | let available_bitmask = |
| 988 | !self.claimed_bitmask.load(Ordering::Relaxed); |
| 989 | if available_bitmask == 0 { |
| 990 | return None; |
| 991 | } |
| 992 | |
| 993 | // Select the first free seat. |
| 994 | let enrolled_index = available_bitmask.trailing_zeros() as usize; // TZCNT |
| 995 | let enrolled_bitmask = 1 << enrolled_index; |
| 996 | |
| 997 | // This RMW tries to atomically take ownership of the seat. |
| 998 | // |
| 999 | // We use `Acquire` ordering here to synchronize with the `Release` |
| 1000 | // RMW any previous owner would make during resignation. If the |
| 1001 | // returned bits show the seat was already taken, we lost a race and |
| 1002 | // retry. |
| 1003 | if self |
| 1004 | .claimed_bitmask |
| 1005 | .fetch_or(enrolled_bitmask, Ordering::Acquire) |
| 1006 | & enrolled_bitmask |
| 1007 | == 0 |
| 1008 | { |
| 1009 | return Some(Membership { |
| 1010 | thread_pool: self, |
no test coverage detected