summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBryan Ischo <bryan@ischo.com>2008-08-12 12:26:56 +0000
committerBryan Ischo <bryan@ischo.com>2008-08-12 12:26:56 +0000
commit953d590bd9a3508d37552efb89aaa68344466d80 (patch)
treef907dd0ae53614b641c84007d630401a8e1eab1e
parent2cda77ecda9c93a03329256744b19a237f92b810 (diff)
downloadceph-libs3-953d590bd9a3508d37552efb89aaa68344466d80.tar.gz
* Remove the threading callbacks in favor of using pthreads API. Will have
to implement relevent pthreads API pieces on Windows now to restore the Windows build.
-rw-r--r--GNUmakefile2
-rw-r--r--inc/libs3.h86
-rw-r--r--src/general.c87
-rw-r--r--src/request.c19
-rw-r--r--src/s3.c2
5 files changed, 34 insertions, 162 deletions
diff --git a/GNUmakefile b/GNUmakefile
index 27aecc6..70e8b37 100644
--- a/GNUmakefile
+++ b/GNUmakefile
@@ -90,7 +90,7 @@ CFLAGS += -Wall -Werror -std=c99 -Iinc $(CURL_CFLAGS) $(LIBXML2_CFLAGS) \
-DLIBS3_VER_MAJOR=\"$(LIBS3_VER_MAJOR)\" \
-DLIBS3_VER_MINOR=\"$(LIBS3_VER_MINOR)\"
-LDFLAGS = $(CURL_LIBS) $(LIBXML2_LIBS)
+LDFLAGS = $(CURL_LIBS) $(LIBXML2_LIBS) -lpthread
# --------------------------------------------------------------------------
diff --git a/inc/libs3.h b/inc/libs3.h
index 92942c1..5d2054b 100644
--- a/inc/libs3.h
+++ b/inc/libs3.h
@@ -216,7 +216,6 @@ typedef enum
S3StatusInternalError ,
S3StatusOutOfMemory ,
S3StatusInterrupted ,
- S3StatusFailedToCreateMutex ,
S3StatusInvalidBucketNameTooLong ,
S3StatusInvalidBucketNameFirstCharacter ,
S3StatusInvalidBucketNameCharacter ,
@@ -448,13 +447,6 @@ typedef enum
************************************************************************** **/
/**
- * This is a type which must be defined by the user of the S3 library. It
- * defines a Mutex type with standard Mutex semantics.
- **/
-struct S3Mutex;
-
-
-/**
* An S3RequestContext manages multiple S3 requests simultaneously; see the
* S3_XXX_request_context functions below for details
**/
@@ -844,58 +836,6 @@ typedef struct S3ErrorDetails
************************************************************************** **/
/**
- * This is the signature of a "thread self" callback, that must be provided to
- * the S3_initialize() method, and implemented by the user of the libs3
- * library. This function returns the thread id of the thread which calls it.
- *
- * @return the thread id of the thread which calls it
- **/
-typedef unsigned long (S3ThreadSelfCallback)();
-
-
-/**
- * This is the signature of a "mutex create" callback, that must be provided
- * to the S3_initialize() method, and implemented by the user of the libs3
- * library. This function returns a newly-created and initialized S3Mutex
- * structure (itself defined by the libs3 user).
- *
- * @return a newly-created and initialized S3Mutex structure
- **/
-typedef struct S3Mutex *(S3MutexCreateCallback)();
-
-
-/**
- * This is the signature of a "mutex lock" callback, that must be provided to
- * the S3_initialize() method, and implemented by the user of the libs3
- * library. This function locks a mutex.
- *
- * @param mutex is the S3Mutex to lock
- **/
-typedef void (S3MutexLockCallback)(struct S3Mutex *mutex);
-
-
-/**
- * This is the signature of a "mutex unlock" callback, that must be provided
- * to the S3_initialize() method, and implemented by the user of the libs3
- * library. This function unlocks a mutex.
- *
- * @param mutex is the S3Mutex to unlock
- **/
-typedef void (S3MutexUnlockCallback)(struct S3Mutex *mutex);
-
-
-/**
- * This is the signature of a "mutex destroy" callback, that must be provided
- * to the S3_initialize() method, and implemented by the user of the libs3
- * library. This function destroys a mutex previously created by a call to
- * S3MutexCreateCallback().
- *
- * @param mutex is the S3Mutex to destroy
- **/
-typedef void (S3MutexDestroyCallback)(struct S3Mutex *mutex);
-
-
-/**
* This callback is made whenever the response properties become available for
* any request.
*
@@ -1164,21 +1104,6 @@ typedef struct S3GetObjectHandler
* NULL or the empty string if you don't care about this. The value
* will not be copied by this function and must remain unaltered by the
* caller until S3_deinitialize() is called.
- * @param threadSelfCallback provides the callback for the S3 library to call
- * to identify the calling thread, or NULL if the caller is not a
- * multithreaded program.
- * @param mutexCreateCallback provides the callback for the S3 library to call
- * to create a mutex, or NULL if the caller is not a multithreaded
- * program.
- * @param mutexLockCallback provides the callback for the S3 library to call
- * to lock a mutex, or NULL if the caller is not a multithreaded
- * program.
- * @param mutexUnlockCallback provides the callback for the S3 library to call
- * to unlock a mutex, or NULL if the caller is not a multithreaded
- * program.
- * @param mutexDestroyCallback provides the callback for the S3 library to
- * call to destroy a mutex, or NULL if the caller is not a
- * multithreaded program.
* @param flags is a bitmask of some combination of S3_INIT_XXX flag, or
* S3_INIT_ALL, indicating which of the libraries that libs3 depends
* upon should be initialized by S3_initialize(). Only if your program
@@ -1198,17 +1123,8 @@ typedef struct S3GetObjectHandler
* S3StatusInternalError if dependent libraries could not be
* initialized
* S3StatusOutOfMemory on failure due to out of memory
- * S3StatusFailedToCreateMutex if the mutex creation function returned
- * NULL for one of the mutexes that are created during the
- * initialization process
**/
-S3Status S3_initialize(const char *userAgentInfo,
- S3ThreadSelfCallback *threadSelfCallback,
- S3MutexCreateCallback *mutexCreateCallback,
- S3MutexLockCallback *mutexLockCallback,
- S3MutexUnlockCallback *mutexUnlockCallback,
- S3MutexDestroyCallback *mutexDestroyCallback,
- int flags);
+S3Status S3_initialize(const char *userAgentInfo, int flags);
/**
diff --git a/src/general.c b/src/general.c
index 329b0a0..9904e5a 100644
--- a/src/general.c
+++ b/src/general.c
@@ -31,6 +31,7 @@
#ifndef OPENSSL_THREADS
#error "Threading support required in OpenSSL library, but not provided"
#endif
+#include <pthread.h>
#include <string.h>
#include "request.h"
#include "simplexml.h"
@@ -38,30 +39,33 @@
static int initializeCountG = 0;
-typedef struct S3Mutex CRYPTO_dynlock_value;
+typedef pthread_mutex_t CRYPTO_dynlock_value;
-static struct S3Mutex **pLocksG;
+static pthread_mutex_t *pLocksG;
-static S3MutexCreateCallback *mutexCreateCallbackG;
-static S3MutexLockCallback *mutexLockCallbackG;
-static S3MutexUnlockCallback *mutexUnlockCallbackG;
-static S3MutexDestroyCallback *mutexDestroyCallbackG;
+
+static unsigned long id_callback()
+{
+ return (unsigned long) pthread_self();
+}
static void locking_callback(int mode, int index, const char *file, int line)
{
if (mode & CRYPTO_LOCK) {
- mutex_lock(pLocksG[index]);
+ pthread_mutex_lock(&(pLocksG[index]));
}
else {
- mutex_unlock(pLocksG[index]);
+ pthread_mutex_unlock(&(pLocksG[index]));
}
}
static struct CRYPTO_dynlock_value *dynlock_create(const char *file, int line)
{
- return (struct CRYPTO_dynlock_value *) mutex_create();
+ pthread_mutex_t *ret = (pthread_mutex_t *) malloc(sizeof(pthread_mutex_t));
+ pthread_mutex_init(ret, 0);
+ return (struct CRYPTO_dynlock_value *) ret;
}
@@ -69,10 +73,10 @@ static void dynlock_lock(int mode, struct CRYPTO_dynlock_value *pLock,
const char *file, int line)
{
if (mode & CRYPTO_LOCK) {
- mutex_lock((struct S3Mutex *) pLock);
+ pthread_mutex_lock((pthread_mutex_t *) pLock);
}
else {
- mutex_unlock((struct S3Mutex *) pLock);
+ pthread_mutex_unlock((pthread_mutex_t *) pLock);
}
}
@@ -80,7 +84,8 @@ static void dynlock_lock(int mode, struct CRYPTO_dynlock_value *pLock,
static void dynlock_destroy(struct CRYPTO_dynlock_value *pLock,
const char *file, int line)
{
- mutex_destroy((struct S3Mutex *) pLock);
+ pthread_mutex_destroy((pthread_mutex_t *) pLock);
+ free(pLock);
}
@@ -94,79 +99,32 @@ static void deinitialize_locks()
int count = CRYPTO_num_locks();
for (int i = 0; i < count; i++) {
- mutex_destroy(pLocksG[i]);
+ pthread_mutex_destroy(&(pLocksG[i]));
}
free(pLocksG);
}
-struct S3Mutex *mutex_create()
-{
- return (mutexCreateCallbackG ?
- (*mutexCreateCallbackG)() : (struct S3Mutex *) 1);
-}
-
-
-void mutex_lock(struct S3Mutex *mutex)
-{
- if (mutexLockCallbackG) {
- (*mutexLockCallbackG)(mutex);
- }
-}
-
-
-void mutex_unlock(struct S3Mutex *mutex)
-{
- if (mutexUnlockCallbackG) {
- (*mutexUnlockCallbackG)(mutex);
- }
-}
-
-
-void mutex_destroy(struct S3Mutex *mutex)
-{
- if (mutexDestroyCallbackG) {
- (*mutexDestroyCallbackG)(mutex);
- }
-}
-
-
-S3Status S3_initialize(const char *userAgentInfo,
- S3ThreadSelfCallback *threadSelfCallback,
- S3MutexCreateCallback *mutexCreateCallback,
- S3MutexLockCallback *mutexLockCallback,
- S3MutexUnlockCallback *mutexUnlockCallback,
- S3MutexDestroyCallback *mutexDestroyCallback,
- int flags)
+S3Status S3_initialize(const char *userAgentInfo, int flags)
{
if (initializeCountG++) {
return S3StatusOK;
}
- mutexCreateCallbackG = mutexCreateCallback;
- mutexLockCallbackG = mutexLockCallback;
- mutexUnlockCallbackG = mutexUnlockCallback;
- mutexDestroyCallbackG = mutexDestroyCallback;
-
/* As required by the openssl library for thread support */
int count = CRYPTO_num_locks(), i;
if (!(pLocksG =
- (struct S3Mutex **) malloc(count * sizeof(struct S3Mutex *)))) {
+ (pthread_mutex_t *) malloc(count * sizeof(pthread_mutex_t)))) {
return S3StatusOutOfMemory;
}
for (i = 0; i < count; i++) {
- if (!(pLocksG[i] = mutex_create())) {
- while (i-- > 0) {
- mutex_destroy(pLocksG[i]);
- }
- return S3StatusFailedToCreateMutex;
- }
+ pthread_mutex_init(&(pLocksG[i]), 0);
}
- CRYPTO_set_id_callback(threadSelfCallback);
+ CRYPTO_set_id_callback(&id_callback);
CRYPTO_set_locking_callback(&locking_callback);
CRYPTO_set_dynlock_create_callback(dynlock_create);
CRYPTO_set_dynlock_lock_callback(dynlock_lock);
@@ -204,7 +162,6 @@ const char *S3_get_status_name(S3Status status)
handlecase(InternalError);
handlecase(OutOfMemory);
handlecase(Interrupted);
- handlecase(FailedToCreateMutex);
handlecase(InvalidBucketNameTooLong);
handlecase(InvalidBucketNameFirstCharacter);
handlecase(InvalidBucketNameCharacter);
diff --git a/src/request.c b/src/request.c
index 6dabdfe..40a16fc 100644
--- a/src/request.c
+++ b/src/request.c
@@ -29,6 +29,7 @@
#include <openssl/buffer.h>
#include <openssl/evp.h>
#include <openssl/hmac.h>
+#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <sys/utsname.h>
@@ -43,7 +44,7 @@
static char userAgentG[USER_AGENT_SIZE];
-static struct S3Mutex *requestStackMutexG;
+static pthread_mutex_t requestStackMutexG;
static Request *requestStackG[REQUEST_STACK_SIZE];
@@ -938,13 +939,13 @@ static S3Status request_get(const RequestParams *params,
// Try to get one from the request stack. We hold the lock for the
// shortest time possible here.
- mutex_lock(requestStackMutexG);
+ pthread_mutex_lock(&requestStackMutexG);
if (requestStackCountG) {
request = requestStackG[--requestStackCountG];
}
- mutex_unlock(requestStackMutexG);
+ pthread_mutex_unlock(&requestStackMutexG);
// If we got one, deinitialize it for re-use
if (request) {
@@ -1019,11 +1020,11 @@ static void request_destroy(Request *request)
static void request_release(Request *request)
{
- mutex_lock(requestStackMutexG);
+ pthread_mutex_lock(&requestStackMutexG);
// If the request stack is full, destroy this one
if (requestStackCountG == REQUEST_STACK_SIZE) {
- mutex_unlock(requestStackMutexG);
+ pthread_mutex_unlock(&requestStackMutexG);
request_destroy(request);
}
// Else put this one at the front of the request stack; we do this because
@@ -1032,7 +1033,7 @@ static void request_release(Request *request)
// times out
else {
requestStackG[requestStackCountG++] = request;
- mutex_unlock(requestStackMutexG);
+ pthread_mutex_unlock(&requestStackMutexG);
}
}
@@ -1045,9 +1046,7 @@ S3Status request_api_initialize(const char *userAgentInfo, int flags)
return S3StatusInternalError;
}
- if (!(requestStackMutexG = mutex_create())) {
- return S3StatusFailedToCreateMutex;
- }
+ pthread_mutex_init(&requestStackMutexG, 0);
requestStackCountG = 0;
@@ -1077,7 +1076,7 @@ S3Status request_api_initialize(const char *userAgentInfo, int flags)
void request_api_deinitialize()
{
- mutex_destroy(requestStackMutexG);
+ pthread_mutex_destroy(&requestStackMutexG);
while (requestStackCountG--) {
request_destroy(requestStackG[requestStackCountG]);
diff --git a/src/s3.c b/src/s3.c
index 8d1ff55..1dad3bc 100644
--- a/src/s3.c
+++ b/src/s3.c
@@ -133,7 +133,7 @@ static char errorDetailsG[4096] = { 0 };
static void S3_init()
{
S3Status status;
- if ((status = S3_initialize("s3", 0, 0, 0, 0, 0, S3_INIT_ALL))
+ if ((status = S3_initialize("s3", S3_INIT_ALL))
!= S3StatusOK) {
fprintf(stderr, "Failed to initialize libs3: %s\n",
S3_get_status_name(status));