Refactoring.
[beanstalkd.git] / job.c
blob47b5ca8716ea96bee39f6376ab060175ee30507a
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/>.
19 #include <stdlib.h>
20 #include <string.h>
22 #include "tube.h"
23 #include "job.h"
24 #include "util.h"
26 static unsigned long long int next_id = 1;
28 job
29 allocate_job(int body_size)
31 job j;
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 */
41 j->tube = NULL;
43 return j;
46 job
47 make_job(unsigned int pri, unsigned int delay, unsigned int ttr, int body_size,
48 tube tube)
50 job j;
52 j = allocate_job(body_size);
53 if (!j) return twarnx("OOM"), NULL;
55 j->id = next_id++;
56 j->pri = pri;
57 j->delay = delay;
58 j->ttr = ttr;
59 TUBE_ASSIGN(j->tube, tube);
61 return j;
64 void
65 job_free(job j)
67 if (j) TUBE_ASSIGN(j->tube, NULL);
68 free(j);
71 int
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;
78 return 0;
80 return a->pri - b->pri;
83 int
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;
90 return 0;
92 return a->deadline - b->deadline;
95 job
96 job_copy(job j)
98 job n;
100 if (!j) return NULL;
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);
109 return n;
112 const char *
113 job_state(job j)
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";
119 return "invalid";
123 job_list_any_p(job head)
125 return head->next != head || head->prev != head;
129 job_remove(job j)
131 if (!j) return NULL;
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;
139 return j;
142 void
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;
148 j->next = head;
149 head->prev->next = j;
150 head->prev = j;
153 unsigned long long int
154 total_jobs()
156 return next_id - 1;