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