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