version 1.15, 2016/10/16 17:55:14 |
version 1.16, 2016/10/16 19:04:05 |
|
|
* Block or wake a client on a named wait channel. |
* Block or wake a client on a named wait channel. |
*/ |
*/ |
|
|
static enum cmd_retval cmd_wait_for_exec(struct cmd *, struct cmd_q *); |
static enum cmd_retval cmd_wait_for_exec(struct cmd *, struct cmdq_item *); |
|
|
const struct cmd_entry cmd_wait_for_entry = { |
const struct cmd_entry cmd_wait_for_entry = { |
.name = "wait-for", |
.name = "wait-for", |
|
|
}; |
}; |
|
|
struct wait_item { |
struct wait_item { |
struct cmd_q *cmdq; |
struct cmdq_item *item; |
TAILQ_ENTRY(wait_item) entry; |
TAILQ_ENTRY(wait_item) entry; |
}; |
}; |
|
|
|
|
return (strcmp(wc1->name, wc2->name)); |
return (strcmp(wc1->name, wc2->name)); |
} |
} |
|
|
static enum cmd_retval cmd_wait_for_signal(struct cmd_q *, const char *, |
static enum cmd_retval cmd_wait_for_signal(struct cmdq_item *, const char *, |
struct wait_channel *); |
struct wait_channel *); |
static enum cmd_retval cmd_wait_for_wait(struct cmd_q *, const char *, |
static enum cmd_retval cmd_wait_for_wait(struct cmdq_item *, const char *, |
struct wait_channel *); |
struct wait_channel *); |
static enum cmd_retval cmd_wait_for_lock(struct cmd_q *, const char *, |
static enum cmd_retval cmd_wait_for_lock(struct cmdq_item *, const char *, |
struct wait_channel *); |
struct wait_channel *); |
static enum cmd_retval cmd_wait_for_unlock(struct cmd_q *, const char *, |
static enum cmd_retval cmd_wait_for_unlock(struct cmdq_item *, const char *, |
struct wait_channel *); |
struct wait_channel *); |
|
|
static struct wait_channel *cmd_wait_for_add(const char *); |
static struct wait_channel *cmd_wait_for_add(const char *); |
static void cmd_wait_for_remove(struct wait_channel *wc); |
static void cmd_wait_for_remove(struct wait_channel *); |
|
|
static struct wait_channel * |
static struct wait_channel * |
cmd_wait_for_add(const char *name) |
cmd_wait_for_add(const char *name) |
|
|
} |
} |
|
|
static enum cmd_retval |
static enum cmd_retval |
cmd_wait_for_exec(struct cmd *self, struct cmd_q *cmdq) |
cmd_wait_for_exec(struct cmd *self, struct cmdq_item *item) |
{ |
{ |
struct args *args = self->args; |
struct args *args = self->args; |
const char *name = args->argv[0]; |
const char *name = args->argv[0]; |
|
|
wc = RB_FIND(wait_channels, &wait_channels, &wc0); |
wc = RB_FIND(wait_channels, &wait_channels, &wc0); |
|
|
if (args_has(args, 'S')) |
if (args_has(args, 'S')) |
return (cmd_wait_for_signal(cmdq, name, wc)); |
return (cmd_wait_for_signal(item, name, wc)); |
if (args_has(args, 'L')) |
if (args_has(args, 'L')) |
return (cmd_wait_for_lock(cmdq, name, wc)); |
return (cmd_wait_for_lock(item, name, wc)); |
if (args_has(args, 'U')) |
if (args_has(args, 'U')) |
return (cmd_wait_for_unlock(cmdq, name, wc)); |
return (cmd_wait_for_unlock(item, name, wc)); |
return (cmd_wait_for_wait(cmdq, name, wc)); |
return (cmd_wait_for_wait(item, name, wc)); |
} |
} |
|
|
static enum cmd_retval |
static enum cmd_retval |
cmd_wait_for_signal(__unused struct cmd_q *cmdq, const char *name, |
cmd_wait_for_signal(__unused struct cmdq_item *item, const char *name, |
struct wait_channel *wc) |
struct wait_channel *wc) |
{ |
{ |
struct wait_item *wi, *wi1; |
struct wait_item *wi, *wi1; |
|
|
log_debug("signal wait channel %s, with waiters", wc->name); |
log_debug("signal wait channel %s, with waiters", wc->name); |
|
|
TAILQ_FOREACH_SAFE(wi, &wc->waiters, entry, wi1) { |
TAILQ_FOREACH_SAFE(wi, &wc->waiters, entry, wi1) { |
wi->cmdq->flags &= ~CMD_Q_WAITING; |
wi->item->flags &= ~CMDQ_WAITING; |
|
|
TAILQ_REMOVE(&wc->waiters, wi, entry); |
TAILQ_REMOVE(&wc->waiters, wi, entry); |
free(wi); |
free(wi); |
|
|
} |
} |
|
|
static enum cmd_retval |
static enum cmd_retval |
cmd_wait_for_wait(struct cmd_q *cmdq, const char *name, struct wait_channel *wc) |
cmd_wait_for_wait(struct cmdq_item *item, const char *name, |
|
struct wait_channel *wc) |
{ |
{ |
struct client *c = cmdq->client; |
struct client *c = item->client; |
struct wait_item *wi; |
struct wait_item *wi; |
|
|
if (c == NULL || c->session != NULL) { |
if (c == NULL || c->session != NULL) { |
cmdq_error(cmdq, "not able to wait"); |
cmdq_error(item, "not able to wait"); |
return (CMD_RETURN_ERROR); |
return (CMD_RETURN_ERROR); |
} |
} |
|
|
|
|
log_debug("wait channel %s not woken (%p)", wc->name, c); |
log_debug("wait channel %s not woken (%p)", wc->name, c); |
|
|
wi = xcalloc(1, sizeof *wi); |
wi = xcalloc(1, sizeof *wi); |
wi->cmdq = cmdq; |
wi->item = item; |
TAILQ_INSERT_TAIL(&wc->waiters, wi, entry); |
TAILQ_INSERT_TAIL(&wc->waiters, wi, entry); |
|
|
return (CMD_RETURN_WAIT); |
return (CMD_RETURN_WAIT); |
} |
} |
|
|
static enum cmd_retval |
static enum cmd_retval |
cmd_wait_for_lock(struct cmd_q *cmdq, const char *name, struct wait_channel *wc) |
cmd_wait_for_lock(struct cmdq_item *item, const char *name, |
|
struct wait_channel *wc) |
{ |
{ |
struct wait_item *wi; |
struct wait_item *wi; |
|
|
if (cmdq->client == NULL || cmdq->client->session != NULL) { |
if (item->client == NULL || item->client->session != NULL) { |
cmdq_error(cmdq, "not able to lock"); |
cmdq_error(item, "not able to lock"); |
return (CMD_RETURN_ERROR); |
return (CMD_RETURN_ERROR); |
} |
} |
|
|
|
|
|
|
if (wc->locked) { |
if (wc->locked) { |
wi = xcalloc(1, sizeof *wi); |
wi = xcalloc(1, sizeof *wi); |
wi->cmdq = cmdq; |
wi->item = item; |
TAILQ_INSERT_TAIL(&wc->lockers, wi, entry); |
TAILQ_INSERT_TAIL(&wc->lockers, wi, entry); |
return (CMD_RETURN_WAIT); |
return (CMD_RETURN_WAIT); |
} |
} |
|
|
} |
} |
|
|
static enum cmd_retval |
static enum cmd_retval |
cmd_wait_for_unlock(struct cmd_q *cmdq, const char *name, |
cmd_wait_for_unlock(struct cmdq_item *item, const char *name, |
struct wait_channel *wc) |
struct wait_channel *wc) |
{ |
{ |
struct wait_item *wi; |
struct wait_item *wi; |
|
|
if (wc == NULL || !wc->locked) { |
if (wc == NULL || !wc->locked) { |
cmdq_error(cmdq, "channel %s not locked", name); |
cmdq_error(item, "channel %s not locked", name); |
return (CMD_RETURN_ERROR); |
return (CMD_RETURN_ERROR); |
} |
} |
|
|
if ((wi = TAILQ_FIRST(&wc->lockers)) != NULL) { |
if ((wi = TAILQ_FIRST(&wc->lockers)) != NULL) { |
wi->cmdq->flags &= ~CMD_Q_WAITING; |
wi->item->flags &= ~CMDQ_WAITING; |
TAILQ_REMOVE(&wc->lockers, wi, entry); |
TAILQ_REMOVE(&wc->lockers, wi, entry); |
free(wi); |
free(wi); |
} else { |
} else { |
|
|
|
|
RB_FOREACH_SAFE(wc, wait_channels, &wait_channels, wc1) { |
RB_FOREACH_SAFE(wc, wait_channels, &wait_channels, wc1) { |
TAILQ_FOREACH_SAFE(wi, &wc->waiters, entry, wi1) { |
TAILQ_FOREACH_SAFE(wi, &wc->waiters, entry, wi1) { |
wi->cmdq->flags &= ~CMD_Q_WAITING; |
wi->item->flags &= ~CMDQ_WAITING; |
TAILQ_REMOVE(&wc->waiters, wi, entry); |
TAILQ_REMOVE(&wc->waiters, wi, entry); |
free(wi); |
free(wi); |
} |
} |
wc->woken = 1; |
wc->woken = 1; |
TAILQ_FOREACH_SAFE(wi, &wc->lockers, entry, wi1) { |
TAILQ_FOREACH_SAFE(wi, &wc->lockers, entry, wi1) { |
wi->cmdq->flags &= ~CMD_Q_WAITING; |
wi->item->flags &= ~CMDQ_WAITING; |
TAILQ_REMOVE(&wc->lockers, wi, entry); |
TAILQ_REMOVE(&wc->lockers, wi, entry); |
free(wi); |
free(wi); |
} |
} |