From c054b8bfa7bde0c209dd3ef688d07553d6ed04fb Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Mon, 25 Jan 2010 23:46:27 +0000 Subject: Constantine Sapuntzakis provided initial thread abstraction layer --- lib/curl_threads.c | 124 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 lib/curl_threads.c (limited to 'lib/curl_threads.c') diff --git a/lib/curl_threads.c b/lib/curl_threads.c new file mode 100644 index 000000000..7c87c19a4 --- /dev/null +++ b/lib/curl_threads.c @@ -0,0 +1,124 @@ +/*************************************************************************** + * _ _ ____ _ + * Project ___| | | | _ \| | + * / __| | | | |_) | | + * | (__| |_| | _ <| |___ + * \___|\___/|_| \_\_____| + * + * Copyright (C) 1998 - 2010, Daniel Stenberg, , 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 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. + * + * $Id$ + ***************************************************************************/ +#include "setup.h" + +#if defined(USE_THREADS_POSIX) +# ifdef HAVE_PTHREAD_H +# include +# endif +#elif defined(USE_THREADS_WIN32) +# ifdef HAVE_PROCESS_H +# include +# endif +#endif + +#include "curl_threads.h" + +#define _MPRINTF_REPLACE /* use our functions only */ +#include + +#include "curl_memory.h" +/* The last #include file should be: */ +#include "memdebug.h" + +#if defined(USE_THREADS_POSIX) + +struct curl_actual_call { + unsigned int (*func)(void *); + void *arg; +}; + +static void *curl_thread_create_thunk(void *arg) +{ + struct curl_actual_call * ac = arg; + unsigned int (*func)(void *) = ac->func; + void *real_arg = ac->arg; + + free(ac); + + (*func)(real_arg); + + return 0; +} + +curl_thread_t Curl_thread_create(unsigned int (*func) (void*), void *arg) +{ + curl_thread_t t; + struct curl_actual_call *ac = malloc(sizeof(struct curl_actual_call)); + if(!ac) + return curl_thread_t_null; + + ac->func = func; + ac->arg = arg; + + if(pthread_create(&t, NULL, curl_thread_create_thunk, ac) != 0) { + free(ac); + return curl_thread_t_null; + } + + return t; +} + +void Curl_thread_destroy(curl_thread_t hnd) +{ + if(hnd != curl_thread_t_null) + pthread_detach(hnd); +} + +int Curl_thread_join(curl_thread_t *hnd) +{ + int ret = (pthread_join(*hnd, NULL) == 0); + + *hnd = curl_thread_t_null; + + return ret; +} + +#elif defined(USE_THREADS_WIN32) + +curl_thread_t Curl_thread_create(unsigned int (CURL_STDCALL *func) (void*), void *arg) +{ +#ifdef _WIN32_WCE + return CreateThread(NULL, 0, func, arg, 0, NULL); +#else + return (curl_thread_t)_beginthreadex(NULL, 0, func, arg, 0, NULL); +#endif +} + +void Curl_thread_destroy(curl_thread_t hnd) +{ + CloseHandle(hnd); +} + +int Curl_thread_join(curl_thread_t *hnd) +{ + int ret = (WaitForSingleObject(*hnd, INFINITE) == WAIT_OBJECT_0); + + Curl_thread_destroy(*hnd); + + *hnd = curl_thread_t_null; + + return ret; +} + +#endif /* USE_THREADS_* */ -- cgit v1.2.1 From 6ebd71d186a6437d3472bea991f4298263d3644f Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Fri, 29 Jan 2010 17:47:54 +0000 Subject: WIN32 fix, _beginthreadex() may return either 0 or -1L upon failure --- lib/curl_threads.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'lib/curl_threads.c') diff --git a/lib/curl_threads.c b/lib/curl_threads.c index 7c87c19a4..96304e440 100644 --- a/lib/curl_threads.c +++ b/lib/curl_threads.c @@ -101,7 +101,11 @@ curl_thread_t Curl_thread_create(unsigned int (CURL_STDCALL *func) (void*), void #ifdef _WIN32_WCE return CreateThread(NULL, 0, func, arg, 0, NULL); #else - return (curl_thread_t)_beginthreadex(NULL, 0, func, arg, 0, NULL); + curl_thread_t t; + t = (curl_thread_t)_beginthreadex(NULL, 0, func, arg, 0, NULL); + if((t == 0) || (t == (curl_thread_t)-1L)) + return curl_thread_t_null; + return t; #endif } -- cgit v1.2.1 From a07bc79117971b96ebf3188c0a34a73ee0a3609b Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Sun, 14 Feb 2010 19:40:18 +0000 Subject: removed trailing whitespace --- lib/curl_threads.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/curl_threads.c') diff --git a/lib/curl_threads.c b/lib/curl_threads.c index 96304e440..3c3eecd02 100644 --- a/lib/curl_threads.c +++ b/lib/curl_threads.c @@ -45,7 +45,7 @@ struct curl_actual_call { unsigned int (*func)(void *); - void *arg; + void *arg; }; static void *curl_thread_create_thunk(void *arg) -- 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 --- lib/curl_threads.c | 1 - 1 file changed, 1 deletion(-) (limited to 'lib/curl_threads.c') diff --git a/lib/curl_threads.c b/lib/curl_threads.c index 3c3eecd02..fd10bd487 100644 --- a/lib/curl_threads.c +++ b/lib/curl_threads.c @@ -18,7 +18,6 @@ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY * KIND, either express or implied. * - * $Id$ ***************************************************************************/ #include "setup.h" -- cgit v1.2.1 From b903186fa0189ff241d756d25d07fdfe9885ae49 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Wed, 20 Apr 2011 15:17:42 +0200 Subject: source cleanup: unify look, style and indent levels By the use of a the new lib/checksrc.pl script that checks that our basic source style rules are followed. --- lib/curl_threads.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'lib/curl_threads.c') diff --git a/lib/curl_threads.c b/lib/curl_threads.c index fd10bd487..e9636440f 100644 --- a/lib/curl_threads.c +++ b/lib/curl_threads.c @@ -5,7 +5,7 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * Copyright (C) 1998 - 2010, Daniel Stenberg, , et al. + * Copyright (C) 1998 - 2011, Daniel Stenberg, , et al. * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms @@ -95,7 +95,8 @@ int Curl_thread_join(curl_thread_t *hnd) #elif defined(USE_THREADS_WIN32) -curl_thread_t Curl_thread_create(unsigned int (CURL_STDCALL *func) (void*), void *arg) +curl_thread_t Curl_thread_create(unsigned int (CURL_STDCALL *func) (void*), + void *arg) { #ifdef _WIN32_WCE return CreateThread(NULL, 0, func, arg, 0, NULL); -- cgit v1.2.1 From f1586cb4775681810afd8e6626e7842d459f3b85 Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Tue, 26 Jul 2011 17:23:27 +0200 Subject: stdio.h, stdlib.h, string.h, stdarg.h and ctype.h inclusion done in setup_once.h --- lib/curl_threads.c | 1 + 1 file changed, 1 insertion(+) (limited to 'lib/curl_threads.c') diff --git a/lib/curl_threads.c b/lib/curl_threads.c index e9636440f..411ccc81d 100644 --- a/lib/curl_threads.c +++ b/lib/curl_threads.c @@ -19,6 +19,7 @@ * KIND, either express or implied. * ***************************************************************************/ + #include "setup.h" #if defined(USE_THREADS_POSIX) -- cgit v1.2.1