lib1541.c 3.9 KB

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