diff options
author | Bryan Ischo <bryan@ischo.com> | 2008-08-12 12:26:56 +0000 |
---|---|---|
committer | Bryan Ischo <bryan@ischo.com> | 2008-08-12 12:26:56 +0000 |
commit | 953d590bd9a3508d37552efb89aaa68344466d80 (patch) | |
tree | f907dd0ae53614b641c84007d630401a8e1eab1e /src | |
parent | 2cda77ecda9c93a03329256744b19a237f92b810 (diff) | |
download | ceph-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.
Diffstat (limited to 'src')
-rw-r--r-- | src/general.c | 87 | ||||
-rw-r--r-- | src/request.c | 19 | ||||
-rw-r--r-- | src/s3.c | 2 |
3 files changed, 32 insertions, 76 deletions
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]); @@ -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)); |