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;
60 ms_clear(&t
->waiting
);
68 if (t
->refs
< 1) return twarnx("refs is zero for tube: %s", t
->name
);
71 if (t
->refs
< 1) tube_free(t
);
82 make_and_insert_tube(const char *name
)
90 /* We want this global tube list to behave like "weak" refs, so don't
91 * increment the ref count. */
92 r
= ms_append(&tubes
, t
);
93 if (!r
) return tube_dref(t
), NULL
;
99 tube_find(const char *name
)
104 for (i
= 0; i
< tubes
.used
; i
++) {
106 if (strncmp(t
->name
, name
, MAX_TUBE_NAME_LEN
) == 0) return t
;
112 tube_find_or_make(const char *name
)
114 return tube_find(name
) ? : make_and_insert_tube(name
);