Coverage Report

Created: 2025-08-29 06:28

/src/tmux/cmd-wait-for.c
Line
Count
Source (jump to first uncovered line)
1
/* $OpenBSD$ */
2
3
/*
4
 * Copyright (c) 2013 Nicholas Marriott <nicholas.marriott@gmail.com>
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
static enum cmd_retval cmd_wait_for_exec(struct cmd *, struct cmdq_item *);
32
33
const struct cmd_entry cmd_wait_for_entry = {
34
  .name = "wait-for",
35
  .alias = "wait",
36
37
  .args = { "LSU", 1, 1, NULL },
38
  .usage = "[-L|-S|-U] channel",
39
40
  .flags = 0,
41
  .exec = cmd_wait_for_exec
42
};
43
44
struct wait_item {
45
  struct cmdq_item  *item;
46
  TAILQ_ENTRY(wait_item)   entry;
47
};
48
49
struct wait_channel {
50
  const char         *name;
51
  int     locked;
52
  int     woken;
53
54
  TAILQ_HEAD(, wait_item) waiters;
55
  TAILQ_HEAD(, wait_item) lockers;
56
57
  RB_ENTRY(wait_channel)  entry;
58
};
59
RB_HEAD(wait_channels, wait_channel);
60
static struct wait_channels wait_channels = RB_INITIALIZER(wait_channels);
61
62
static int wait_channel_cmp(struct wait_channel *, struct wait_channel *);
63
RB_GENERATE_STATIC(wait_channels, wait_channel, entry, wait_channel_cmp);
64
65
static int
66
wait_channel_cmp(struct wait_channel *wc1, struct wait_channel *wc2)
67
0
{
68
0
  return (strcmp(wc1->name, wc2->name));
69
0
}
70
71
static enum cmd_retval  cmd_wait_for_signal(struct cmdq_item *, const char *,
72
          struct wait_channel *);
73
static enum cmd_retval  cmd_wait_for_wait(struct cmdq_item *, const char *,
74
          struct wait_channel *);
75
static enum cmd_retval  cmd_wait_for_lock(struct cmdq_item *, const char *,
76
          struct wait_channel *);
77
static enum cmd_retval  cmd_wait_for_unlock(struct cmdq_item *, const char *,
78
          struct wait_channel *);
79
80
static struct wait_channel  *cmd_wait_for_add(const char *);
81
static void      cmd_wait_for_remove(struct wait_channel *);
82
83
static struct wait_channel *
84
cmd_wait_for_add(const char *name)
85
0
{
86
0
  struct wait_channel *wc;
87
88
0
  wc = xmalloc(sizeof *wc);
89
0
  wc->name = xstrdup(name);
90
91
0
  wc->locked = 0;
92
0
  wc->woken = 0;
93
94
0
  TAILQ_INIT(&wc->waiters);
95
0
  TAILQ_INIT(&wc->lockers);
96
97
0
  RB_INSERT(wait_channels, &wait_channels, wc);
98
99
0
  log_debug("add wait channel %s", wc->name);
100
101
0
  return (wc);
102
0
}
103
104
static void
105
cmd_wait_for_remove(struct wait_channel *wc)
106
0
{
107
0
  if (wc->locked)
108
0
    return;
109
0
  if (!TAILQ_EMPTY(&wc->waiters) || !wc->woken)
110
0
    return;
111
112
0
  log_debug("remove wait channel %s", wc->name);
113
114
0
  RB_REMOVE(wait_channels, &wait_channels, wc);
115
116
0
  free((void *)wc->name);
117
0
  free(wc);
118
0
}
119
120
static enum cmd_retval
121
cmd_wait_for_exec(struct cmd *self, struct cmdq_item *item)
122
0
{
123
0
  struct args       *args = cmd_get_args(self);
124
0
  const char    *name = args_string(args, 0);
125
0
  struct wait_channel *wc, find;
126
127
0
  find.name = name;
128
0
  wc = RB_FIND(wait_channels, &wait_channels, &find);
129
130
0
  if (args_has(args, 'S'))
131
0
    return (cmd_wait_for_signal(item, name, wc));
132
0
  if (args_has(args, 'L'))
133
0
    return (cmd_wait_for_lock(item, name, wc));
134
0
  if (args_has(args, 'U'))
135
0
    return (cmd_wait_for_unlock(item, name, wc));
136
0
  return (cmd_wait_for_wait(item, name, wc));
137
0
}
138
139
static enum cmd_retval
140
cmd_wait_for_signal(__unused struct cmdq_item *item, const char *name,
141
    struct wait_channel *wc)
142
0
{
143
0
  struct wait_item  *wi, *wi1;
144
145
0
  if (wc == NULL)
146
0
    wc = cmd_wait_for_add(name);
147
148
0
  if (TAILQ_EMPTY(&wc->waiters) && !wc->woken) {
149
0
    log_debug("signal wait channel %s, no waiters", wc->name);
150
0
    wc->woken = 1;
151
0
    return (CMD_RETURN_NORMAL);
152
0
  }
153
0
  log_debug("signal wait channel %s, with waiters", wc->name);
154
155
0
  TAILQ_FOREACH_SAFE(wi, &wc->waiters, entry, wi1) {
156
0
    cmdq_continue(wi->item);
157
158
0
    TAILQ_REMOVE(&wc->waiters, wi, entry);
159
0
    free(wi);
160
0
  }
161
162
0
  cmd_wait_for_remove(wc);
163
0
  return (CMD_RETURN_NORMAL);
164
0
}
165
166
static enum cmd_retval
167
cmd_wait_for_wait(struct cmdq_item *item, const char *name,
168
    struct wait_channel *wc)
169
0
{
170
0
  struct client   *c = cmdq_get_client(item);
171
0
  struct wait_item  *wi;
172
173
0
  if (c == NULL) {
174
0
    cmdq_error(item, "not able to wait");
175
0
    return (CMD_RETURN_ERROR);
176
0
  }
177
178
0
  if (wc == NULL)
179
0
    wc = cmd_wait_for_add(name);
180
181
0
  if (wc->woken) {
182
0
    log_debug("wait channel %s already woken (%p)", wc->name, c);
183
0
    cmd_wait_for_remove(wc);
184
0
    return (CMD_RETURN_NORMAL);
185
0
  }
186
0
  log_debug("wait channel %s not woken (%p)", wc->name, c);
187
188
0
  wi = xcalloc(1, sizeof *wi);
189
0
  wi->item = item;
190
0
  TAILQ_INSERT_TAIL(&wc->waiters, wi, entry);
191
192
0
  return (CMD_RETURN_WAIT);
193
0
}
194
195
static enum cmd_retval
196
cmd_wait_for_lock(struct cmdq_item *item, const char *name,
197
    struct wait_channel *wc)
198
0
{
199
0
  struct wait_item  *wi;
200
201
0
  if (cmdq_get_client(item) == NULL) {
202
0
    cmdq_error(item, "not able to lock");
203
0
    return (CMD_RETURN_ERROR);
204
0
  }
205
206
0
  if (wc == NULL)
207
0
    wc = cmd_wait_for_add(name);
208
209
0
  if (wc->locked) {
210
0
    wi = xcalloc(1, sizeof *wi);
211
0
    wi->item = item;
212
0
    TAILQ_INSERT_TAIL(&wc->lockers, wi, entry);
213
0
    return (CMD_RETURN_WAIT);
214
0
  }
215
0
  wc->locked = 1;
216
217
0
  return (CMD_RETURN_NORMAL);
218
0
}
219
220
static enum cmd_retval
221
cmd_wait_for_unlock(struct cmdq_item *item, const char *name,
222
    struct wait_channel *wc)
223
0
{
224
0
  struct wait_item  *wi;
225
226
0
  if (wc == NULL || !wc->locked) {
227
0
    cmdq_error(item, "channel %s not locked", name);
228
0
    return (CMD_RETURN_ERROR);
229
0
  }
230
231
0
  if ((wi = TAILQ_FIRST(&wc->lockers)) != NULL) {
232
0
    cmdq_continue(wi->item);
233
0
    TAILQ_REMOVE(&wc->lockers, wi, entry);
234
0
    free(wi);
235
0
  } else {
236
0
    wc->locked = 0;
237
0
    cmd_wait_for_remove(wc);
238
0
  }
239
240
0
  return (CMD_RETURN_NORMAL);
241
0
}
242
243
void
244
cmd_wait_for_flush(void)
245
0
{
246
0
  struct wait_channel *wc, *wc1;
247
0
  struct wait_item  *wi, *wi1;
248
249
0
  RB_FOREACH_SAFE(wc, wait_channels, &wait_channels, wc1) {
250
0
    TAILQ_FOREACH_SAFE(wi, &wc->waiters, entry, wi1) {
251
0
      cmdq_continue(wi->item);
252
0
      TAILQ_REMOVE(&wc->waiters, wi, entry);
253
0
      free(wi);
254
0
    }
255
0
    wc->woken = 1;
256
0
    TAILQ_FOREACH_SAFE(wi, &wc->lockers, entry, wi1) {
257
0
      cmdq_continue(wi->item);
258
0
      TAILQ_REMOVE(&wc->lockers, wi, entry);
259
0
      free(wi);
260
0
    }
261
0
    wc->locked = 0;
262
0
    cmd_wait_for_remove(wc);
263
0
  }
264
0
}