enhance ordq

This commit is contained in:
Rongsong Shen 2025-12-16 17:04:04 +08:00
parent c478ea8516
commit 05abed1964
4 changed files with 18 additions and 3 deletions

View file

@ -16,6 +16,7 @@ int ordq_init(ordq_t* q, ordq_data_killer_t killer, ordq_cmp_fn_t cmp)
q->head = NULL;
q->killer = killer;
q->cmp = cmp;
q->ctx = NULL;
return 0;
}
@ -25,7 +26,7 @@ static int ordq_put_(ordq_t* q, ordq_item_t* it)
ordq_item_t** p = &(q->head);
while ((*p) != NULL) {
if (q->cmp((*p)->key, it->key) > 0) {
if (q->cmp(q->ctx, (*p)->key, it->key) > 0) {
/* we found the location to insert */
break;
}
@ -105,3 +106,14 @@ ordq_item_t* ordq_peek(ordq_t* q)
return it;
}
void ordq_set_ctx(ordq_t* q, void* ctx)
{
if (q == NULL) {
return;
}
mylock_lock(&(q->lock));
q->ctx = ctx;
mylock_unlock(&(q->lock));
}