merge job rusage and pending reason support
This commit is contained in:
parent
30824ae5ea
commit
cb0c7fe9e7
82 changed files with 4280 additions and 2561 deletions
107
src/lib/ordq.c
Normal file
107
src/lib/ordq.c
Normal file
|
|
@ -0,0 +1,107 @@
|
|||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <strings.h>
|
||||
|
||||
#include "mylock.h"
|
||||
#include "ordq.h"
|
||||
|
||||
int ordq_init(ordq_t* q, ordq_data_killer_t killer, ordq_cmp_fn_t cmp)
|
||||
{
|
||||
if ((q == NULL) || (cmp == NULL)) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
mylock_init(&(q->lock));
|
||||
q->head = NULL;
|
||||
q->killer = killer;
|
||||
q->cmp = cmp;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
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) {
|
||||
/* we found the location to insert */
|
||||
break;
|
||||
}
|
||||
|
||||
p = &((*p)->next);
|
||||
}
|
||||
|
||||
it->next = *p;
|
||||
*p = it;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ordq_put(ordq_t* q, ordkey_t key, void* data)
|
||||
{
|
||||
int rc = 0;
|
||||
ordq_item_t* it;
|
||||
|
||||
it = (ordq_item_t*)calloc(1, sizeof(*it));
|
||||
if (it == NULL) {
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
it->key = key;
|
||||
it->data = data;
|
||||
|
||||
mylock_lock(&(q->lock));
|
||||
rc = ordq_put_(q, it);
|
||||
mylock_unlock(&(q->lock));
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
void ordq_fini(ordq_t* q)
|
||||
{
|
||||
ordq_item_t* p = q->head;
|
||||
|
||||
while (p != NULL) {
|
||||
ordq_item_t* p1 = p->next;
|
||||
|
||||
if (p->data != NULL) {
|
||||
q->killer(p->data);
|
||||
}
|
||||
|
||||
p->data = NULL;
|
||||
free(p);
|
||||
|
||||
p = p1;
|
||||
}
|
||||
|
||||
mylock_fini(&(q->lock));
|
||||
}
|
||||
|
||||
ordq_item_t* ordq_get(ordq_t* q)
|
||||
{
|
||||
ordq_item_t* it;
|
||||
|
||||
mylock_lock(&(q->lock));
|
||||
|
||||
it = q->head;
|
||||
if (it != NULL) {
|
||||
q->head = it->next;
|
||||
}
|
||||
|
||||
mylock_unlock(&(q->lock));
|
||||
|
||||
return it;
|
||||
}
|
||||
|
||||
ordq_item_t* ordq_peek(ordq_t* q)
|
||||
{
|
||||
ordq_item_t* it = NULL;
|
||||
|
||||
mylock_lock(&(q->lock));
|
||||
it = q->head;
|
||||
mylock_unlock(&(q->lock));
|
||||
|
||||
return it;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue