aboutsummaryrefslogtreecommitdiff
path: root/lib/share.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/share.c')
-rw-r--r--lib/share.c177
1 files changed, 79 insertions, 98 deletions
diff --git a/lib/share.c b/lib/share.c
index f0203f1a9..4d12b66e5 100644
--- a/lib/share.c
+++ b/lib/share.c
@@ -24,141 +24,122 @@
#include "setup.h"
#include <stdlib.h>
#include <curl/curl.h>
-#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<<type);
+ break;
+
+ case CURLSHOPT_UNSHARE:
+ /* this is a type this share will no longer share */
+ type = va_arg(param, int);
+ share->specifier &= ~(1<<type);
+ break;
+
+ case CURLSHOPT_LOCKFUNC:
+ lockfunc = va_arg(param, curl_lock_function);
+ share->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;
-}
-
-CURLcode
-curl_share_set_lock_function (curl_share *share, curl_lock_function lock)
-{
- if (CURL_SHARE_IS_DIRTY(share)) {
- return CURLE_SHARE_IN_USE;
- }
-
- share->lockfunc = lock;
- return CURLE_OK;
+ return CURLSHE_OK;
}
-CURLcode
-curl_share_set_unlock_function (curl_share *share, curl_unlock_function unlock)
+CURLSHcode curl_share_cleanup(CURLSH *sh)
{
- 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;
- share->unlockfunc = unlock;
- return CURLE_OK;
+ free (share);
+
+ return CURLSHE_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)
+CURLSHcode
+Curl_share_acquire_lock(struct SessionHandle *data, curl_lock_data type)
{
- curl_share *share = CURL_SHARE_GET (handle);
- if (share == NULL) {
- return SHARE_ERROR_INVALID;
- }
+ struct Curl_share *share = data->share;
- if (! (share->specifier & type)) {
- return SHARE_ERROR_NOT_REGISTERED;
- }
+ if (share == NULL)
+ return CURLSHE_INVALID;
- if (CURL_SHARE_IS_LOCKED (share, type)) {
- return SHARE_ERROR_OK;
+ if(share->specifier & (1<<type)) {
+ share->lockfunc (data, type, CURL_LOCK_ACCESS_SINGLE, share->clientdata);
+ share->locked |= (1<<type);
}
+ /* else if we don't share this, pretend successful lock */
- share->lockfunc (handle, type, share->clientdata);
- CURL_SHARE_SET_LOCKED (share, type);
-
- return SHARE_ERROR_OK;
+ return CURLSHE_OK;
}
-Curl_share_error
-Curl_share_release_lock (CURL *handle, curl_lock_type type)
+CURLSHcode
+Curl_share_release_lock(struct SessionHandle *data, curl_lock_data type)
{
- curl_share *share = CURL_SHARE_GET(handle);
- if (share == NULL) {
- return SHARE_ERROR_INVALID;
- }
+ struct Curl_share *share = data->share;
- if (! (share->specifier & type)) {
- return SHARE_ERROR_NOT_REGISTERED;
- }
+ if (share == NULL)
+ return CURLSHE_INVALID;
- if (!CURL_SHARE_IS_LOCKED (share, type)) {
- return SHARE_ERROR_OK;
+ if(share->specifier & (1<<type)) {
+ share->unlockfunc (data, type, share->clientdata);
+ share->locked &= ~(1<<type);
}
- share->unlockfunc (handle, type, share->clientdata);
- CURL_SHARE_SET_UNLOCKED (share, type);
-
- return SHARE_ERROR_OK;
+ return CURLSHE_OK;
}
-CURLcode curl_share_destroy (curl_share *share)
-{
- if (CURL_SHARE_IS_DIRTY(share)) {
- return CURLE_SHARE_IN_USE;
- }
-
- free (share);
-
- return CURLE_OK;
-}
/*
* local variables: