From f152f23a680670e72e230d6247a2ed4552750480 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Thu, 9 Jan 2003 10:21:03 +0000 Subject: [PATCH] Updated more and now looks and and the API possibly works almost like the design document specifies. There is still no code inside that uses this. --- lib/share.c | 205 ++++++++++++++++++++++++---------------------------- lib/share.h | 11 +-- 2 files changed, 95 insertions(+), 121 deletions(-) diff --git a/lib/share.c b/lib/share.c index f0203f1a9..4d12b66e5 100644 --- a/lib/share.c +++ b/lib/share.c @@ -24,142 +24,123 @@ #include "setup.h" #include #include -#include "share.h" #include "urldata.h" +#include "share.h" /* The last #include file should be: */ #ifdef MALLOCDEBUG #include "memdebug.h" #endif -#define CURL_SHARE_SET_LOCKED(__share, __type) ((__share)->locked += (__type)) -#define CURL_SHARE_SET_UNLOCKED(__share, __type) ((__share)->locked -= (__type)) - -#define CURL_SHARE_SET_USED(__share, __type) ((__share)->specifier += (__type)) -#define CURL_SHARE_SET_UNUSED(__share, __type) ((__share)->specifier -= (__type)) -#define CURL_SHARE_IS_USED(__share, __type) ((__share)->specifier & (__type)) -#define CURL_SHARE_IS_LOCKED(__share, __type) ((__share)->locked & (__type)) - -#define CURL_SHARE_IS_DIRTY(__share) ((__share)->dirty) - -#define CURL_SHARE_GET(__handle) (((struct SessionHandle *) (__handle))->share) - CURLSH * -curl_share_init (void) +curl_share_init(void) { - curl_share *share = (curl_share *) malloc (sizeof (curl_share)); - if (share) { - memset (share, 0, sizeof (curl_share)); - } + struct Curl_share *share = + (struct Curl_share *)malloc(sizeof(struct Curl_share)); + if (share) + memset (share, 0, sizeof(struct Curl_share)); return share; } -CURLcode -curl_share_setopt (curl_share *share, curl_lock_type option, int enable) +CURLSHcode +curl_share_setopt(CURLSH *sh, CURLSHoption option, ...) { - if (CURL_SHARE_IS_DIRTY(share)) { - return CURLE_SHARE_IN_USE; + struct Curl_share *share = (struct Curl_share *)sh; + va_list param; + int type; + curl_lock_function lockfunc; + curl_unlock_function unlockfunc; + void *ptr; + + if (share->dirty) + /* don't allow setting options while one or more handles are already + using this share */ + return CURLSHE_IN_USE; + + va_start(param, option); + + switch(option) { + case CURLSHOPT_SHARE: + /* this is a type this share will share */ + type = va_arg(param, int); + share->specifier |= (1<specifier &= ~(1<lockfunc = lockfunc; + break; + + case CURLSHOPT_UNLOCKFUNC: + unlockfunc = va_arg(param, curl_unlock_function); + share->unlockfunc = unlockfunc; + break; + + case CURLSHOPT_USERDATA: + ptr = va_arg(param, void *); + share->clientdata = ptr; + break; + + default: + return CURLSHE_BAD_OPTION; } - if (enable) { - CURL_SHARE_SET_USED (share, option); - } - else { - CURL_SHARE_SET_UNUSED (share, option); - } - - return CURLE_OK; + return CURLSHE_OK; } -CURLcode -curl_share_set_lock_function (curl_share *share, curl_lock_function lock) +CURLSHcode curl_share_cleanup(CURLSH *sh) { - if (CURL_SHARE_IS_DIRTY(share)) { - return CURLE_SHARE_IN_USE; - } - - share->lockfunc = lock; - return CURLE_OK; -} - -CURLcode -curl_share_set_unlock_function (curl_share *share, curl_unlock_function unlock) -{ - if (CURL_SHARE_IS_DIRTY(share)) { - return CURLE_SHARE_IN_USE; - } - - share->unlockfunc = unlock; - return CURLE_OK; -} - -CURLcode -curl_share_set_lock_data (curl_share *share, void *data) -{ - if (CURL_SHARE_IS_DIRTY(share)) { - return CURLE_SHARE_IN_USE; - } - - share->clientdata = data; - return CURLE_OK; -} - -Curl_share_error -Curl_share_acquire_lock (CURL *handle, curl_lock_type type) -{ - curl_share *share = CURL_SHARE_GET (handle); - if (share == NULL) { - return SHARE_ERROR_INVALID; - } - - if (! (share->specifier & type)) { - return SHARE_ERROR_NOT_REGISTERED; - } - - if (CURL_SHARE_IS_LOCKED (share, type)) { - return SHARE_ERROR_OK; - } - - share->lockfunc (handle, type, share->clientdata); - CURL_SHARE_SET_LOCKED (share, type); - - return SHARE_ERROR_OK; -} - -Curl_share_error -Curl_share_release_lock (CURL *handle, curl_lock_type type) -{ - curl_share *share = CURL_SHARE_GET(handle); - if (share == NULL) { - return SHARE_ERROR_INVALID; - } - - if (! (share->specifier & type)) { - return SHARE_ERROR_NOT_REGISTERED; - } - - if (!CURL_SHARE_IS_LOCKED (share, type)) { - return SHARE_ERROR_OK; - } - - share->unlockfunc (handle, type, share->clientdata); - CURL_SHARE_SET_UNLOCKED (share, type); - - return SHARE_ERROR_OK; -} - -CURLcode curl_share_destroy (curl_share *share) -{ - if (CURL_SHARE_IS_DIRTY(share)) { - return CURLE_SHARE_IN_USE; - } + struct Curl_share *share = (struct Curl_share *)sh; + if (share->dirty) + return CURLSHE_IN_USE; free (share); - return CURLE_OK; + return CURLSHE_OK; } + +CURLSHcode +Curl_share_acquire_lock(struct SessionHandle *data, curl_lock_data type) +{ + struct Curl_share *share = data->share; + + if (share == NULL) + return CURLSHE_INVALID; + + if(share->specifier & (1<lockfunc (data, type, CURL_LOCK_ACCESS_SINGLE, share->clientdata); + share->locked |= (1<share; + + if (share == NULL) + return CURLSHE_INVALID; + + if(share->specifier & (1<unlockfunc (data, type, share->clientdata); + share->locked &= ~(1< -typedef enum { - SHARE_ERROR_OK = 0, - SHARE_ERROR_INVALID, - SHARE_ERROR_NOT_REGISTERED, - SHARE_ERROR_LAST -} Curl_share_error; - /* this struct is libcurl-private, don't export details */ struct Curl_share { unsigned int specifier; @@ -45,8 +38,8 @@ struct Curl_share { void *clientdata; }; -Curl_share_error Curl_share_aquire_lock (CURL *, curl_lock_data); -Curl_share_error Curl_share_release_lock (CURL *, curl_lock_data); +CURLSHcode Curl_share_aquire_lock (struct SessionHandle *, curl_lock_data); +CURLSHcode Curl_share_release_lock (struct SessionHandle *, curl_lock_data); #endif /* __CURL_SHARE_H */