1 /* job.c - a job in the queue */
9 static unsigned long long int next_id
= 1;
12 allocate_job(int body_size
)
16 j
= malloc(sizeof(struct job
) + body_size
);
17 if (!j
) return twarnx("OOM"), NULL
;
19 j
->state
= JOB_STATE_INVALID
;
20 j
->creation
= time(NULL
);
21 j
->timeout_ct
= j
->release_ct
= j
->bury_ct
= j
->kick_ct
= 0;
22 j
->body_size
= body_size
;
23 j
->next
= j
->prev
= j
; /* not in a linked list */
29 make_job(unsigned int pri
, unsigned int delay
, int body_size
)
33 j
= allocate_job(body_size
);
34 if (!j
) return twarnx("OOM"), NULL
;
44 job_pri_cmp(job a
, job b
)
46 if (a
->pri
== b
->pri
) {
47 /* we can't just subtract because id has too many bits */
48 if (a
->id
> b
->id
) return 1;
49 if (a
->id
< b
->id
) return -1;
52 return a
->pri
- b
->pri
;
56 job_delay_cmp(job a
, job b
)
58 if (a
->deadline
== b
->deadline
) {
59 /* we can't just subtract because id has too many bits */
60 if (a
->id
> b
->id
) return 1;
61 if (a
->id
< b
->id
) return -1;
64 return a
->deadline
- b
->deadline
;
74 n
= malloc(sizeof(struct job
) + j
->body_size
);
75 if (!n
) return twarnx("OOM"), NULL
;
79 n
->body_size
= j
->body_size
;
80 memcpy(n
->body
, j
->body
, j
->body_size
);
88 if (j
->state
== JOB_STATE_READY
) return "ready";
89 if (j
->state
== JOB_STATE_RESERVED
) return "reserved";
90 if (j
->state
== JOB_STATE_BURIED
) return "buried";
91 if (j
->state
== JOB_STATE_DELAY
) return "delay";
96 job_list_any_p(job head
)
98 return head
->next
!= head
|| head
->prev
!= head
;
105 if (!job_list_any_p(j
)) return NULL
; /* not in a doubly-linked list */
107 j
->next
->prev
= j
->prev
;
108 j
->prev
->next
= j
->next
;
110 j
->prev
= j
->next
= j
;
116 job_insert(job head
, job j
)
118 if (job_list_any_p(j
)) return; /* already in a linked list */
120 j
->prev
= head
->prev
;
122 head
->prev
->next
= j
;
126 unsigned long long int