2
0

lib1554.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. #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. CURLcode test(char *URL)
  44. {
  45. CURLcode res = CURLE_OK;
  46. CURLSH *share = NULL;
  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. goto test_cleanup;
  53. }
  54. curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT);
  55. curl_share_setopt(share, CURLSHOPT_LOCKFUNC, my_lock);
  56. curl_share_setopt(share, CURLSHOPT_UNLOCKFUNC, my_unlock);
  57. /* Loop the transfer and cleanup the handle properly every lap. This will
  58. still reuse connections since the pool is in the shared object! */
  59. for(i = 0; i < 3; i++) {
  60. CURL *curl = curl_easy_init();
  61. if(curl) {
  62. curl_easy_setopt(curl, CURLOPT_URL, URL);
  63. /* use the share object */
  64. curl_easy_setopt(curl, CURLOPT_SHARE, share);
  65. /* Perform the request, res will get the return code */
  66. res = curl_easy_perform(curl);
  67. /* always cleanup */
  68. curl_easy_cleanup(curl);
  69. /* Check for errors */
  70. if(res != CURLE_OK) {
  71. fprintf(stderr, "curl_easy_perform() failed: %s\n",
  72. curl_easy_strerror(res));
  73. goto test_cleanup;
  74. }
  75. }
  76. }
  77. test_cleanup:
  78. curl_share_cleanup(share);
  79. curl_global_cleanup();
  80. return res;
  81. }