• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
10  * This software is licensed as described in the file COPYING, which
11  * you should have received as part of this distribution. The terms
12  * are also available at https://curl.se/docs/copyright.html.
13  *
14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15  * copies of the Software, and permit persons to whom the Software is
16  * furnished to do so, under the terms of the COPYING file.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  * SPDX-License-Identifier: curl
22  *
23  ***************************************************************************/
24 /* <DESC>
25  * Connection cache shared between easy handles with the share interface
26  * </DESC>
27  */
28 #include <stdio.h>
29 #include <curl/curl.h>
30 
my_lock(CURL * handle,curl_lock_data data,curl_lock_access laccess,void * useptr)31 static void my_lock(CURL *handle, curl_lock_data data,
32                     curl_lock_access laccess, void *useptr)
33 {
34   (void)handle;
35   (void)data;
36   (void)laccess;
37   (void)useptr;
38   fprintf(stderr, "-> Mutex lock\n");
39 }
40 
my_unlock(CURL * handle,curl_lock_data data,void * useptr)41 static void my_unlock(CURL *handle, curl_lock_data data, void *useptr)
42 {
43   (void)handle;
44   (void)data;
45   (void)useptr;
46   fprintf(stderr, "<- Mutex unlock\n");
47 }
48 
main(void)49 int main(void)
50 {
51   CURLSH *share;
52   int i;
53 
54   share = curl_share_init();
55   curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT);
56 
57   curl_share_setopt(share, CURLSHOPT_LOCKFUNC, my_lock);
58   curl_share_setopt(share, CURLSHOPT_UNLOCKFUNC, my_unlock);
59 
60   /* Loop the transfer and cleanup the handle properly every lap. This will
61      still reuse connections since the pool is in the shared object! */
62 
63   for(i = 0; i < 3; i++) {
64     CURL *curl = curl_easy_init();
65     if(curl) {
66       CURLcode res;
67 
68       curl_easy_setopt(curl, CURLOPT_URL, "https://curl.se/");
69 
70       /* use the share object */
71       curl_easy_setopt(curl, CURLOPT_SHARE, share);
72 
73       /* Perform the request, res will get the return code */
74       res = curl_easy_perform(curl);
75       /* Check for errors */
76       if(res != CURLE_OK)
77         fprintf(stderr, "curl_easy_perform() failed: %s\n",
78                 curl_easy_strerror(res));
79 
80       /* always cleanup */
81       curl_easy_cleanup(curl);
82     }
83   }
84 
85   curl_share_cleanup(share);
86   return 0;
87 }
88