1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
|
/*
* libwebsockets - small server side websockets and web server implementation
*
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "private-lib-core.h"
/*
* per pending event
*/
typedef struct lws_seq_event {
struct lws_dll2 seq_event_list;
void *data;
void *aux;
lws_seq_events_t e;
} lws_seq_event_t;
/*
* per sequencer
*/
typedef struct lws_sequencer {
struct lws_dll2 seq_list;
lws_sorted_usec_list_t sul_timeout;
lws_sorted_usec_list_t sul_pending;
struct lws_dll2_owner seq_event_owner;
struct lws_context_per_thread *pt;
lws_seq_event_cb cb;
const char *name;
const lws_retry_bo_t *retry;
lws_usec_t time_created;
lws_usec_t timeout; /* 0 or time we timeout */
uint8_t going_down:1;
uint8_t wakesuspend:1;
} lws_seq_t;
#define QUEUE_SANITY_LIMIT 10
static void
lws_sul_seq_heartbeat_cb(lws_sorted_usec_list_t *sul)
{
struct lws_context_per_thread *pt = lws_container_of(sul,
struct lws_context_per_thread, sul_seq_heartbeat);
/* send every sequencer a heartbeat message... it can ignore it */
lws_start_foreach_dll_safe(struct lws_dll2 *, p, tp,
lws_dll2_get_head(&pt->seq_owner)) {
lws_seq_t *s = lws_container_of(p, lws_seq_t, seq_list);
/* queue the message to inform the sequencer */
lws_seq_queue_event(s, LWSSEQ_HEARTBEAT, NULL, NULL);
} lws_end_foreach_dll_safe(p, tp);
/* schedule the next one */
__lws_sul_insert_us(&pt->pt_sul_owner[LWSSULLI_MISS_IF_SUSPENDED],
&pt->sul_seq_heartbeat, LWS_US_PER_SEC);
}
int
lws_seq_pt_init(struct lws_context_per_thread *pt)
{
pt->sul_seq_heartbeat.cb = lws_sul_seq_heartbeat_cb;
/* schedule the first heartbeat */
__lws_sul_insert_us(&pt->pt_sul_owner[LWSSULLI_MISS_IF_SUSPENDED],
&pt->sul_seq_heartbeat, LWS_US_PER_SEC);
return 0;
}
lws_seq_t *
lws_seq_create(lws_seq_info_t *i)
{
struct lws_context_per_thread *pt = &i->context->pt[i->tsi];
lws_seq_t *seq = lws_zalloc(sizeof(*seq) + i->user_size, __func__);
if (!seq)
return NULL;
seq->cb = i->cb;
seq->pt = pt;
seq->name = i->name;
seq->retry = i->retry;
seq->wakesuspend = i->wakesuspend;
*i->puser = (void *)&seq[1];
/* add the sequencer to the pt */
lws_pt_lock(pt, __func__); /* ---------------------------------- pt { */
lws_dll2_add_tail(&seq->seq_list, &pt->seq_owner);
lws_pt_unlock(pt); /* } pt ------------------------------------------ */
seq->time_created = lws_now_usecs();
/* try to queue the creation cb */
if (lws_seq_queue_event(seq, LWSSEQ_CREATED, NULL, NULL)) {
lws_dll2_remove(&seq->seq_list);
lws_free(seq);
return NULL;
}
return seq;
}
static int
seq_ev_destroy(struct lws_dll2 *d, void *user)
{
lws_seq_event_t *seqe = lws_container_of(d, lws_seq_event_t,
seq_event_list);
lws_dll2_remove(&seqe->seq_event_list);
lws_free(seqe);
return 0;
}
void
lws_seq_destroy(lws_seq_t **pseq)
{
lws_seq_t *seq = *pseq;
/* defeat another thread racing to add events while we are destroying */
seq->going_down = 1;
seq->cb(seq, (void *)&seq[1], LWSSEQ_DESTROYED, NULL, NULL);
lws_pt_lock(seq->pt, __func__); /* -------------------------- pt { */
lws_dll2_remove(&seq->seq_list);
lws_dll2_remove(&seq->sul_timeout.list);
lws_dll2_remove(&seq->sul_pending.list);
/* remove and destroy any pending events */
lws_dll2_foreach_safe(&seq->seq_event_owner, NULL, seq_ev_destroy);
lws_pt_unlock(seq->pt); /* } pt ---------------------------------- */
lws_free_set_NULL(seq);
}
void
lws_seq_destroy_all_on_pt(struct lws_context_per_thread *pt)
{
lws_start_foreach_dll_safe(struct lws_dll2 *, p, tp,
pt->seq_owner.head) {
lws_seq_t *s = lws_container_of(p, lws_seq_t,
seq_list);
lws_seq_destroy(&s);
} lws_end_foreach_dll_safe(p, tp);
}
static void
lws_seq_sul_pending_cb(lws_sorted_usec_list_t *sul)
{
lws_seq_t *seq = lws_container_of(sul, lws_seq_t, sul_pending);
lws_seq_event_t *seqe;
struct lws_dll2 *dh;
int n;
if (!seq->seq_event_owner.count)
return;
/* events are only added at tail, so no race possible yet... */
dh = lws_dll2_get_head(&seq->seq_event_owner);
seqe = lws_container_of(dh, lws_seq_event_t, seq_event_list);
n = (int)seq->cb(seq, (void *)&seq[1], (int)seqe->e, seqe->data, seqe->aux);
/* ... have to lock here though, because we will change the list */
lws_pt_lock(seq->pt, __func__); /* ----------------------------- pt { */
/* detach event from sequencer event list and free it */
lws_dll2_remove(&seqe->seq_event_list);
lws_free(seqe);
lws_pt_unlock(seq->pt); /* } pt ------------------------------------- */
if (n) {
lwsl_info("%s: destroying seq '%s' by request\n", __func__,
seq->name);
lws_seq_destroy(&seq);
}
}
int
lws_seq_queue_event(lws_seq_t *seq, lws_seq_events_t e, void *data, void *aux)
{
lws_seq_event_t *seqe;
if (!seq || seq->going_down)
return 1;
seqe = lws_zalloc(sizeof(*seqe), __func__);
if (!seqe)
return 1;
seqe->e = e;
seqe->data = data;
seqe->aux = aux;
// lwsl_notice("%s: seq %s: event %d\n", __func__, seq->name, e);
lws_pt_lock(seq->pt, __func__); /* ----------------------------- pt { */
if (seq->seq_event_owner.count > QUEUE_SANITY_LIMIT) {
lwsl_err("%s: more than %d events queued\n", __func__,
QUEUE_SANITY_LIMIT);
}
lws_dll2_add_tail(&seqe->seq_event_list, &seq->seq_event_owner);
seq->sul_pending.cb = lws_seq_sul_pending_cb;
__lws_sul_insert_us(&seq->pt->pt_sul_owner[seq->wakesuspend],
&seq->sul_pending, 1);
lws_pt_unlock(seq->pt); /* } pt ------------------------------------- */
return 0;
}
/*
* Check if wsi still extant, by peeking in the message queue for a
* LWSSEQ_WSI_CONN_CLOSE message about wsi. (Doesn't need to do the same for
* CONN_FAIL since that will never have produced any messages prior to that).
*
* Use this to avoid trying to perform operations on wsi that have already
* closed but we didn't get to that message yet.
*
* Returns 0 if not closed yet or 1 if it has closed but we didn't process the
* close message yet.
*/
int
lws_seq_check_wsi(lws_seq_t *seq, struct lws *wsi)
{
lws_seq_event_t *seqe;
struct lws_dll2 *dh;
lws_pt_lock(seq->pt, __func__); /* ----------------------------- pt { */
dh = lws_dll2_get_head(&seq->seq_event_owner);
while (dh) {
seqe = lws_container_of(dh, lws_seq_event_t, seq_event_list);
if (seqe->e == LWSSEQ_WSI_CONN_CLOSE && seqe->data == wsi)
break;
dh = dh->next;
}
lws_pt_unlock(seq->pt); /* } pt ------------------------------------- */
return !!dh;
}
static void
lws_seq_sul_timeout_cb(lws_sorted_usec_list_t *sul)
{
lws_seq_t *s = lws_container_of(sul, lws_seq_t, sul_timeout);
lws_seq_queue_event(s, LWSSEQ_TIMED_OUT, NULL, NULL);
}
/* set us to LWS_SET_TIMER_USEC_CANCEL to remove timeout */
int
lws_seq_timeout_us(lws_seq_t *seq, lws_usec_t us)
{
seq->sul_timeout.cb = lws_seq_sul_timeout_cb;
/* list is always at the very top of the sul */
__lws_sul_insert_us(&seq->pt->pt_sul_owner[seq->wakesuspend],
(lws_sorted_usec_list_t *)&seq->sul_timeout.list, us);
return 0;
}
lws_seq_t *
lws_seq_from_user(void *u)
{
return &((lws_seq_t *)u)[-1];
}
const char *
lws_seq_name(lws_seq_t *seq)
{
return seq->name;
}
lws_usec_t
lws_seq_us_since_creation(lws_seq_t *seq)
{
return lws_now_usecs() - seq->time_created;
}
struct lws_context *
lws_seq_get_context(lws_seq_t *seq)
{
return seq->pt->context;
}
|