forked from beanstalkd/beanstalkd
/
job.c
146 lines (115 loc) · 3.06 KB
/
job.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* job.c - a job in the queue */
/* Copyright (C) 2007 Keith Rarick and Philotic Inc.
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <string.h>
#include "job.h"
#include "util.h"
static unsigned long long int next_id = 1;
job
allocate_job(int body_size)
{
job j;
j = malloc(sizeof(struct job) + body_size);
if (!j) return twarnx("OOM"), NULL;
j->state = JOB_STATE_INVALID;
j->creation = time(NULL);
j->timeout_ct = j->release_ct = j->bury_ct = j->kick_ct = 0;
j->body_size = body_size;
j->next = j->prev = j; /* not in a linked list */
return j;
}
job
make_job(unsigned int pri, unsigned int delay, int body_size)
{
job j;
j = allocate_job(body_size);
if (!j) return twarnx("OOM"), NULL;
j->id = next_id++;
j->pri = pri;
j->delay = delay;
return j;
}
int
job_pri_cmp(job a, job b)
{
if (a->pri == b->pri) {
/* we can't just subtract because id has too many bits */
if (a->id > b->id) return 1;
if (a->id < b->id) return -1;
return 0;
}
return a->pri - b->pri;
}
int
job_delay_cmp(job a, job b)
{
if (a->deadline == b->deadline) {
/* we can't just subtract because id has too many bits */
if (a->id > b->id) return 1;
if (a->id < b->id) return -1;
return 0;
}
return a->deadline - b->deadline;
}
job
job_copy(job j)
{
job n;
if (!j) return NULL;
n = malloc(sizeof(struct job) + j->body_size);
if (!n) return twarnx("OOM"), NULL;
n->id = j->id;
n->pri = j->pri;
n->body_size = j->body_size;
memcpy(n->body, j->body, j->body_size);
return n;
}
const char *
job_state(job j)
{
if (j->state == JOB_STATE_READY) return "ready";
if (j->state == JOB_STATE_RESERVED) return "reserved";
if (j->state == JOB_STATE_BURIED) return "buried";
if (j->state == JOB_STATE_DELAYED) return "delayed";
return "invalid";
}
int
job_list_any_p(job head)
{
return head->next != head || head->prev != head;
}
job
job_remove(job j)
{
if (!j) return NULL;
if (!job_list_any_p(j)) return NULL; /* not in a doubly-linked list */
j->next->prev = j->prev;
j->prev->next = j->next;
j->prev = j->next = j;
return j;
}
void
job_insert(job head, job j)
{
if (job_list_any_p(j)) return; /* already in a linked list */
j->prev = head->prev;
j->next = head;
head->prev->next = j;
head->prev = j;
}
unsigned long long int
total_jobs()
{
return next_id - 1;
}