Add an option to display the version string.
[beanstalkd.git] / tube.c
blobc03337707535923f040043eb543fcd9f0ca5aaff
1 /* tube.c - tubes implementation */
3 /* Copyright (C) 2008 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 "stat.h"
23 #include "tube.h"
24 #include "prot.h"
25 #include "util.h"
27 tube
28 make_tube(const char *name)
30 tube t;
32 t = malloc(sizeof(struct tube));
33 if (!t) return NULL;
35 t->refs = 0;
37 t->name[MAX_TUBE_NAME_LEN - 1] = '\0';
38 strncpy(t->name, name, MAX_TUBE_NAME_LEN - 1);
39 if (t->name[MAX_TUBE_NAME_LEN - 1] != '\0') twarnx("truncating tube name");
41 pq_init(&t->ready, job_pri_cmp);
42 pq_init(&t->delay, job_delay_cmp);
43 t->buried = (struct job) { &t->buried, &t->buried, 0 };
44 ms_init(&t->waiting, NULL, NULL);
46 t->stat = (struct stats) {0, 0, 0, 0};
47 t->using_ct = t->watching_ct = 0;
49 return t;
52 static void
53 tube_free(tube t)
55 prot_remove_tube(t);
56 pq_clear(&t->ready);
57 ms_clear(&t->waiting);
58 free(t);
61 void
62 tube_dref(tube t)
64 if (!t) return;
65 if (t->refs < 1) return twarnx("refs is zero for tube: %s", t->name);
67 --t->refs;
68 if (t->refs < 1) tube_free(t);
71 void
72 tube_iref(tube t)
74 if (!t) return;
75 ++t->refs;