From e4c0a85da03b1b9070fc93e06ec989d1dce33fed Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Mon, 9 May 2005 21:12:03 +0000 Subject: Jeremy Brown's OpenSSL thread-locking example --- docs/examples/opensslthreadlock.c | 77 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 docs/examples/opensslthreadlock.c (limited to 'docs/examples/opensslthreadlock.c') diff --git a/docs/examples/opensslthreadlock.c b/docs/examples/opensslthreadlock.c new file mode 100644 index 000000000..82de206da --- /dev/null +++ b/docs/examples/opensslthreadlock.c @@ -0,0 +1,77 @@ +/***************************************************************************** + * _ _ ____ _ + * Project ___| | | | _ \| | + * / __| | | | |_) | | + * | (__| |_| | _ <| |___ + * \___|\___/|_| \_\_____| + * + * $Id$ + * + * Example source code to show one way to set the necessary OpenSSL locking + * callbacks if you want to do multi-threaded transfers with HTTPS/FTPS with + * libcurl built to use OpenSSL. + * + * This is not a complete stand-alone example. + * + * Author: Jeremy Brown + */ + +#define MUTEX_TYPE pthread_mutex_t +#define MUTEX_SETUP(x) pthread_mutex_init(&(x), NULL) +#define MUTEX_CLEANUP(x) pthread_mutex_destroy(&(x)) +#define MUTEX_LOCK(x) pthread_mutex_lock(&(x)) +#define MUTEX_UNLOCK(x) pthread_mutex_unlock(&(x)) +#define THREAD_ID pthread_self( ) + + +void handle_error(const char *file, int lineno, const char *msg){ + fprintf(stderr, ** %s:%i %s\n, file, lineno, msg); + ERR_print_errors_fp(stderr); + /* exit(-1); */ + } + +/* This array will store all of the mutexes available to OpenSSL. */ +static MUTEX_TYPE *mutex_buf= NULL; + + +static void locking_function(int mode, int n, const char * file, int line) +{ + if (mode & CRYPTO_LOCK) + MUTEX_LOCK(mutex_buf[n]); + else + MUTEX_UNLOCK(mutex_buf[n]); +} + +static unsigned long id_function(void) +{ + return ((unsigned long)THREAD_ID); +} + +int thread_setup(void) +{ + int i; + + mutex_buf = (MUTEX_TYPE *)malloc(CRYPTO_num_locks( ) * sizeof(MUTEX_TYPE)); + if (!mutex_buf) + return 0; + for (i = 0; i < CRYPTO_num_locks( ); i++) + MUTEX_SETUP(mutex_buf[i]); + CRYPTO_set_id_callback(id_function); + CRYPTO_set_locking_callback(locking_function); + return 1; +} + +int thread_cleanup(void) +{ + int i; + + if (!mutex_buf) + return 0; + CRYPTO_set_id_callback(NULL); + CRYPTO_set_locking_callback(NULL); + for (i = 0; i < CRYPTO_num_locks( ); i++) + MUTEX_CLEANUP(mutex_buf[i]); + free(mutex_buf); + mutex_buf = NULL; + return 1; +} -- cgit v1.2.1 From 49ce3e5160a9576e797bf87cef012b09d1c54ecb Mon Sep 17 00:00:00 2001 From: Dan Fandrich Date: Thu, 12 Jul 2007 21:11:10 +0000 Subject: Fixed some compile warnings and errors and improved portability in the examples. Removed ftp3rdparty.c since libcurl doesn't support 3rd party FTP transfers any longer. --- docs/examples/opensslthreadlock.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'docs/examples/opensslthreadlock.c') diff --git a/docs/examples/opensslthreadlock.c b/docs/examples/opensslthreadlock.c index 82de206da..18a2f77fc 100644 --- a/docs/examples/opensslthreadlock.c +++ b/docs/examples/opensslthreadlock.c @@ -16,6 +16,11 @@ * Author: Jeremy Brown */ + +#include +#include +#include + #define MUTEX_TYPE pthread_mutex_t #define MUTEX_SETUP(x) pthread_mutex_init(&(x), NULL) #define MUTEX_CLEANUP(x) pthread_mutex_destroy(&(x)) @@ -25,7 +30,7 @@ void handle_error(const char *file, int lineno, const char *msg){ - fprintf(stderr, ** %s:%i %s\n, file, lineno, msg); + fprintf(stderr, "** %s:%d %s\n", file, lineno, msg); ERR_print_errors_fp(stderr); /* exit(-1); */ } -- cgit v1.2.1 From 59e378f48fed849e8e41f0bc6a10bf7a1732ae8a Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Sat, 6 Sep 2008 05:29:05 +0000 Subject: remove unnecessary typecasting of malloc() --- docs/examples/opensslthreadlock.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'docs/examples/opensslthreadlock.c') diff --git a/docs/examples/opensslthreadlock.c b/docs/examples/opensslthreadlock.c index 18a2f77fc..3ac7124b0 100644 --- a/docs/examples/opensslthreadlock.c +++ b/docs/examples/opensslthreadlock.c @@ -56,7 +56,7 @@ int thread_setup(void) { int i; - mutex_buf = (MUTEX_TYPE *)malloc(CRYPTO_num_locks( ) * sizeof(MUTEX_TYPE)); + mutex_buf = malloc(CRYPTO_num_locks( ) * sizeof(MUTEX_TYPE)); if (!mutex_buf) return 0; for (i = 0; i < CRYPTO_num_locks( ); i++) -- cgit v1.2.1 From 2309b4e330b96bc2e1f8e36b6184015e59544037 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Wed, 24 Mar 2010 11:02:54 +0100 Subject: remove the CVSish $Id$ lines --- docs/examples/opensslthreadlock.c | 1 - 1 file changed, 1 deletion(-) (limited to 'docs/examples/opensslthreadlock.c') diff --git a/docs/examples/opensslthreadlock.c b/docs/examples/opensslthreadlock.c index 3ac7124b0..706e90121 100644 --- a/docs/examples/opensslthreadlock.c +++ b/docs/examples/opensslthreadlock.c @@ -5,7 +5,6 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * $Id$ * * Example source code to show one way to set the necessary OpenSSL locking * callbacks if you want to do multi-threaded transfers with HTTPS/FTPS with -- cgit v1.2.1 From 1aeb635cdd296c16acb375a4a83a78f13166ccab Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Thu, 10 Mar 2011 11:48:02 +0100 Subject: sources: update source headers All C and H files now (should) feature the proper project curl source code header, which includes basic info, a copyright statement and some basic disclaimers. --- docs/examples/opensslthreadlock.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'docs/examples/opensslthreadlock.c') diff --git a/docs/examples/opensslthreadlock.c b/docs/examples/opensslthreadlock.c index 706e90121..ad54f08ea 100644 --- a/docs/examples/opensslthreadlock.c +++ b/docs/examples/opensslthreadlock.c @@ -1,12 +1,25 @@ -/***************************************************************************** +/*************************************************************************** * _ _ ____ _ * Project ___| | | | _ \| | * / __| | | | |_) | | * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * + * Copyright (C) 1998 - 2011, Daniel Stenberg, , et al. * - * Example source code to show one way to set the necessary OpenSSL locking + * 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 http://curl.haxx.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. + * + ***************************************************************************/ +/* Example source code to show one way to set the necessary OpenSSL locking * callbacks if you want to do multi-threaded transfers with HTTPS/FTPS with * libcurl built to use OpenSSL. * -- cgit v1.2.1