Fallback for Solaris.
[beanstalkd.git] / tube.c
blob9185475da8f69662ab30def79579d957c5d34383
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) { };
47 t->buried.prev = t->buried.next = &t->buried;
48 ms_init(&t->waiting, NULL, NULL);
50 t->stat = (struct stats) {0, 0, 0, 0};
51 t->using_ct = t->watching_ct = 0;
53 return t;
56 static void
57 tube_free(tube t)
59 prot_remove_tube(t);
60 pq_clear(&t->ready);
61 pq_clear(&t->delay);
62 ms_clear(&t->waiting);
63 free(t);
66 void
67 tube_dref(tube t)
69 if (!t) return;
70 if (t->refs < 1) return twarnx("refs is zero for tube: %s", t->name);
72 --t->refs;
73 if (t->refs < 1) tube_free(t);
76 void
77 tube_iref(tube t)
79 if (!t) return;
80 ++t->refs;
83 static tube
84 make_and_insert_tube(const char *name)
86 int r;
87 tube t = NULL;
89 t = make_tube(name);
90 if (!t) return NULL;
92 /* We want this global tube list to behave like "weak" refs, so don't
93 * increment the ref count. */
94 r = ms_append(&tubes, t);
95 if (!r) return tube_dref(t), (tube) 0;
97 return t;
100 tube
101 tube_find(const char *name)
103 tube t;
104 size_t i;
106 for (i = 0; i < tubes.used; i++) {
107 t = tubes.items[i];
108 if (strncmp(t->name, name, MAX_TUBE_NAME_LEN) == 0) return t;
110 return NULL;
113 tube
114 tube_find_or_make(const char *name)
116 return tube_find(name) ? : make_and_insert_tube(name);