Cancellable timers: sorted_list::remove, timer cancel, replace done flag with timer cancellation

This commit is contained in:
Ian Gulliver
2026-04-11 08:28:32 +09:00
parent 3a3c5873c3
commit e486f6501a
5 changed files with 48 additions and 18 deletions

View File

@@ -28,8 +28,8 @@ struct sorted_list {
T& front() { return head->value(); }
const T& front() const { return head->value(); }
void insert(T value) {
if (full()) return;
node* insert(T value) {
if (full()) return nullptr;
node* n = free_head;
free_head = n->next;
new (n->storage) T(std::move(value));
@@ -37,7 +37,7 @@ struct sorted_list {
if (!head || n->value() < head->value()) {
n->next = head;
head = n;
return;
return n;
}
node* cur = head;
@@ -45,6 +45,7 @@ struct sorted_list {
cur = cur->next;
n->next = cur->next;
cur->next = n;
return n;
}
void pop_front() {
@@ -55,4 +56,21 @@ struct sorted_list {
n->next = free_head;
free_head = n;
}
bool remove(node* target) {
if (!target || empty()) return false;
if (head == target) {
pop_front();
return true;
}
node* cur = head;
while (cur->next && cur->next != target)
cur = cur->next;
if (cur->next != target) return false;
cur->next = target->next;
target->value().~T();
target->next = free_head;
free_head = target;
return true;
}
};