{"thread":{"id":"9157","subject":"[PATCH 1/4] Refactor http.h USE_CURL_MULTI fill_active_slots().","startedAt":"2007-07-22T22:09:20Z","lastAt":"2007-07-22T22:09:20Z","messageCount":1,"participants":["Daniel Barkalow"],"isPatch":true,"patchVersion":1,"patchTotal":4},"messages":[{"id":"48210","messageId":"Pine.LNX.4.64.0707221808460.29679@iabervon.org","threadId":"9157","inReplyTo":null,"subject":"[PATCH 1/4] Refactor http.h USE_CURL_MULTI fill_active_slots().","fromName":"Daniel Barkalow","fromEmail":"barkalow@iabervon.org","sentAt":"2007-07-22T22:09:20Z","receivedAt":"2007-07-22T22:09:20Z","isPatch":true,"sender":{"key":"barkalow@iabervon.org","avatar":"https://avatars.githubusercontent.com/u/55364219?v=4"},"body":"This removes all of the boilerplate and http-internal stuff from\nfill_active_slots() and makes it easy to turn into a callback.\n\nSigned-off-by: Daniel Barkalow <barkalow@iabervon.org>\n---\n http-fetch.c |   23 +++++++----------------\n http-push.c  |   23 ++++++-----------------\n http.c       |   24 ++++++++++++++++++++++++\n http.h       |    9 +++------\n 4 files changed, 40 insertions(+), 39 deletions(-)\n\ndiff --git a/http-fetch.c b/http-fetch.c\nindex 202fae0..e9b9f30 100644\n--- a/http-fetch.c\n+++ b/http-fetch.c\n@@ -317,30 +317,21 @@ static void release_object_request(struct object_request *obj_req)\n }\n \n #ifdef USE_CURL_MULTI\n-void fill_active_slots(void)\n+int fill_active_slot(void)\n {\n-\tstruct object_request *obj_req = object_queue_head;\n-\tstruct active_request_slot *slot = active_queue_head;\n-\tint num_transfers;\n+\tstruct object_request *obj_req;\n \n-\twhile (active_requests < max_requests && obj_req != NULL) {\n+\tfor (obj_req = object_queue_head; obj_req; obj_req = obj_req->next) {\n \t\tif (obj_req->state == WAITING) {\n \t\t\tif (has_sha1_file(obj_req->sha1))\n \t\t\t\tobj_req->state = COMPLETE;\n-\t\t\telse\n+\t\t\telse {\n \t\t\t\tstart_object_request(obj_req);\n-\t\t\tcurl_multi_perform(curlm, &num_transfers);\n-\t\t}\n-\t\tobj_req = obj_req->next;\n-\t}\n-\n-\twhile (slot != NULL) {\n-\t\tif (!slot->in_use && slot->curl != NULL) {\n-\t\t\tcurl_easy_cleanup(slot->curl);\n-\t\t\tslot->curl = NULL;\n+\t\t\t\treturn 1;\n+\t\t\t}\n \t\t}\n-\t\tslot = slot->next;\n \t}\n+\treturn 0;\n }\n #endif\n \ndiff --git a/http-push.c b/http-push.c\nindex 7c3720f..c7471fa 100644\n--- a/http-push.c\n+++ b/http-push.c\n@@ -795,38 +795,27 @@ static void finish_request(struct transfer_request *request)\n }\n \n #ifdef USE_CURL_MULTI\n-void fill_active_slots(void)\n+int fill_active_slot(void)\n {\n \tstruct transfer_request *request = request_queue_head;\n-\tstruct transfer_request *next;\n-\tstruct active_request_slot *slot = active_queue_head;\n-\tint num_transfers;\n \n \tif (aborted)\n-\t\treturn;\n+\t\treturn 0;\n \n-\twhile (active_requests < max_requests && request != NULL) {\n-\t\tnext = request->next;\n+\tfor (request = request_queue_head; request; request = request->next) {\n \t\tif (request->state == NEED_FETCH) {\n \t\t\tstart_fetch_loose(request);\n+\t\t\treturn 1;\n \t\t} else if (pushing && request->state == NEED_PUSH) {\n \t\t\tif (remote_dir_exists[request->obj->sha1[0]] == 1) {\n \t\t\t\tstart_put(request);\n \t\t\t} else {\n \t\t\t\tstart_mkcol(request);\n \t\t\t}\n-\t\t\tcurl_multi_perform(curlm, &num_transfers);\n-\t\t}\n-\t\trequest = next;\n-\t}\n-\n-\twhile (slot != NULL) {\n-\t\tif (!slot->in_use && slot->curl != NULL) {\n-\t\t\tcurl_easy_cleanup(slot->curl);\n-\t\t\tslot->curl = NULL;\n+\t\t\treturn 1;\n \t\t}\n-\t\tslot = slot->next;\n \t}\n+\treturn 0;\n }\n #endif\n \ndiff --git a/http.c b/http.c\nindex c6fb8ac..1f305bd 100644\n--- a/http.c\n+++ b/http.c\n@@ -372,6 +372,7 @@ int start_active_slot(struct active_request_slot *slot)\n {\n #ifdef USE_CURL_MULTI\n \tCURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl);\n+\tint num_transfers;\n \n \tif (curlm_result != CURLM_OK &&\n \t    curlm_result != CURLM_CALL_MULTI_PERFORM) {\n@@ -379,11 +380,34 @@ int start_active_slot(struct active_request_slot *slot)\n \t\tslot->in_use = 0;\n \t\treturn 0;\n \t}\n+\n+\t/*\n+\t * We know there must be something to do, since we just added\n+\t * something.\n+\t */\n+\tcurl_multi_perform(curlm, &num_transfers);\n #endif\n \treturn 1;\n }\n \n #ifdef USE_CURL_MULTI\n+void fill_active_slots(void)\n+{\n+\tstruct active_request_slot *slot = active_queue_head;\n+\n+\twhile (active_requests < max_requests)\n+\t\tif (!fill_active_slot())\n+\t\t\tbreak;\n+\n+\twhile (slot != NULL) {\n+\t\tif (!slot->in_use && slot->curl != NULL) {\n+\t\t\tcurl_easy_cleanup(slot->curl);\n+\t\t\tslot->curl = NULL;\n+\t\t}\n+\t\tslot = slot->next;\n+\t}\n+}\n+\n void step_active_slots(void)\n {\n \tint num_transfers;\ndiff --git a/http.h b/http.h\nindex 69b6b66..559105c 100644\n--- a/http.h\n+++ b/http.h\n@@ -71,6 +71,9 @@ extern void release_active_slot(struct active_request_slot *slot);\n #ifdef USE_CURL_MULTI\n extern void fill_active_slots(void);\n extern void step_active_slots(void);\n+\n+/* Provided by the program using http. */\n+extern int fill_active_slot(void);\n #endif\n \n extern void http_init(void);\n@@ -79,10 +82,6 @@ extern void http_cleanup(void);\n extern int data_received;\n extern int active_requests;\n \n-#ifdef USE_CURL_MULTI\n-extern int max_requests;\n-extern CURLM *curlm;\n-#endif\n #ifndef NO_CURL_EASY_DUPHANDLE\n extern CURL *curl_default;\n #endif\n@@ -103,6 +102,4 @@ extern long curl_low_speed_time;\n extern struct curl_slist *pragma_header;\n extern struct curl_slist *no_range_header;\n \n-extern struct active_request_slot *active_queue_head;\n-\n #endif /* HTTP_H */\n-- \n1.5.3.rc1.818.g84b7\n"}]}