Skip to content

Commit

Permalink
comments
Browse files Browse the repository at this point in the history
  • Loading branch information
bmisiak committed Feb 21, 2024
1 parent 6f93349 commit a7e6cae
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,24 @@ mod amx_arguments;
mod timer;

thread_local! {
/// A slotmap of timers. Stable keys.
static TIMERS: RefCell<Slab<Timer>> = RefCell::new(Slab::with_capacity(1000));
/// Always sorted queue of timers. Easy O(1) peeking and popping of the next scheduled timer.
static QUEUE: RefCell<PriorityQueue<usize, Reverse<TimerScheduling>, fnv::FnvBuildHasher>> = RefCell::new(PriorityQueue::with_capacity_and_default_hasher(1000));
}

/// The plugin
struct PreciseTimers;

/// A struct defining when a timer gets triggered
#[derive(Clone)]
struct TimerScheduling {
/// If Some, it's a repeating timer.
/// If None, it will be gone after the next trigger.
interval: Option<Duration>,
/// If true, the timer is marked for deletion
execution_forbidden: bool,
/// The timer will be executed after this instant passes
next_trigger: Instant,
}

Expand Down Expand Up @@ -200,9 +207,7 @@ impl SampPlugin for PreciseTimers {
// Must pop before the timer is executed, so that
// the callback can't schedule anything as the very next timer before
// we have a chance to pop from the queue.
let (popped_key, _) = QUEUE
.with_borrow_mut(|q| q.pop())
.expect("priority queue should have at least the timer we peeked");
let (popped_key, _) = QUEUE.with_borrow_mut(|q| q.pop().expect("peeked timer gone from queue"));
assert_eq!(popped_key, key);
// Remove from slab
let timer = TIMERS.with_borrow_mut(|t| t.remove(key));
Expand All @@ -228,11 +233,13 @@ impl SampPlugin for PreciseTimers {
} else {
true
}
})
});
});
QUEUE.with_borrow_mut(|q| {
for key in removed_timers {
q.remove(&key);
}
});
for key in removed_timers {
QUEUE.with_borrow_mut(|q| q.remove(&key));
}
}
}

Expand Down

0 comments on commit a7e6cae

Please sign in to comment.