lib1554.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, 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. ***************************************************************************/
  22. #include "test.h"
  23. #include "memdebug.h"
  24. static void my_lock(CURL *handle, curl_lock_data data,
  25. curl_lock_access laccess, void *useptr)
  26. {
  27. (void)handle;
  28. (void)data;
  29. (void)laccess;
  30. (void)useptr;
  31. printf("-> Mutex lock\n");
  32. }
  33. static void my_unlock(CURL *handle, curl_lock_data data, void *useptr)
  34. {
  35. (void)handle;
  36. (void)data;
  37. (void)useptr;
  38. printf("<- Mutex unlock\n");
  39. }
  40. /* test function */
  41. int test(char *URL)
  42. {
  43. CURLcode res = CURLE_OK;
  44. CURLSH *share;
  45. int i;
  46. global_init(CURL_GLOBAL_ALL);
  47. share = curl_share_init();
  48. if(!share) {
  49. fprintf(stderr, "curl_share_init() failed\n");
  50. curl_global_cleanup();
  51. return TEST_ERR_MAJOR_BAD;
  52. }
  53. curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT);
  54. curl_share_setopt(share, CURLSHOPT_LOCKFUNC, my_lock);
  55. curl_share_setopt(share, CURLSHOPT_UNLOCKFUNC, my_unlock);
  56. /* Loop the transfer and cleanup the handle properly every lap. This will
  57. still reuse connections since the pool is in the shared object! */
  58. for(i = 0; i < 3; i++) {
  59. CURL *curl = curl_easy_init();
  60. if(curl) {
  61. curl_easy_setopt(curl, CURLOPT_URL, URL);
  62. /* use the share object */
  63. curl_easy_setopt(curl, CURLOPT_SHARE, share);
  64. /* Perform the request, res will get the return code */
  65. res = curl_easy_perform(curl);
  66. /* Check for errors */
  67. if(res != CURLE_OK)
  68. fprintf(stderr, "curl_easy_perform() failed: %s\n",
  69. curl_easy_strerror(res));
  70. /* always cleanup */
  71. curl_easy_cleanup(curl);
  72. }
  73. }
  74. curl_share_cleanup(share);
  75. curl_global_cleanup();
  76. return 0;
  77. }