Commit c346c4c8 authored by Daniel Stenberg's avatar Daniel Stenberg
Browse files

multi_init: moved init code here from add_handle

The closure_handle is "owned" by the multi handle and it is
unconditional so the setting up of it should be in the Curl_multi_handle
function rather than curl_multi_add_handle.
parent bc7d806e
Loading
Loading
Loading
Loading
+18 −39
Original line number Diff line number Diff line
@@ -307,6 +307,14 @@ struct Curl_multi *Curl_multi_handle(int hashsize, /* socket hash */
  if(!multi->msglist)
    goto error;

  /* allocate a new easy handle to use when closing cached connections */
  multi->closure_handle = curl_easy_init();
  if(!multi->closure_handle)
    goto error;

  multi->closure_handle->multi = multi;
  multi->closure_handle->state.conn_cache = multi->conn_cache;

  multi->max_pipeline_length = 5;
  return (CURLM *) multi;

@@ -318,6 +326,8 @@ struct Curl_multi *Curl_multi_handle(int hashsize, /* socket hash */
  multi->hostcache = NULL;
  Curl_conncache_destroy(multi->conn_cache);
  multi->conn_cache = NULL;
  Curl_close(multi->closure_handle);
  multi->closure_handle = NULL;

  free(multi);
  return NULL;
@@ -329,22 +339,12 @@ CURLM *curl_multi_init(void)
                           CURL_CONNECTION_HASH_SIZE);
}

/*
 * Store a pointed to the multi handle within the easy handle's data struct.
 */
static void easy_addmulti(struct SessionHandle *data,
                          void *multi)
{
  data->multi = multi;
}

CURLMcode curl_multi_add_handle(CURLM *multi_handle,
                                CURL *easy_handle)
{
  struct curl_llist *timeoutlist;
  struct Curl_multi *multi = (struct Curl_multi *)multi_handle;
  struct SessionHandle *data = (struct SessionHandle *)easy_handle;
  struct SessionHandle *new_closure = NULL;

  /* First, make some basic checks that the CURLM handle is a good handle */
  if(!GOOD_MULTI_HANDLE(multi))
@@ -365,32 +365,13 @@ CURLMcode curl_multi_add_handle(CURLM *multi_handle,
  if(!timeoutlist)
    return CURLM_OUT_OF_MEMORY;

  /* In case multi handle has no closure_handle yet, allocate
     a new easy handle to use when closing cached connections */
  if(!multi->closure_handle) {
    new_closure = (struct SessionHandle *)curl_easy_init();
    if(!new_closure) {
      free(data);
      Curl_llist_destroy(timeoutlist, NULL);
      return CURLM_OUT_OF_MEMORY;
    }
  }

  /*
  ** No failure allowed in this function beyond this point. And
  ** no modification of easy nor multi handle allowed before this
  ** except for potential multi's connection cache growing which
  ** won't be undone in this function no matter what.
   * No failure allowed in this function beyond this point. And no
   * modification of easy nor multi handle allowed before this except for
   * potential multi's connection cache growing which won't be undone in this
   * function no matter what.
   */

  /* In case a new closure handle has been initialized above, it
     is associated now with the multi handle which lacked one. */
  if(new_closure) {
    multi->closure_handle = new_closure;
    easy_addmulti(multi->closure_handle, multi_handle);
    multi->closure_handle->state.conn_cache = multi->conn_cache;
  }

  /* Make easy handle use timeout list initialized above */
  data->state.timeoutlist = timeoutlist;
  timeoutlist = NULL;
@@ -440,7 +421,7 @@ CURLMcode curl_multi_add_handle(CURLM *multi_handle,
  }

  /* make the SessionHandle refer back to this multi handle */
  easy_addmulti(easy_handle, multi_handle);
  data->multi = multi_handle;

  /* Set the timeout for this handle to expire really soon so that it will
     be taken care of even when this handle is added in the midst of operation
@@ -581,8 +562,7 @@ CURLMcode curl_multi_remove_handle(CURLM *multi_handle,
      data->easy_conn = NULL;
    }

    easy_addmulti(data, NULL); /* clear the association to this multi
                                  handle */
    data->multi = NULL; /* clear the association to this multi handle */

    {
      /* make sure there's no pending message in the queue sent from this easy
@@ -1836,8 +1816,7 @@ CURLMcode curl_multi_cleanup(CURLM *multi_handle)

      /* Clear the pointer to the connection cache */
      data->state.conn_cache = NULL;

      easy_addmulti(data, NULL); /* clear the association */
      data->multi = NULL; /* clear the association */

      data = nextdata;
    }