flux_t *connector_init (const char *path, int flags) { ctx_t *c = malloc (sizeof (*c)); if (!c) { errno = ENOMEM; goto error; } memset (c, 0, sizeof (*c)); c->magic = CTX_MAGIC; if (!(c->queue = msglist_create ((msglist_free_f)flux_msg_destroy))) goto error; if (!(c->h = flux_handle_create (c, &handle_ops, flags))) goto error; /* Fake out size, rank, tbon-arity attributes for testing. */ if (flux_attr_fake (c->h, "rank", "0", FLUX_ATTRFLAG_IMMUTABLE) < 0 || flux_attr_fake (c->h, "size", "1", FLUX_ATTRFLAG_IMMUTABLE) < 0 || flux_attr_fake (c->h, "tbon-arity", "2", FLUX_ATTRFLAG_IMMUTABLE) < 0) goto error; return c->h; error: if (c) { int saved_errno = errno; op_fini (c); errno = saved_errno; } return NULL; }
flux_t connector_init (const char *path, int flags) { ctx_t *c = xzmalloc (sizeof (*c)); c->magic = CTX_MAGIC; c->rank = 0; if (!(c->queue = msglist_create ((msglist_free_f)flux_msg_destroy))) goto error; c->h = flux_handle_create (c, &handle_ops, flags); /* Fake out flux_size() and flux_rank () for testing. */ c->size = 1; flux_aux_set (c->h, "flux::size", &c->size, NULL); flux_aux_set (c->h, "flux::rank", &c->rank, NULL); return c->h; error: if (c) { int saved_errno = errno; op_fini (c); errno = saved_errno; } return NULL; }
flux_t *flux_handle_create (void *impl, const struct flux_handle_ops *ops, int flags) { flux_t *h = malloc (sizeof (*h)); if (!h) goto nomem; memset (h, 0, sizeof (*h)); h->usecount = 1; h->flags = flags; h->ops = ops; h->impl = impl; if (!(h->tagpool = tagpool_create ())) goto nomem; tagpool_set_grow_cb (h->tagpool, tagpool_grow_notify, h); if (!(h->queue = msglist_create ((msglist_free_f)flux_msg_destroy))) goto nomem; h->pollfd = -1; return h; nomem: flux_handle_destroy (h); errno = ENOMEM; return NULL; }
int main (int argc, char *argv[]) { msglist_t *ml; int e; char *msg; struct pollfd pfd; plan (19); ok ((ml = msglist_create (free)) != NULL, "msglist_create works"); ok ((e = msglist_pollevents (ml)) >= 0 && e == POLLOUT, "msglist_pollevents on empty msglist returns POLLOUT"); ok (msglist_append (ml, xstrdup ("foo")) == 0, "msglist_append 'foo' works"); ok ((e = msglist_pollevents (ml)) >= 0 && e == (POLLOUT | POLLIN), "msglist_pollevents on non-empty msglist returns POLLOUT | POLLIN"); ok (msglist_push (ml, xstrdup ("bar")) == 0, "msglist_push 'bar' works"); ok ((e = msglist_pollevents (ml)) >= 0 && e == (POLLOUT | POLLIN), "msglist_pollevents still returns POLLOUT | POLLIN"); ok ((msg = msglist_pop (ml)) != NULL && !strcmp (msg, "bar"), "msglist_pop returns 'bar'"); ok ((e = msglist_pollevents (ml)) >= 0 && e == (POLLOUT | POLLIN), "msglist_pollevents still returns POLLOUT | POLLIN"); free (msg); ok ((msg = msglist_pop (ml)) != NULL && !strcmp (msg, "foo"), "msglist_pop returns 'foo'"); ok ((e = msglist_pollevents (ml)) >= 0 && e == POLLOUT, "msglist_pollevents on empty msglist returns POLLOUT"); free (msg); ok ((pfd.fd = msglist_pollfd (ml)) >= 0, "msglist_pollfd works"); pfd.events = POLLIN, pfd.revents = 0, ok (poll (&pfd, 1, 0) == 1 && pfd.revents == POLLIN, "pollfd suggests we read pollevents"); ok ((e = msglist_pollevents (ml)) >= 0 && e == POLLOUT, "msglist_pollevents on empty msglist returns POLLOUT"); pfd.events = POLLIN, pfd.revents = 0, ok (poll (&pfd, 1, 0) == 0, "pollfd is no longer ready"); ok (msglist_push (ml, xstrdup ("foo")) == 0, "msglist_push 'foo' works"); pfd.events = POLLIN, pfd.revents = 0, ok (poll (&pfd, 1, 0) == 1 && pfd.revents == POLLIN, "pollfd suggests we read pollevents"); ok ((e = msglist_pollevents (ml)) >= 0 && e == (POLLOUT | POLLIN), "msglist_pollevents on non-empty msglist returns POLLOUT | POLLIN"); pfd.events = POLLIN, pfd.revents = 0, ok (poll (&pfd, 1, 0) == 0, "pollfd is no longer ready"); ok ((e = msglist_pollevents (ml)) >= 0 && e == (POLLOUT | POLLIN), "msglist_pollevents still returns POLLOUT | POLLIN"); msglist_destroy (ml); done_testing (); return (0); }