1/*
2 * nghttp3
3 *
4 * Copyright (c) 2019 nghttp3 contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25#include "nghttp3_tnode.h"
26
27#include <assert.h>
28
29#include "nghttp3_macro.h"
30#include "nghttp3_stream.h"
31#include "nghttp3_conn.h"
32#include "nghttp3_conv.h"
33
34nghttp3_node_id *nghttp3_node_id_init(nghttp3_node_id *nid,
35                                      nghttp3_node_id_type type, int64_t id) {
36  nid->type = type;
37  nid->id = id;
38  return nid;
39}
40
41int nghttp3_node_id_eq(const nghttp3_node_id *a, const nghttp3_node_id *b) {
42  return a->type == b->type && a->id == b->id;
43}
44
45void nghttp3_tnode_init(nghttp3_tnode *tnode, const nghttp3_node_id *nid,
46                        uint64_t seq, uint8_t pri) {
47  assert(nghttp3_pri_uint8_urgency(pri) < NGHTTP3_URGENCY_LEVELS);
48
49  tnode->pe.index = NGHTTP3_PQ_BAD_INDEX;
50  tnode->nid = *nid;
51  tnode->seq = seq;
52  tnode->cycle = 0;
53  tnode->pri = pri;
54}
55
56void nghttp3_tnode_free(nghttp3_tnode *tnode) { (void)tnode; }
57
58static void tnode_unschedule(nghttp3_tnode *tnode, nghttp3_pq *pq) {
59  assert(tnode->pe.index != NGHTTP3_PQ_BAD_INDEX);
60
61  nghttp3_pq_remove(pq, &tnode->pe);
62  tnode->pe.index = NGHTTP3_PQ_BAD_INDEX;
63}
64
65void nghttp3_tnode_unschedule(nghttp3_tnode *tnode, nghttp3_pq *pq) {
66  if (tnode->pe.index == NGHTTP3_PQ_BAD_INDEX) {
67    return;
68  }
69
70  tnode_unschedule(tnode, pq);
71}
72
73static uint64_t pq_get_first_cycle(nghttp3_pq *pq) {
74  nghttp3_tnode *top;
75
76  if (nghttp3_pq_empty(pq)) {
77    return 0;
78  }
79
80  top = nghttp3_struct_of(nghttp3_pq_top(pq), nghttp3_tnode, pe);
81  return top->cycle;
82}
83
84int nghttp3_tnode_schedule(nghttp3_tnode *tnode, nghttp3_pq *pq,
85                           uint64_t nwrite) {
86  uint64_t penalty = nwrite / NGHTTP3_STREAM_MIN_WRITELEN;
87
88  if (tnode->pe.index == NGHTTP3_PQ_BAD_INDEX) {
89    tnode->cycle = pq_get_first_cycle(pq) +
90                   ((nwrite == 0 || !nghttp3_pri_uint8_inc(tnode->pri))
91                        ? 0
92                        : nghttp3_max(1, penalty));
93  } else if (nwrite > 0) {
94    if (!nghttp3_pri_uint8_inc(tnode->pri) || nghttp3_pq_size(pq) == 1) {
95      return 0;
96    }
97
98    nghttp3_pq_remove(pq, &tnode->pe);
99    tnode->pe.index = NGHTTP3_PQ_BAD_INDEX;
100    tnode->cycle += nghttp3_max(1, penalty);
101  } else {
102    return 0;
103  }
104
105  return nghttp3_pq_push(pq, &tnode->pe);
106}
107
108int nghttp3_tnode_is_scheduled(nghttp3_tnode *tnode) {
109  return tnode->pe.index != NGHTTP3_PQ_BAD_INDEX;
110}
111