1 /* $OpenBSD: cmd-wait-for.c,v 1.1 2013/03/25 10:09:05 nicm Exp $ */ 2 3 /* 4 * Copyright (c) 2013 Nicholas Marriott <nicm@users.sourceforge.net> 5 * Copyright (c) 2013 Thiago de Arruda <tpadilha84@gmail.com> 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER 16 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING 17 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20 #include <sys/types.h> 21 22 #include <stdlib.h> 23 #include <string.h> 24 25 #include "tmux.h" 26 27 /* 28 * Block or wake a client on a named wait channel. 29 */ 30 31 enum cmd_retval cmd_wait_for_exec(struct cmd *, struct cmd_q *); 32 33 const struct cmd_entry cmd_wait_for_entry = { 34 "wait-for", "wait", 35 "S", 1, 1, 36 "[-S] channel", 37 0, 38 NULL, 39 NULL, 40 cmd_wait_for_exec 41 }; 42 43 struct wait_channel { 44 const char *name; 45 TAILQ_HEAD(, cmd_q) waiters; 46 47 RB_ENTRY(wait_channel) entry; 48 }; 49 RB_HEAD(wait_channels, wait_channel); 50 struct wait_channels wait_channels = RB_INITIALIZER(wait_channels); 51 52 int wait_channel_cmp(struct wait_channel *, struct wait_channel *); 53 RB_PROTOTYPE(wait_channels, wait_channel, entry, wait_channel_cmp); 54 RB_GENERATE(wait_channels, wait_channel, entry, wait_channel_cmp); 55 56 int 57 wait_channel_cmp(struct wait_channel *wc1, struct wait_channel *wc2) 58 { 59 return (strcmp(wc1->name, wc2->name)); 60 } 61 62 enum cmd_retval cmd_wait_for_signal(struct cmd_q *, const char *, 63 struct wait_channel *); 64 enum cmd_retval cmd_wait_for_wait(struct cmd_q *, const char *, 65 struct wait_channel *); 66 67 enum cmd_retval 68 cmd_wait_for_exec(struct cmd *self, struct cmd_q *cmdq) 69 { 70 struct args *args = self->args; 71 const char *name = args->argv[0]; 72 struct wait_channel *wc, wc0; 73 74 wc0.name = name; 75 wc = RB_FIND(wait_channels, &wait_channels, &wc0); 76 77 if (args_has(args, 'S')) 78 return (cmd_wait_for_signal(cmdq, name, wc)); 79 return (cmd_wait_for_wait(cmdq, name, wc)); 80 } 81 82 enum cmd_retval 83 cmd_wait_for_signal(struct cmd_q *cmdq, const char *name, 84 struct wait_channel *wc) 85 { 86 struct cmd_q *wq, *wq1; 87 88 if (wc == NULL || TAILQ_EMPTY(&wc->waiters)) { 89 cmdq_error(cmdq, "no waiting clients on %s", name); 90 return (CMD_RETURN_ERROR); 91 } 92 93 TAILQ_FOREACH_SAFE(wq, &wc->waiters, waitentry, wq1) { 94 TAILQ_REMOVE(&wc->waiters, wq, waitentry); 95 if (!cmdq_free(wq)) 96 cmdq_continue(wq); 97 } 98 RB_REMOVE(wait_channels, &wait_channels, wc); 99 free((void*) wc->name); 100 free(wc); 101 102 return (CMD_RETURN_NORMAL); 103 } 104 105 enum cmd_retval 106 cmd_wait_for_wait(struct cmd_q *cmdq, const char *name, 107 struct wait_channel *wc) 108 { 109 if (cmdq->client == NULL || cmdq->client->session != NULL) { 110 cmdq_error(cmdq, "not able to wait"); 111 return (CMD_RETURN_ERROR); 112 } 113 114 if (wc == NULL) { 115 wc = xmalloc(sizeof *wc); 116 wc->name = xstrdup(name); 117 TAILQ_INIT(&wc->waiters); 118 RB_INSERT(wait_channels, &wait_channels, wc); 119 } 120 TAILQ_INSERT_TAIL(&wc->waiters, cmdq, waitentry); 121 cmdq->references++; 122 123 return (CMD_RETURN_WAIT); 124 } 125