lib1541.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2019 - 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. /*
  23. * KNOW_BUGS "A shared connection cache is not thread-safe"
  24. *
  25. * This source code was used to verify shared connection cache but since this
  26. * is a known issue the test is no longer built or run. This code is here to
  27. * allow for testing once someone gets to work on fixing this.
  28. */
  29. #include "test.h"
  30. #include "testutil.h"
  31. #include "warnless.h"
  32. #include "memdebug.h"
  33. #ifdef HAVE_PTHREAD_H
  34. #include <pthread.h>
  35. #include <time.h>
  36. /* number of threads to fire up in parallel */
  37. #define NUM_THREADS 67
  38. /* for how many seconds each thread will loop */
  39. #define RUN_FOR_SECONDS 7
  40. static pthread_mutex_t connlock;
  41. static size_t write_db(void *ptr, size_t size, size_t nmemb, void *data)
  42. {
  43. /* not interested in the downloaded bytes, return the size */
  44. (void)ptr; /* unused */
  45. (void)data; /* unused */
  46. return (size_t)(size * nmemb);
  47. }
  48. static void lock_cb(CURL *handle, curl_lock_data data,
  49. curl_lock_access access, void *userptr)
  50. {
  51. (void)access; /* unused */
  52. (void)userptr; /* unused */
  53. (void)handle; /* unused */
  54. (void)data; /* unused */
  55. pthread_mutex_lock(&connlock);
  56. }
  57. static void unlock_cb(CURL *handle, curl_lock_data data,
  58. void *userptr)
  59. {
  60. (void)userptr; /* unused */
  61. (void)handle; /* unused */
  62. (void)data; /* unused */
  63. pthread_mutex_unlock(&connlock);
  64. }
  65. static void init_locks(void)
  66. {
  67. pthread_mutex_init(&connlock, NULL);
  68. }
  69. static void kill_locks(void)
  70. {
  71. pthread_mutex_destroy(&connlock);
  72. }
  73. struct initurl {
  74. const char *url;
  75. CURLSH *share;
  76. int threadno;
  77. };
  78. static void *run_thread(void *ptr)
  79. {
  80. struct initurl *u = (struct initurl *)ptr;
  81. int i;
  82. time_t end = time(NULL) + RUN_FOR_SECONDS;
  83. for(i = 0; time(NULL) < end; i++) {
  84. CURL *curl = curl_easy_init();
  85. curl_easy_setopt(curl, CURLOPT_URL, u->url);
  86. curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L);
  87. curl_easy_setopt(curl, CURLOPT_SHARE, u->share);
  88. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_db);
  89. curl_easy_perform(curl); /* ignores error */
  90. curl_easy_cleanup(curl);
  91. fprintf(stderr, "Thread %d transfer %d\n", u->threadno, i);
  92. }
  93. return NULL;
  94. }
  95. int test(char *URL)
  96. {
  97. pthread_t tid[NUM_THREADS];
  98. int i;
  99. CURLSH *share;
  100. struct initurl url[NUM_THREADS];
  101. /* Must initialize libcurl before any threads are started */
  102. curl_global_init(CURL_GLOBAL_ALL);
  103. share = curl_share_init();
  104. curl_share_setopt(share, CURLSHOPT_LOCKFUNC, lock_cb);
  105. curl_share_setopt(share, CURLSHOPT_UNLOCKFUNC, unlock_cb);
  106. curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT);
  107. init_locks();
  108. for(i = 0; i< NUM_THREADS; i++) {
  109. int error;
  110. url[i].url = URL;
  111. url[i].share = share;
  112. url[i].threadno = i;
  113. error = pthread_create(&tid[i], NULL, run_thread, &url[i]);
  114. if(0 != error)
  115. fprintf(stderr, "Couldn't run thread number %d, errno %d\n", i, error);
  116. else
  117. fprintf(stderr, "Thread %d, gets %s\n", i, URL);
  118. }
  119. /* now wait for all threads to terminate */
  120. for(i = 0; i< NUM_THREADS; i++) {
  121. pthread_join(tid[i], NULL);
  122. fprintf(stderr, "Thread %d terminated\n", i);
  123. }
  124. kill_locks();
  125. curl_share_cleanup(share);
  126. curl_global_cleanup();
  127. return 0;
  128. }
  129. #else /* without pthread, this test doesn't work */
  130. int test(char *URL)
  131. {
  132. (void)URL;
  133. return 0;
  134. }
  135. #endif