Merge branch 'at'
[beanstalkd.git] / job.c
blobb92362de9ad7b229a145e024a0f5c19fc1fff421
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 "primes.h"
25 #include "util.h"
27 static unsigned long long int next_id = 1;
29 static int cur_prime = 0;
31 static job *all_jobs=NULL;
32 static size_t all_jobs_cap = 12289; /* == primes[0] */
33 static size_t all_jobs_used = 0;
35 static int hash_table_was_oom = 0;
37 static void rehash();
39 static int
40 _get_job_hash_index(unsigned long long int job_id)
42 return job_id % all_jobs_cap;
45 static void
46 store_job(job j)
48 int index=0;
50 index = _get_job_hash_index(j->id);
52 j->ht_next = all_jobs[index];
53 all_jobs[index] = j;
54 all_jobs_used++;
56 /* accept a load factor of 4 */
57 if (all_jobs_used > (all_jobs_cap << 2)) rehash();
60 static void
61 rehash()
63 job *old = all_jobs;
64 size_t old_cap = all_jobs_cap, old_used = all_jobs_used, i;
66 if (cur_prime >= NUM_PRIMES) return;
67 if (hash_table_was_oom) return;
69 all_jobs_cap = primes[++cur_prime];
70 all_jobs = calloc(all_jobs_cap, sizeof(job));
71 if (!all_jobs) {
72 twarnx("Failed to allocate %d new hash buckets", all_jobs_cap);
73 hash_table_was_oom = 1;
74 --cur_prime;
75 all_jobs = old;
76 all_jobs_cap = old_cap;
77 all_jobs_used = old_used;
78 return;
80 all_jobs_used = 0;
82 for (i = 0; i < old_cap; i++) {
83 while (old[i]) {
84 job j = old[i];
85 old[i] = j->ht_next;
86 j->ht_next = NULL;
87 store_job(j);
92 job
93 job_find(unsigned long long int job_id)
95 job jh = NULL;
96 int index = _get_job_hash_index(job_id);
98 for (jh = all_jobs[index]; jh && jh->id != job_id; jh = jh->ht_next);
100 return jh;
104 allocate_job(int body_size)
106 job j;
108 j = malloc(sizeof(struct job) + body_size);
109 if (!j) return twarnx("OOM"), NULL;
111 j->id = 0;
112 j->state = JOB_STATE_INVALID;
113 j->creation = time(NULL);
114 j->timeout_ct = j->release_ct = j->bury_ct = j->kick_ct = 0;
115 j->body_size = body_size;
116 j->next = j->prev = j; /* not in a linked list */
117 j->ht_next = NULL;
118 j->tube = NULL;
120 return j;
124 make_job(unsigned int pri, unsigned int delay, unsigned int ttr, int body_size,
125 tube tube)
127 job j;
129 j = allocate_job(body_size);
130 if (!j) return twarnx("OOM"), NULL;
132 j->id = next_id++;
133 j->pri = pri;
134 j->delay = delay;
135 j->ttr = ttr;
137 store_job(j);
139 TUBE_ASSIGN(j->tube, tube);
141 return j;
144 static void
145 job_hash_free(job j)
147 int index=_get_job_hash_index(j->id);
148 job jh = all_jobs ? all_jobs[index] : NULL;
150 if (jh) {
151 if (jh == j) {
152 /* Special case the first */
153 all_jobs[index] = jh->ht_next;
154 } else {
155 job tmp;
156 while (jh->ht_next && jh->ht_next != j) jh = jh->ht_next;
157 if (jh->ht_next) {
158 tmp = jh->ht_next;
159 jh->ht_next = jh->ht_next->ht_next;
164 all_jobs_used--;
167 void
168 job_free(job j)
170 if (j) {
171 TUBE_ASSIGN(j->tube, NULL);
172 job_hash_free(j);
175 free(j);
179 job_pri_cmp(job a, job b)
181 if (a->pri == b->pri) {
182 /* we can't just subtract because id has too many bits */
183 if (a->id > b->id) return 1;
184 if (a->id < b->id) return -1;
185 return 0;
187 return a->pri - b->pri;
191 job_delay_cmp(job a, job b)
193 if (a->deadline == b->deadline) {
194 /* we can't just subtract because id has too many bits */
195 if (a->id > b->id) return 1;
196 if (a->id < b->id) return -1;
197 return 0;
199 return a->deadline - b->deadline;
203 job_copy(job j)
205 job n;
207 if (!j) return NULL;
209 n = malloc(sizeof(struct job) + j->body_size);
210 if (!n) return twarnx("OOM"), NULL;
212 memcpy(n, j, sizeof(struct job) + j->body_size);
213 n->next = n->prev = n; /* not in a linked list */
215 n->tube = 0; /* Don't use memcpy for the tube, which we must refcount. */
216 TUBE_ASSIGN(n->tube, j->tube);
218 return n;
221 const char *
222 job_state(job j)
224 if (j->state == JOB_STATE_READY) return "ready";
225 if (j->state == JOB_STATE_RESERVED) return "reserved";
226 if (j->state == JOB_STATE_BURIED) return "buried";
227 if (j->state == JOB_STATE_DELAYED) return "delayed";
228 return "invalid";
232 job_list_any_p(job head)
234 return head->next != head || head->prev != head;
238 job_remove(job j)
240 if (!j) return NULL;
241 if (!job_list_any_p(j)) return NULL; /* not in a doubly-linked list */
243 j->next->prev = j->prev;
244 j->prev->next = j->next;
246 j->prev = j->next = j;
248 return j;
251 void
252 job_insert(job head, job j)
254 if (job_list_any_p(j)) return; /* already in a linked list */
256 j->prev = head->prev;
257 j->next = head;
258 head->prev->next = j;
259 head->prev = j;
262 unsigned long long int
263 total_jobs()
265 return next_id - 1;
268 void
269 job_init()
271 all_jobs = calloc(all_jobs_cap, sizeof(job));
272 if (!all_jobs) {
273 twarnx("Failed to allocate %d hash buckets", all_jobs_cap);