add changelog for 1.13
[beanstalkd.git] / tube.c
blob2f9f8abbccc0181f411d326ae632c173bc1797d2
1 #include "dat.h"
2 #include <stdint.h>
3 #include <stdlib.h>
4 #include <string.h>
6 struct Ms tubes;
8 Tube *
9 make_tube(const char *name)
11 Tube *t = new(Tube);
12 if (!t)
13 return NULL;
15 strncpy(t->name, name, MAX_TUBE_NAME_LEN);
16 if (t->name[MAX_TUBE_NAME_LEN - 1] != '\0') {
17 t->name[MAX_TUBE_NAME_LEN - 1] = '\0';
18 twarnx("truncating tube name");
21 t->ready.less = job_pri_less;
22 t->delay.less = job_delay_less;
23 t->ready.setpos = job_setpos;
24 t->delay.setpos = job_setpos;
26 Job j = {.tube = NULL};
27 t->buried = j;
28 t->buried.prev = t->buried.next = &t->buried;
29 ms_init(&t->waiting_conns, NULL, NULL);
31 return t;
34 static void
35 tube_free(Tube *t)
37 ms_remove(&tubes, t);
38 free(t->ready.data);
39 free(t->delay.data);
40 ms_clear(&t->waiting_conns);
41 free(t);
44 void
45 tube_dref(Tube *t)
47 if (!t) return;
48 if (t->refs < 1) {
49 twarnx("refs is zero for tube: %s", t->name);
50 return;
53 --t->refs;
54 if (t->refs < 1)
55 tube_free(t);
58 void
59 tube_iref(Tube *t)
61 if (!t) return;
62 ++t->refs;
65 static Tube *
66 make_and_insert_tube(const char *name)
68 int r;
69 Tube *t = NULL;
71 t = make_tube(name);
72 if (!t)
73 return NULL;
75 /* We want this global tube list to behave like "weak" refs, so don't
76 * increment the ref count. */
77 r = ms_append(&tubes, t);
78 if (!r)
79 return tube_dref(t), (Tube *) 0;
81 return t;
84 Tube *
85 tube_find(Ms *tubeset, const char *name)
87 size_t i;
89 for (i = 0; i < tubeset->len; i++) {
90 Tube *t = tubeset->items[i];
91 if (strncmp(t->name, name, MAX_TUBE_NAME_LEN) == 0)
92 return t;
94 return NULL;
97 Tube *
98 tube_find_or_make(const char *name)
100 Tube *t = tube_find(&tubes, name);
101 if (t)
102 return t;
103 return make_and_insert_tube(name);