@@ -400,6 +400,9 @@ static int _send_block (struct curvecpr_messager *messager, struct curvecpr_bloc
400400 sent because it could fail for any other arbitrary reason as well and need
401401 to be reinvoked. */
402402 }
403+
404+ /* Update the last sent time for timeout calcuations. */
405+ messager -> my_sent_clock = messager -> chicago .clock ;
403406 }
404407
405408 /* Remove all the acknowledged ranges from the pending queue. */
@@ -418,9 +421,6 @@ static int _send_block (struct curvecpr_messager *messager, struct curvecpr_bloc
418421 if (messager -> their_eof && messager -> their_contiguous_sent_bytes >= messager -> their_total_bytes )
419422 messager -> their_final = 1 ;
420423
421- /* Update the last sent time for timeout calcuations. */
422- messager -> my_sent_clock = messager -> chicago .clock ;
423-
424424 /* Reset last received ID so we don't acknowledge an old message. */
425425 messager -> their_sent_id = 0 ;
426426
@@ -490,13 +490,19 @@ long long curvecpr_messager_next_timeout (struct curvecpr_messager *messager)
490490
491491 long long at , timeout ;
492492
493+ /* If we have anything to be written, we wouldn't spin at all, so don't include an
494+ adjustment in the timeout for it in that case. */
495+ int would_spin = 1 ;
496+
493497 curvecpr_chicago_refresh_clock (chicago );
494498
495499 at = chicago -> clock + 60000000000LL ; /* 60 seconds. */
496500
497501 if (!cf -> ops .sendmarkq_is_full (messager )) {
498502 /* If we have pending data, we might write it. */
499503 if (!cf -> ops .sendq_is_empty (messager )) {
504+ would_spin = 0 ;
505+
500506 /* Write at the write rate. */
501507 if (at > messager -> my_sent_clock + chicago -> wr_rate )
502508 at = messager -> my_sent_clock + chicago -> wr_rate ;
@@ -507,16 +513,20 @@ long long curvecpr_messager_next_timeout (struct curvecpr_messager *messager)
507513 if (cf -> ops .sendmarkq_head (messager , & block )) {
508514 /* No earliest block. */
509515 } else {
516+ would_spin = 0 ;
517+
510518 if (at > block -> clock + chicago -> rtt_timeout )
511519 at = block -> clock + chicago -> rtt_timeout ;
512520 }
513521
514- /* If the current time is after the next action time, the timeout is 0. However, we
515- always have at least a 1 millisecond timeout to prevent the CPU from spinning. */
516522 if (chicago -> clock > at )
517- timeout = 1000000 ;
523+ timeout = 0 ;
518524 else
519- timeout = at - chicago -> clock + 1000000 ;
525+ timeout = at - chicago -> clock ;
526+
527+ /* Apply spinning adjustment if necessary. */
528+ if (would_spin )
529+ timeout += 1000000 ;
520530
521531 if (cf -> ops .put_next_timeout )
522532 cf -> ops .put_next_timeout (messager , timeout );
0 commit comments