share: concurrency handling, easy updates

Replace the `volatile int dirty` with a reference counter
protected by a mutex when available.

Solve the problem of when to call application's lock function
by adding a volatile flag that indicates a share has been added
to easy handles in its lifetime. That flag ever goes from
FALSE to TRUE, so volatile might work (in the absence of a mutex).

(The problem is that the lock/unlock functions need 2-3
`curl_share_setopt()` invocations to become usable and there
is no way of telling if the third will ever happen. Calling
the lock function before the 3rd setopt may crash the
application.)

When removing a share from an easy handle (or replacing it with
another share), detach the easy connection on a share with a
connection pool.

When cleaning up a share, allow this even if it is still used in
easy handles. It will be destroyed when the reference count
drops to 0.

Closes #20870
This commit is contained in:
Stefan Eissing 2026-03-09 15:40:34 +01:00 committed by Daniel Stenberg
parent 745344ea4e
commit 82009c4220
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
26 changed files with 378 additions and 210 deletions

View file

@ -220,7 +220,7 @@
/* ================================================================ */
/* Give calloc a chance to be dragging in early, so we do not redefine */
#if defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)
#if defined(HAVE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)
# include <pthread.h>
#endif
@ -442,7 +442,7 @@
# if !(defined(__NEWLIB__) || \
(defined(__CLIB2__) && defined(__THREAD_SAFE)))
/* disable threaded resolver with clib2 - requires newlib or clib-ts */
# undef USE_THREADS_POSIX
# undef USE_RESOLV_THREADED
# endif
# endif
# include <exec/types.h>
@ -685,6 +685,16 @@
#endif /* _WIN32 */
/* We want to use mutex when available. */
#if defined(HAVE_THREADS_POSIX) || defined(_WIN32)
#define USE_MUTEX
#endif
/* threaded resolver is the only feature requiring threads. */
#ifdef USE_RESOLV_THREADED
#define USE_THREADS
#endif
/* ---------------------------------------------------------------- */
/* resolver specialty compile-time defines */
/* CURLRES_* defines to use in the host*.c sources */
@ -702,12 +712,10 @@
# define CURLRES_IPV4
#endif
#if defined(USE_THREADS_POSIX) || defined(USE_THREADS_WIN32)
#ifdef USE_RESOLV_THREADED
# define CURLRES_ASYNCH
# define CURLRES_THREADED
#elif defined(USE_ARES)
#elif defined(USE_RESOLV_ARES)
# define CURLRES_ASYNCH
# define CURLRES_ARES
/* now undef the stock libc functions to avoid them being used */
# undef HAVE_GETADDRINFO
# undef HAVE_FREEADDRINFO