1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al.
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 http://curl.haxx.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
24 #include <curl/mprintf.h>
30 /* struct containing data of a thread */
42 static void my_lock(CURL *handle, curl_lock_data data, curl_lock_access laccess,
46 struct userdata *user = (struct userdata *)useptr;
52 case CURL_LOCK_DATA_SHARE:
55 case CURL_LOCK_DATA_DNS:
58 case CURL_LOCK_DATA_COOKIE:
61 case CURL_LOCK_DATA_SSL_SESSION:
65 fprintf(stderr, "lock: no such data: %d\n", (int)data);
68 printf("lock: %-6s [%s]: %d\n", what, user->text, user->counter);
73 static void my_unlock(CURL *handle, curl_lock_data data, void *useptr )
76 struct userdata *user = (struct userdata *)useptr;
79 case CURL_LOCK_DATA_SHARE:
82 case CURL_LOCK_DATA_DNS:
85 case CURL_LOCK_DATA_COOKIE:
88 case CURL_LOCK_DATA_SSL_SESSION:
92 fprintf(stderr, "unlock: no such data: %d\n", (int)data);
95 printf("unlock: %-6s [%s]: %d\n", what, user->text, user->counter);
99 /* the dummy thread function */
100 static void *fire(void *ptr)
103 struct Tdata *tdata = (struct Tdata*)ptr;
107 if ((curl = curl_easy_init()) == NULL) {
108 fprintf(stderr, "curl_easy_init() failed\n");
112 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
113 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
114 curl_easy_setopt(curl, CURLOPT_URL, tdata->url);
115 printf( "CURLOPT_SHARE\n" );
116 curl_easy_setopt(curl, CURLOPT_SHARE, tdata->share);
118 printf( "PERFORM\n" );
119 code = curl_easy_perform(curl);
120 if( code != CURLE_OK ) {
121 fprintf(stderr, "perform url '%s' repeat %d failed, curlcode %d\n",
122 tdata->url, i, (int)code);
125 printf( "CLEANUP\n" );
126 curl_easy_cleanup(curl);
135 CURLSHcode scode = CURLSHE_OK;
141 struct userdata user;
143 user.text = (char *)"Pigs in space";
146 printf( "GLOBAL_INIT\n" );
147 if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
148 fprintf(stderr, "curl_global_init() failed\n");
149 return TEST_ERR_MAJOR_BAD;
153 printf( "SHARE_INIT\n" );
154 if ((share = curl_share_init()) == NULL) {
155 fprintf(stderr, "curl_share_init() failed\n");
156 curl_global_cleanup();
157 return TEST_ERR_MAJOR_BAD;
160 if ( CURLSHE_OK == scode ) {
161 printf( "CURLSHOPT_LOCKFUNC\n" );
162 scode = curl_share_setopt( share, CURLSHOPT_LOCKFUNC, my_lock);
164 if ( CURLSHE_OK == scode ) {
165 printf( "CURLSHOPT_UNLOCKFUNC\n" );
166 scode = curl_share_setopt( share, CURLSHOPT_UNLOCKFUNC, my_unlock);
168 if ( CURLSHE_OK == scode ) {
169 printf( "CURLSHOPT_USERDATA\n" );
170 scode = curl_share_setopt( share, CURLSHOPT_USERDATA, &user);
172 if ( CURLSHE_OK == scode ) {
173 printf( "CURL_LOCK_DATA_SSL_SESSION\n" );
174 scode = curl_share_setopt( share, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION);
177 if ( CURLSHE_OK != scode ) {
178 fprintf(stderr, "curl_share_setopt() failed\n");
179 curl_share_cleanup(share);
180 curl_global_cleanup();
181 return TEST_ERR_MAJOR_BAD;
188 for (i=1; i<=THREADS; i++ ) {
190 /* set thread data */
194 /* simulate thread, direct call of "thread" function */
195 printf( "*** run %d\n",i );
200 /* fetch a another one */
201 printf( "*** run %d\n", i );
202 if ((curl = curl_easy_init()) == NULL) {
203 fprintf(stderr, "curl_easy_init() failed\n");
204 curl_share_cleanup(share);
205 curl_global_cleanup();
206 return TEST_ERR_MAJOR_BAD;
210 test_setopt( curl, CURLOPT_URL, url );
211 printf( "CURLOPT_SHARE\n" );
212 test_setopt( curl, CURLOPT_SHARE, share );
214 printf( "PERFORM\n" );
215 curl_easy_perform( curl );
217 /* try to free share, expect to fail because share is in use*/
218 printf( "try SHARE_CLEANUP...\n" );
219 scode = curl_share_cleanup( share );
220 if ( scode==CURLSHE_OK )
222 fprintf(stderr, "curl_share_cleanup succeed but error expected\n");
225 printf( "SHARE_CLEANUP failed, correct\n" );
230 /* clean up last handle */
231 printf( "CLEANUP\n" );
232 curl_easy_cleanup( curl );
235 printf( "SHARE_CLEANUP\n" );
236 scode = curl_share_cleanup( share );
237 if ( scode!=CURLSHE_OK )
238 fprintf(stderr, "curl_share_cleanup failed, code errno %d\n",
241 printf( "GLOBAL_CLEANUP\n" );
242 curl_global_cleanup();