From 454e840590eef557c114b32edb14c113f165272a Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Sun, 3 Feb 2008 12:28:48 +0000 Subject: threaded-ssl.c is a little example that does multi-threaded downloads from HTTPS sites with OpenSSL-enabled libcurl (and pthreads) and thus do the thread-locking and things openssl-style. --- docs/examples/Makefile.am | 2 +- docs/examples/threaded-ssl.c | 124 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 docs/examples/threaded-ssl.c (limited to 'docs') diff --git a/docs/examples/Makefile.am b/docs/examples/Makefile.am index 73383131e..16f3152b5 100644 --- a/docs/examples/Makefile.am +++ b/docs/examples/Makefile.am @@ -33,5 +33,5 @@ noinst_PROGRAMS = 10-at-a-time anyauthput cookie_interface \ COMPLICATED_EXAMPLES = \ curlgtk.c curlx.c htmltitle.cc cacertinmem.c ftpuploadresume.c \ ghiper.c hiperfifo.c htmltidy.c multithread.c \ - opensslthreadlock.c sampleconv.c synctime.c + opensslthreadlock.c sampleconv.c synctime.c threaded-ssl.c diff --git a/docs/examples/threaded-ssl.c b/docs/examples/threaded-ssl.c new file mode 100644 index 000000000..e49443d40 --- /dev/null +++ b/docs/examples/threaded-ssl.c @@ -0,0 +1,124 @@ +/***************************************************************************** + * _ _ ____ _ + * Project ___| | | | _ \| | + * / __| | | | |_) | | + * | (__| |_| | _ <| |___ + * \___|\___/|_| \_\_____| + * + * $Id$ + * + * A multi-threaded example that uses pthreads and fetches 4 remote files at + * once over HTTPS. The lock callbacks and stuff assume OpenSSL so far. + * Should be expanded to do optional GnuTLS style locking. + * + * OpenSSL docs for this: http://www.openssl.org/docs/crypto/threads.html + */ + +#include +#include +#include +#include + +/* we have this global to let the callback get easy access to it */ +static pthread_mutex_t *lockarray; + +static void lock_callback(int mode, int type, char *file, int line) +{ + (void)file; + (void)line; + if (mode & CRYPTO_LOCK) { + pthread_mutex_lock(&(lockarray[type])); + } + else { + pthread_mutex_unlock(&(lockarray[type])); + } +} + +static unsigned long thread_id(void) +{ + unsigned long ret; + + ret=(unsigned long)pthread_self(); + return(ret); +} + +static void init_locks(void) +{ + int i; + + lockarray=(pthread_mutex_t *)OPENSSL_malloc(CRYPTO_num_locks() * + sizeof(pthread_mutex_t)); + for (i=0; i