/* The splay tree only has each sessionhandle as a single node and the nearest timeout is used to sort it on.  */
static CURLMcode add_next_timeout(struct curltime now, struct Curl_multi *multi, struct Curl_easy *d)
/* The splay tree only has each sessionhandle as a single node and the nearest timeout is used to sort it on.  */
static CURLMcode add_next_timeout(struct curltime now, struct Curl_multi *multi, struct Curl_easy *d)
{
  struct curltime *tv = &d->state.expiretime;
  struct curl_llist *list = &d->state.timeoutlist;
  struct curl_llist_element *e;
  struct time_node *node = NULL;
  for(e = list->head; e;) {
    struct curl_llist_element *n = e->next;
    timediff_t diff;
    node = (struct time_node *)e->ptr;
    diff = Curl_timediff(node->time, now);
    if(diff <= 0)
      Curl_llist_remove(list, e, NULL);
    else
      break;
    e = n;
  }
  e = list->head;
  if(!e) {
    tv->tv_sec = 0;
    tv->tv_usec = 0;
  }
  else {
    memcpy(tv, &node->time, sizeof(*tv));
    multi->timetree = Curl_splayinsert(*tv, multi->timetree,
                                       &d->state.timenode);
  }
  return CURLM_OK;
}