Revert "Disable binlog for the next release."
[beanstalkd.git] / tube.c
blob421132f5b6f268b1deda129ce7072ab539abb6bc
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 "ms.h"
23 #include "stat.h"
24 #include "tube.h"
25 #include "prot.h"
26 #include "util.h"
28 struct ms tubes;
30 tube
31 make_tube(const char *name)
33 tube t;
35 t = malloc(sizeof(struct tube));
36 if (!t) return NULL;
38 t->refs = 0;
40 t->name[MAX_TUBE_NAME_LEN - 1] = '\0';
41 strncpy(t->name, name, MAX_TUBE_NAME_LEN - 1);
42 if (t->name[MAX_TUBE_NAME_LEN - 1] != '\0') twarnx("truncating tube name");
44 pq_init(&t->ready, job_pri_cmp);
45 pq_init(&t->delay, job_delay_cmp);
46 t->buried = (struct job) { &t->buried, &t->buried, 0 };
47 ms_init(&t->waiting, NULL, NULL);
49 t->stat = (struct stats) {0, 0, 0, 0};
50 t->using_ct = t->watching_ct = 0;
52 return t;
55 static void
56 tube_free(tube t)
58 prot_remove_tube(t);
59 pq_clear(&t->ready);
60 pq_clear(&t->delay);
61 ms_clear(&t->waiting);
62 free(t);
65 void
66 tube_dref(tube t)
68 if (!t) return;
69 if (t->refs < 1) return twarnx("refs is zero for tube: %s", t->name);
71 --t->refs;
72 if (t->refs < 1) tube_free(t);
75 void
76 tube_iref(tube t)
78 if (!t) return;
79 ++t->refs;
82 static tube
83 make_and_insert_tube(const char *name)
85 int r;
86 tube t = NULL;
88 t = make_tube(name);
89 if (!t) return NULL;
91 /* We want this global tube list to behave like "weak" refs, so don't
92 * increment the ref count. */
93 r = ms_append(&tubes, t);
94 if (!r) return tube_dref(t), NULL;
96 return t;
99 tube
100 tube_find(const char *name)
102 tube t;
103 size_t i;
105 for (i = 0; i < tubes.used; i++) {
106 t = tubes.items[i];
107 if (strncmp(t->name, name, MAX_TUBE_NAME_LEN) == 0) return t;
109 return NULL;
112 tube
113 tube_find_or_make(const char *name)
115 return tube_find(name) ? : make_and_insert_tube(name);