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/>.
27 static uint64_t 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;
40 _get_job_hash_index(uint64_t job_id
)
42 return job_id
% all_jobs_cap
;
50 index
= _get_job_hash_index(j
->id
);
52 j
->ht_next
= all_jobs
[index
];
56 /* accept a load factor of 4 */
57 if (all_jobs_used
> (all_jobs_cap
<< 2)) rehash();
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
));
72 twarnx("Failed to allocate %d new hash buckets", all_jobs_cap
);
73 hash_table_was_oom
= 1;
76 all_jobs_cap
= old_cap
;
77 all_jobs_used
= old_used
;
82 for (i
= 0; i
< old_cap
; i
++) {
93 job_find(uint64_t job_id
)
96 int index
= _get_job_hash_index(job_id
);
98 for (jh
= all_jobs
[index
]; jh
&& jh
->id
!= job_id
; jh
= jh
->ht_next
);
104 allocate_job(int body_size
)
108 j
= malloc(sizeof(struct job
) + body_size
);
109 if (!j
) return twarnx("OOM"), (job
) 0;
112 j
->state
= JOB_STATE_INVALID
;
113 j
->created_at
= now_usec();
114 j
->reserve_ct
= 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 */
121 j
->reserved_binlog_space
= 0;
127 make_job_with_id(unsigned int pri
, usec delay
, usec ttr
,
128 int body_size
, tube tube
, uint64_t id
)
132 j
= allocate_job(body_size
);
133 if (!j
) return twarnx("OOM"), (job
) 0;
137 if (id
>= next_id
) next_id
= id
+ 1;
147 TUBE_ASSIGN(j
->tube
, tube
);
157 slot
= &all_jobs
[_get_job_hash_index(j
->id
)];
158 while (*slot
&& *slot
!= j
) slot
= &(*slot
)->ht_next
;
160 *slot
= (*slot
)->ht_next
;
169 TUBE_ASSIGN(j
->tube
, NULL
);
170 if (j
->state
!= JOB_STATE_COPY
) job_hash_free(j
);
176 /* We can't substrct any of these values because there are too many bits */
178 job_pri_cmp(job a
, job b
)
180 if (a
->pri
> b
->pri
) return 1;
181 if (a
->pri
< b
->pri
) return -1;
182 if (a
->id
> b
->id
) return 1;
183 if (a
->id
< b
->id
) return -1;
187 /* We can't substrct any of these values because there are too many bits */
189 job_delay_cmp(job a
, job b
)
191 if (a
->deadline_at
> b
->deadline_at
) return 1;
192 if (a
->deadline_at
< b
->deadline_at
) return -1;
193 if (a
->id
> b
->id
) return 1;
194 if (a
->id
< b
->id
) return -1;
205 n
= malloc(sizeof(struct job
) + j
->body_size
);
206 if (!n
) return twarnx("OOM"), (job
) 0;
208 memcpy(n
, j
, sizeof(struct job
) + j
->body_size
);
209 n
->next
= n
->prev
= n
; /* not in a linked list */
211 n
->binlog
= NULL
; /* copies do not have refcnt on the binlog */
213 n
->tube
= 0; /* Don't use memcpy for the tube, which we must refcount. */
214 TUBE_ASSIGN(n
->tube
, j
->tube
);
216 /* Mark this job as a copy so it can be appropriately freed later on */
217 n
->state
= JOB_STATE_COPY
;
225 if (j
->state
== JOB_STATE_READY
) return "ready";
226 if (j
->state
== JOB_STATE_RESERVED
) return "reserved";
227 if (j
->state
== JOB_STATE_BURIED
) return "buried";
228 if (j
->state
== JOB_STATE_DELAYED
) return "delayed";
233 job_list_any_p(job head
)
235 return head
->next
!= head
|| head
->prev
!= head
;
242 if (!job_list_any_p(j
)) return NULL
; /* not in a doubly-linked list */
244 j
->next
->prev
= j
->prev
;
245 j
->prev
->next
= j
->next
;
247 j
->prev
= j
->next
= j
;
253 job_insert(job head
, job j
)
255 if (job_list_any_p(j
)) return; /* already in a linked list */
257 j
->prev
= head
->prev
;
259 head
->prev
->next
= j
;
273 return all_jobs_used
;
279 all_jobs
= calloc(all_jobs_cap
, sizeof(job
));
281 twarnx("Failed to allocate %d hash buckets", all_jobs_cap
);