2
0

lib1554.c 2.6 KB

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