nghttp3_tnode.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. #include <assert.h>
  27. #include "nghttp3_macro.h"
  28. #include "nghttp3_stream.h"
  29. #include "nghttp3_conn.h"
  30. #include "nghttp3_conv.h"
  31. void nghttp3_tnode_init(nghttp3_tnode *tnode, int64_t id) {
  32. tnode->pe.index = NGHTTP3_PQ_BAD_INDEX;
  33. tnode->id = id;
  34. tnode->cycle = 0;
  35. tnode->pri.urgency = NGHTTP3_DEFAULT_URGENCY;
  36. tnode->pri.inc = 0;
  37. }
  38. void nghttp3_tnode_free(nghttp3_tnode *tnode) { (void)tnode; }
  39. static void tnode_unschedule(nghttp3_tnode *tnode, nghttp3_pq *pq) {
  40. assert(tnode->pe.index != NGHTTP3_PQ_BAD_INDEX);
  41. nghttp3_pq_remove(pq, &tnode->pe);
  42. tnode->pe.index = NGHTTP3_PQ_BAD_INDEX;
  43. }
  44. void nghttp3_tnode_unschedule(nghttp3_tnode *tnode, nghttp3_pq *pq) {
  45. if (tnode->pe.index == NGHTTP3_PQ_BAD_INDEX) {
  46. return;
  47. }
  48. tnode_unschedule(tnode, pq);
  49. }
  50. static uint64_t pq_get_first_cycle(nghttp3_pq *pq) {
  51. nghttp3_tnode *top;
  52. if (nghttp3_pq_empty(pq)) {
  53. return 0;
  54. }
  55. top = nghttp3_struct_of(nghttp3_pq_top(pq), nghttp3_tnode, pe);
  56. return top->cycle;
  57. }
  58. int nghttp3_tnode_schedule(nghttp3_tnode *tnode, nghttp3_pq *pq,
  59. uint64_t nwrite) {
  60. uint64_t penalty = nwrite / NGHTTP3_STREAM_MIN_WRITELEN;
  61. if (tnode->pe.index == NGHTTP3_PQ_BAD_INDEX) {
  62. tnode->cycle =
  63. pq_get_first_cycle(pq) +
  64. ((nwrite == 0 || !tnode->pri.inc) ? 0 : nghttp3_max_uint64(1, penalty));
  65. } else if (nwrite > 0) {
  66. if (!tnode->pri.inc || nghttp3_pq_size(pq) == 1) {
  67. return 0;
  68. }
  69. nghttp3_pq_remove(pq, &tnode->pe);
  70. tnode->pe.index = NGHTTP3_PQ_BAD_INDEX;
  71. tnode->cycle += nghttp3_max_uint64(1, penalty);
  72. } else {
  73. return 0;
  74. }
  75. return nghttp3_pq_push(pq, &tnode->pe);
  76. }
  77. int nghttp3_tnode_is_scheduled(nghttp3_tnode *tnode) {
  78. return tnode->pe.index != NGHTTP3_PQ_BAD_INDEX;
  79. }