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/>.
31 make_tube(const char *name
)
35 t
= malloc(sizeof(struct tube
));
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;
61 ms_clear(&t
->waiting
);
69 if (t
->refs
< 1) return twarnx("refs is zero for tube: %s", t
->name
);
72 if (t
->refs
< 1) tube_free(t
);
83 make_and_insert_tube(const char *name
)
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
;
100 tube_find(const char *name
)
105 for (i
= 0; i
< tubes
.used
; i
++) {
107 if (strncmp(t
->name
, name
, MAX_TUBE_NAME_LEN
) == 0) return t
;
113 tube_find_or_make(const char *name
)
115 return tube_find(name
) ? : make_and_insert_tube(name
);