1 /* job.c - a job in the queue */
3 /* Copyright (C) 2007 Keith Rarick and Philotic Inc.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 static unsigned long long int next_id
= 1;
29 allocate_job(int body_size
)
33 j
= malloc(sizeof(struct job
) + body_size
);
34 if (!j
) return twarnx("OOM"), NULL
;
36 j
->state
= JOB_STATE_INVALID
;
37 j
->creation
= time(NULL
);
38 j
->timeout_ct
= j
->release_ct
= j
->bury_ct
= j
->kick_ct
= 0;
39 j
->body_size
= body_size
;
40 j
->next
= j
->prev
= j
; /* not in a linked list */
47 make_job(unsigned int pri
, unsigned int delay
, unsigned int ttr
, int body_size
,
52 j
= allocate_job(body_size
);
53 if (!j
) return twarnx("OOM"), NULL
;
59 TUBE_ASSIGN(j
->tube
, tube
);
67 if (j
) TUBE_ASSIGN(j
->tube
, NULL
);
72 job_pri_cmp(job a
, job b
)
74 if (a
->pri
== b
->pri
) {
75 /* we can't just subtract because id has too many bits */
76 if (a
->id
> b
->id
) return 1;
77 if (a
->id
< b
->id
) return -1;
80 return a
->pri
- b
->pri
;
84 job_delay_cmp(job a
, job b
)
86 if (a
->deadline
== b
->deadline
) {
87 /* we can't just subtract because id has too many bits */
88 if (a
->id
> b
->id
) return 1;
89 if (a
->id
< b
->id
) return -1;
92 return a
->deadline
- b
->deadline
;
102 n
= malloc(sizeof(struct job
) + j
->body_size
);
103 if (!n
) return twarnx("OOM"), NULL
;
105 memcpy(n
, j
, sizeof(struct job
) + j
->body_size
);
106 n
->next
= n
->prev
= n
; /* not in a linked list */
107 TUBE_ASSIGN(n
->tube
, j
->tube
);
115 if (j
->state
== JOB_STATE_READY
) return "ready";
116 if (j
->state
== JOB_STATE_RESERVED
) return "reserved";
117 if (j
->state
== JOB_STATE_BURIED
) return "buried";
118 if (j
->state
== JOB_STATE_DELAYED
) return "delayed";
123 job_list_any_p(job head
)
125 return head
->next
!= head
|| head
->prev
!= head
;
132 if (!job_list_any_p(j
)) return NULL
; /* not in a doubly-linked list */
134 j
->next
->prev
= j
->prev
;
135 j
->prev
->next
= j
->next
;
137 j
->prev
= j
->next
= j
;
143 job_insert(job head
, job j
)
145 if (job_list_any_p(j
)) return; /* already in a linked list */
147 j
->prev
= head
->prev
;
149 head
->prev
->next
= j
;
153 unsigned long long int