*
* curl_global_init() should be invoked exactly once for each application that
* uses libcurl and before any call of other libcurl functions.
- *
- * This function is not thread-safe!
+
+ * This function is thread-safe if CURL_VERSION_THREADSAFE_INIT is set in the
+ * curl_version_info_data.features flag (fetch by curl_version_info()).
+
*/
CURL_EXTERN CURLcode curl_global_init(long flags);
#define CURL_VERSION_UNICODE (1<<27) /* Unicode support on Windows */
#define CURL_VERSION_HSTS (1<<28) /* HSTS is supported */
#define CURL_VERSION_GSASL (1<<29) /* libgsasl is supported */
+#define CURL_VERSION_THREADSAFE_INIT (1<<30) /* curl_global_init/cleanup() are
+ thread-safe */
/*
* NAME curl_version_info()
lib1915 lib1916 lib1917 lib1918 lib1919 \
lib1933 lib1934 lib1935 lib1936 lib1937 lib1938 lib1939 lib1940 \
lib1945 lib1946 \
- lib3010 lib3025
+ lib3010 lib3025 lib3026
chkdecimalpoint_SOURCES = chkdecimalpoint.c ../../lib/mprintf.c \
../../lib/curl_ctype.c ../../lib/dynbuf.c ../../lib/strdup.c
lib3025_SOURCES = lib3025.c $(SUPPORTFILES) $(TESTUTIL) $(WARNLESS)
lib3025_LDADD = $(TESTUTIL_LIBS)
lib3025_CPPFLAGS = $(AM_CPPFLAGS)
+
+lib3026_SOURCES = lib3026.c $(SUPPORTFILES) $(TESTUTIL) $(WARNLESS)
+lib3026_LDADD = $(TESTUTIL_LIBS)
+lib3026_CPPFLAGS = $(AM_CPPFLAGS)
--- /dev/null
+/***************************************************************************
+ * _ _ ____ _
+ * Project ___| | | | _ \| |
+ * / __| | | | |_) | |
+ * | (__| |_| | _ <| |___
+ * \___|\___/|_| \_\_____|
+ *
+ * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
+ *
+ * This software is licensed as described in the file COPYING, which
+ * you should have received as part of this distribution. The terms
+ * are also available at https://curl.se/docs/copyright.html.
+ *
+ * You may opt to use, copy, modify, merge, publish, distribute and/or sell
+ * copies of the Software, and permit persons to whom the Software is
+ * furnished to do so, under the terms of the COPYING file.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ***************************************************************************/
+#include "test.h"
+
+#include "testutil.h"
+#include "warnless.h"
+
+#ifdef HAVE_PTHREAD_H
+#include <pthread.h>
+#include <unistd.h>
+
+#define NUM_THREADS 1000
+
+static void *run_thread(void *ptr)
+{
+ CURLcode *result = ptr;
+
+ *result = curl_global_init(CURL_GLOBAL_ALL);
+ if(*result == CURLE_OK)
+ curl_global_cleanup();
+
+ return NULL;
+}
+
+int test(char *URL)
+{
+ CURLcode results[NUM_THREADS];
+ pthread_t tids[NUM_THREADS];
+ unsigned tid_count = NUM_THREADS, i;
+ int test_failure = 0;
+ curl_version_info_data *ver;
+ (void) URL;
+
+ ver = curl_version_info(CURLVERSION_NOW);
+ if((ver->features & CURL_VERSION_THREADSAFE_INIT) == 0) {
+ fprintf(stderr, "%s:%d Have pthread but the "
+ "CURL_VERSION_THREADSAFE_INIT feature flag is not set\n",
+ __FILE__, __LINE__);
+ return -1;
+ }
+
+ for(i = 0; i < tid_count; i++) {
+ int res = pthread_create(&tids[i], NULL, run_thread, &results[i]);
+ if(res) {
+ fprintf(stderr, "%s:%d Couldn't create thread, errno %d\n",
+ __FILE__, __LINE__, res);
+ tid_count = i;
+ test_failure = -1;
+ goto cleanup;
+ }
+ }
+
+cleanup:
+ for(i = 0; i < tid_count; i++) {
+ pthread_join(tids[i], NULL);
+ if(results[i] != CURLE_OK) {
+ fprintf(stderr, "%s:%d thread[%u]: curl_global_init() failed,"
+ "with code %d (%s)\n", __FILE__, __LINE__,
+ i, (int) results[i], curl_easy_strerror(results[i]));
+ test_failure = -1;
+ }
+ }
+
+ return test_failure;
+}
+
+#else /* without pthread, this test doesn't work */
+int test(char *URL)
+{
+ curl_version_info_data *ver;
+ (void)URL;
+
+ ver = curl_version_info(CURLVERSION_NOW);
+ if((ver->features & CURL_VERSION_THREADSAFE_INIT) != 0) {
+ fprintf(stderr, "%s:%d No pthread but the "
+ "CURL_VERSION_THREADSAFE_INIT feature flag is set\n",
+ __FILE__, __LINE__);
+ return -1;
+ }
+ return 0;
+}
+#endif