Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions crates/tinymemory-integrations/src/cortex/testing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,13 @@ pub(crate) type Shared = Arc<Double>;

/// Decrements `counter` if positive; whether a unit was taken.
pub(crate) fn take_one(counter: &AtomicUsize) -> bool {
counter
// `fetch_update` is deprecated for `try_update` on newer toolchains, which
// is not stable on this crate's MSRV.
#[allow(deprecated)]
let taken = counter
.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |n| n.checked_sub(1))
.is_ok()
.is_ok();
taken
}

impl Double {
Expand Down
8 changes: 5 additions & 3 deletions crates/tinymemory-tools/src/deletion/mod_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,16 @@ impl ListStore<PendingDeletion> for &Flaky {
}
fn save(&self, all: &[PendingDeletion]) -> Result<(), crate::list_store::StoreError> {
let failing = &self.failing_saves;
if failing
// `fetch_update` is deprecated for `try_update` on newer toolchains, which is not stable on this crate's MSRV.
#[allow(deprecated)]
let taken = failing
.fetch_update(
std::sync::atomic::Ordering::SeqCst,
std::sync::atomic::Ordering::SeqCst,
|left| left.checked_sub(1),
)
.is_ok()
{
.is_ok();
if taken {
return Err(crate::list_store::StoreError("full".into()));
}
self.saves.lock().unwrap().push(all.to_vec());
Expand Down
26 changes: 18 additions & 8 deletions crates/tinymemory-tools/src/prefetch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,24 @@ impl WorkerPool {
}

fn try_acquire(&self) -> Option<Permit> {
self.free
.fetch_update(Ordering::AcqRel, Ordering::Acquire, |free| {
free.checked_sub(1)
})
.ok()
.map(|_| Permit {
free: self.free.clone(),
})
// A compare_exchange loop rather than `fetch_update`: that method is
// deprecated in favour of `try_update` on newer toolchains, but
// `try_update` is not stable on this crate's MSRV.
let mut free = self.free.load(Ordering::Acquire);
loop {
let next = free.checked_sub(1)?;
match self
.free
.compare_exchange_weak(free, next, Ordering::AcqRel, Ordering::Acquire)
{
Ok(_) => {
return Some(Permit {
free: self.free.clone(),
});
}
Err(actual) => free = actual,
}
}
}

/// Workers not running a refresh.
Expand Down
19 changes: 19 additions & 0 deletions crates/tinymemory-tools/src/prefetch/mod_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,22 @@ fn runtime_shutdown_releases_worker_and_pending_work() {
assert_eq!(pool.available(), 1, "the permit is released");
assert!(cache.completed(&"k").is_none());
}

#[test]
fn concurrent_acquires_never_exceed_the_pool_size() {
let pool = WorkerPool::new(3);
let granted = std::sync::Mutex::new(Vec::new());
std::thread::scope(|scope| {
for _ in 0..16 {
scope.spawn(|| {
if let Some(permit) = pool.try_acquire() {
granted.lock().unwrap().push(permit);
}
});
}
});
assert_eq!(granted.lock().unwrap().len(), 3);
assert_eq!(pool.available(), 0);
granted.lock().unwrap().clear();
assert_eq!(pool.available(), 3);
}
Loading