Prefer named locks in curl

This removes the confusing and unsafe pthread_mutex_t array.
This commit is contained in:
Andrew Gaul 2020-08-18 21:37:02 +09:00
parent 0e895f60a0
commit ba61470bae
2 changed files with 16 additions and 20 deletions

View File

@ -387,7 +387,7 @@ static const char* SPECIAL_DARWIN_MIME_FILE = "/etc/apache2/mime.types";
#endif #endif
pthread_mutex_t S3fsCurl::curl_handles_lock; pthread_mutex_t S3fsCurl::curl_handles_lock;
pthread_mutex_t S3fsCurl::curl_share_lock[SHARE_MUTEX_MAX]; S3fsCurl::callback_locks_t S3fsCurl::callback_locks;
bool S3fsCurl::is_initglobal_done = false; bool S3fsCurl::is_initglobal_done = false;
CurlHandlerPool* S3fsCurl::sCurlPool = NULL; CurlHandlerPool* S3fsCurl::sCurlPool = NULL;
int S3fsCurl::sCurlPoolSize = 32; int S3fsCurl::sCurlPoolSize = 32;
@ -448,10 +448,10 @@ bool S3fsCurl::InitS3fsCurl()
if(0 != pthread_mutex_init(&S3fsCurl::curl_handles_lock, &attr)){ if(0 != pthread_mutex_init(&S3fsCurl::curl_handles_lock, &attr)){
return false; return false;
} }
if(0 != pthread_mutex_init(&S3fsCurl::curl_share_lock[SHARE_MUTEX_DNS], &attr)){ if(0 != pthread_mutex_init(&S3fsCurl::callback_locks.dns, &attr)){
return false; return false;
} }
if(0 != pthread_mutex_init(&S3fsCurl::curl_share_lock[SHARE_MUTEX_SSL_SESSION], &attr)){ if(0 != pthread_mutex_init(&S3fsCurl::callback_locks.ssl_session, &attr)){
return false; return false;
} }
if(!S3fsCurl::InitGlobalCurl()){ if(!S3fsCurl::InitGlobalCurl()){
@ -494,10 +494,10 @@ bool S3fsCurl::DestroyS3fsCurl()
if(!S3fsCurl::DestroyGlobalCurl()){ if(!S3fsCurl::DestroyGlobalCurl()){
result = false; result = false;
} }
if(0 != pthread_mutex_destroy(&S3fsCurl::curl_share_lock[SHARE_MUTEX_DNS])){ if(0 != pthread_mutex_destroy(&S3fsCurl::callback_locks.dns)){
result = false; result = false;
} }
if(0 != pthread_mutex_destroy(&S3fsCurl::curl_share_lock[SHARE_MUTEX_SSL_SESSION])){ if(0 != pthread_mutex_destroy(&S3fsCurl::callback_locks.ssl_session)){
result = false; result = false;
} }
if(0 != pthread_mutex_destroy(&S3fsCurl::curl_handles_lock)){ if(0 != pthread_mutex_destroy(&S3fsCurl::curl_handles_lock)){
@ -571,7 +571,7 @@ bool S3fsCurl::InitShareCurl()
S3FS_PRN_WARN("curl_share_setopt(SSL SESSION) returns %d(%s), but continue without shared ssl session data.", nSHCode, curl_share_strerror(nSHCode)); S3FS_PRN_WARN("curl_share_setopt(SSL SESSION) returns %d(%s), but continue without shared ssl session data.", nSHCode, curl_share_strerror(nSHCode));
} }
} }
if(CURLSHE_OK != (nSHCode = curl_share_setopt(S3fsCurl::hCurlShare, CURLSHOPT_USERDATA, (void*)&S3fsCurl::curl_share_lock[0]))){ if(CURLSHE_OK != (nSHCode = curl_share_setopt(S3fsCurl::hCurlShare, CURLSHOPT_USERDATA, &S3fsCurl::callback_locks))){
S3FS_PRN_ERR("curl_share_setopt(USERDATA) returns %d(%s)", nSHCode, curl_share_strerror(nSHCode)); S3FS_PRN_ERR("curl_share_setopt(USERDATA) returns %d(%s)", nSHCode, curl_share_strerror(nSHCode));
return false; return false;
} }
@ -599,15 +599,15 @@ void S3fsCurl::LockCurlShare(CURL* handle, curl_lock_data nLockData, curl_lock_a
if(!hCurlShare){ if(!hCurlShare){
return; return;
} }
S3fsCurl::callback_locks_t* locks = static_cast<S3fsCurl::callback_locks_t*>(useptr);
int res; int res;
pthread_mutex_t* lockmutex = static_cast<pthread_mutex_t*>(useptr);
if(CURL_LOCK_DATA_DNS == nLockData){ if(CURL_LOCK_DATA_DNS == nLockData){
if(0 != (res = pthread_mutex_lock(&lockmutex[SHARE_MUTEX_DNS]))){ if(0 != (res = pthread_mutex_lock(&locks->dns))){
S3FS_PRN_CRIT("pthread_mutex_lock returned: %d", res); S3FS_PRN_CRIT("pthread_mutex_lock returned: %d", res);
abort(); abort();
} }
}else if(CURL_LOCK_DATA_SSL_SESSION == nLockData){ }else if(CURL_LOCK_DATA_SSL_SESSION == nLockData){
if(0 != (res = pthread_mutex_lock(&lockmutex[SHARE_MUTEX_SSL_SESSION]))){ if(0 != (res = pthread_mutex_lock(&locks->ssl_session))){
S3FS_PRN_CRIT("pthread_mutex_lock returned: %d", res); S3FS_PRN_CRIT("pthread_mutex_lock returned: %d", res);
abort(); abort();
} }
@ -619,15 +619,15 @@ void S3fsCurl::UnlockCurlShare(CURL* handle, curl_lock_data nLockData, void* use
if(!hCurlShare){ if(!hCurlShare){
return; return;
} }
S3fsCurl::callback_locks_t* locks = static_cast<S3fsCurl::callback_locks_t*>(useptr);
int res; int res;
pthread_mutex_t* lockmutex = static_cast<pthread_mutex_t*>(useptr);
if(CURL_LOCK_DATA_DNS == nLockData){ if(CURL_LOCK_DATA_DNS == nLockData){
if(0 != (res = pthread_mutex_unlock(&lockmutex[SHARE_MUTEX_DNS]))){ if(0 != (res = pthread_mutex_unlock(&locks->dns))){
S3FS_PRN_CRIT("pthread_mutex_unlock returned: %d", res); S3FS_PRN_CRIT("pthread_mutex_unlock returned: %d", res);
abort(); abort();
} }
}else if(CURL_LOCK_DATA_SSL_SESSION == nLockData){ }else if(CURL_LOCK_DATA_SSL_SESSION == nLockData){
if(0 != (res = pthread_mutex_unlock(&lockmutex[SHARE_MUTEX_SSL_SESSION]))){ if(0 != (res = pthread_mutex_unlock(&locks->ssl_session))){
S3FS_PRN_CRIT("pthread_mutex_unlock returned: %d", res); S3FS_PRN_CRIT("pthread_mutex_unlock returned: %d", res);
abort(); abort();
} }

View File

@ -227,13 +227,6 @@ enum sse_type_t {
SSE_KMS // server side encrypting by kms id SSE_KMS // server side encrypting by kms id
}; };
// share
enum {
SHARE_MUTEX_DNS = 0,
SHARE_MUTEX_SSL_SESSION = 1,
SHARE_MUTEX_MAX = 2,
};
// Class for lapping curl // Class for lapping curl
// //
class S3fsCurl class S3fsCurl
@ -262,7 +255,10 @@ class S3fsCurl
// class variables // class variables
static pthread_mutex_t curl_handles_lock; static pthread_mutex_t curl_handles_lock;
static pthread_mutex_t curl_share_lock[SHARE_MUTEX_MAX]; static struct callback_locks_t {
pthread_mutex_t dns;
pthread_mutex_t ssl_session;
} callback_locks;
static bool is_initglobal_done; static bool is_initglobal_done;
static CurlHandlerPool* sCurlPool; static CurlHandlerPool* sCurlPool;
static int sCurlPoolSize; static int sCurlPoolSize;