Wrapped mutex locking and unlocking functions in error checking macro

This commit is contained in:
Fufu Fang 2019-09-01 01:21:40 +01:00
parent 92a9658c66
commit 1a44a4d960
No known key found for this signature in database
GPG Key ID: 0F6BB5EF6F8BB729
4 changed files with 53 additions and 31 deletions

View File

@ -724,7 +724,7 @@ Cache *Cache_open(const char *fn)
fprintf(stderr, "Cache_open(): thread %lu: locking cf_lock;\n",
pthread_self());
#endif
pthread_mutex_lock(&cf_lock);
PTHREAD_MUTEX_LOCK(&cf_lock);
if (link->cache_opened) {
link->cache_opened++;
@ -732,7 +732,7 @@ Cache *Cache_open(const char *fn)
fprintf(stderr, "Cache_open(): thread %lu: unlocking cf_lock;\n",
pthread_self());
#endif
pthread_mutex_unlock(&cf_lock);
PTHREAD_MUTEX_UNLOCK(&cf_lock);
return link->cache_ptr;
}
@ -740,7 +740,7 @@ Cache *Cache_open(const char *fn)
fprintf(stderr, "Cache_open(): thread %lu: unlocking cf_lock;\n",
pthread_self());
#endif
pthread_mutex_unlock(&cf_lock);
PTHREAD_MUTEX_UNLOCK(&cf_lock);
/*----------------------------------------------------------------*/
/* Create the cache in-memory data structure */
@ -808,7 +808,7 @@ void Cache_close(Cache *cf)
fprintf(stderr, "Cache_close(): thread %lu: locking cf_lock;\n",
pthread_self());
#endif
pthread_mutex_lock(&cf_lock);
PTHREAD_MUTEX_LOCK(&cf_lock);
cf->link->cache_opened--;
@ -817,7 +817,7 @@ void Cache_close(Cache *cf)
fprintf(stderr, "Cache_close(): thread %lu: unlocking cf_lock;\n",
pthread_self());
#endif
pthread_mutex_unlock(&cf_lock);
PTHREAD_MUTEX_UNLOCK(&cf_lock);
return;
}
@ -825,7 +825,7 @@ void Cache_close(Cache *cf)
fprintf(stderr, "Cache_close(): thread %lu: unlocking cf_lock;\n",
pthread_self());
#endif
pthread_mutex_unlock(&cf_lock);
PTHREAD_MUTEX_UNLOCK(&cf_lock);
/*----------------------------------------------------------------*/
@ -881,7 +881,7 @@ static void *Cache_bgdl(void *arg)
fprintf(stderr, "Cache_bgdl(): thread %lu: locking rw_lock;\n",
pthread_self());
#endif
pthread_mutex_lock(&cf->rw_lock);
PTHREAD_MUTEX_LOCK(&cf->rw_lock);
uint8_t *recv_buf = calloc(cf->blksz, sizeof(uint8_t));
fprintf(stderr, "Cache_bgdl(): thread %lu:", pthread_self());
long recv = path_download(cf->path, (char *) recv_buf, cf->blksz,
@ -900,12 +900,12 @@ static void *Cache_bgdl(void *arg)
fprintf(stderr, "Cache_bgdl(): thread %lu: unlocking bgt_lock;\n",
pthread_self());
#endif
pthread_mutex_unlock(&cf->bgt_lock);
PTHREAD_MUTEX_UNLOCK(&cf->bgt_lock);
#ifdef CACHE_LOCK_DEBUG
fprintf(stderr, "Cache_bgdl(): thread %lu: unlocking rw_lock;\n",
pthread_self());
#endif
pthread_mutex_unlock(&cf->rw_lock);
PTHREAD_MUTEX_UNLOCK(&cf->rw_lock);
pthread_detach(pthread_self());
pthread_exit(NULL);
}
@ -942,15 +942,15 @@ long Cache_read(Cache *cf, char *output_buf, off_t len, off_t offset)
"Cache_read(): thread %lu: locking and unlocking bgt_lock;\n",
pthread_self());
#endif
pthread_mutex_lock(&cf->bgt_lock);
pthread_mutex_unlock(&cf->bgt_lock);
PTHREAD_MUTEX_LOCK(&cf->bgt_lock);
PTHREAD_MUTEX_UNLOCK(&cf->bgt_lock);
#ifdef CACHE_LOCK_DEBUG
/* Wait for any other download thread to finish*/
fprintf(stderr, "Cache_read(): thread %lu: locking rw_lock;\n",
pthread_self());
#endif
pthread_mutex_lock(&cf->rw_lock);
PTHREAD_MUTEX_LOCK(&cf->rw_lock);
if (Seg_exist(cf, offset)) {
/* The segment already exists - it was downloaded by other
* download thread. Send it off and unlock the I/O */
@ -959,7 +959,7 @@ long Cache_read(Cache *cf, char *output_buf, off_t len, off_t offset)
fprintf(stderr, "Cache_read(): thread %lu: unlocking rw_lock;\n",
pthread_self());
#endif
pthread_mutex_unlock(&cf->rw_lock);
PTHREAD_MUTEX_UNLOCK(&cf->rw_lock);
goto bgdl;
}
}
@ -995,7 +995,7 @@ long Cache_read(Cache *cf, char *output_buf, off_t len, off_t offset)
fprintf(stderr, "Cache_read(): thread %lu: unlocking rw_lock;\n",
pthread_self());
#endif
pthread_mutex_unlock(&cf->rw_lock);
PTHREAD_MUTEX_UNLOCK(&cf->rw_lock);
/* -----------Download the next segment in background -------------------*/
bgdl:

View File

@ -289,7 +289,7 @@ LinkTable *LinkTable_new(const char *url)
"LinkTable_new(): thread %lu: locking link_lock;\n",
pthread_self());
#endif
pthread_mutex_lock(&link_lock);
PTHREAD_MUTEX_LOCK(&link_lock);
LinkTable *linktbl = calloc(1, sizeof(LinkTable));
if (!linktbl) {
fprintf(stderr, "LinkTable_new(): calloc failure!\n");
@ -378,7 +378,7 @@ HTTP %ld\n", url, http_resp);
"LinkTable_new(): thread %lu: unlocking link_lock;\n",
pthread_self());
#endif
pthread_mutex_unlock(&link_lock);
PTHREAD_MUTEX_UNLOCK(&link_lock);
return linktbl;
}
@ -589,8 +589,8 @@ long path_download(const char *path, char *output_buf, size_t size,
pthread_self());
#endif
pthread_mutex_lock(&link_lock);
pthread_mutex_unlock(&link_lock);
PTHREAD_MUTEX_LOCK(&link_lock);
PTHREAD_MUTEX_UNLOCK(&link_lock);
transfer_blocking(curl);

View File

@ -41,9 +41,9 @@ static void crypto_lock_callback(int mode, int type, char *file, int line)
(void)file;
(void)line;
if(mode & CRYPTO_LOCK) {
pthread_mutex_lock(&(crypto_lockarray[type]));
PTHREAD_MUTEX_LOCK(&(crypto_lockarray[type]));
} else {
pthread_mutex_unlock(&(crypto_lockarray[type]));
PTHREAD_MUTEX_UNLOCK(&(crypto_lockarray[type]));
}
}
@ -85,7 +85,7 @@ static void curl_callback_lock(CURL *handle, curl_lock_data data,
(void)userptr; /* unused */
(void)handle; /* unused */
(void)data; /* unused */
pthread_mutex_lock(&curl_lock);
PTHREAD_MUTEX_LOCK(&curl_lock);
}
static void curl_callback_unlock(CURL *handle, curl_lock_data data,
@ -94,7 +94,7 @@ static void curl_callback_unlock(CURL *handle, curl_lock_data data,
(void)userptr; /* unused */
(void)handle; /* unused */
(void)data; /* unused */
pthread_mutex_unlock(&curl_lock);
PTHREAD_MUTEX_UNLOCK(&curl_lock);
}
/**
@ -165,7 +165,7 @@ int curl_multi_perform_once()
"curl_multi_perform_once(): thread %lu: locking transfer_lock;\n",
pthread_self());
#endif
pthread_mutex_lock(&transfer_lock);
PTHREAD_MUTEX_LOCK(&transfer_lock);
/* Get curl multi interface to perform pending tasks */
int n_running_curl;
CURLMcode mc = curl_multi_perform(curl_multi, &n_running_curl);
@ -227,7 +227,7 @@ int curl_multi_perform_once()
"curl_multi_perform_once(): thread %lu: unlocking transfer_lock;\n",
pthread_self());
#endif
pthread_mutex_unlock(&transfer_lock);
PTHREAD_MUTEX_UNLOCK(&transfer_lock);
return n_running_curl;
}
@ -341,14 +341,14 @@ void transfer_blocking(CURL *curl)
"transfer_blocking(): thread %lu: locking transfer_lock;\n",
pthread_self());
#endif
pthread_mutex_lock(&transfer_lock);
PTHREAD_MUTEX_LOCK(&transfer_lock);
CURLMcode res = curl_multi_add_handle(curl_multi, curl);
#ifdef NETWORK_LOCK_DEBUG
fprintf(stderr,
"transfer_blocking(): thread %lu: unlocking transfer_lock;\n",
pthread_self());
#endif
pthread_mutex_unlock(&transfer_lock);
PTHREAD_MUTEX_UNLOCK(&transfer_lock);
if(res > 0) {
fprintf(stderr, "transfer_blocking(): %d, %s\n",
@ -368,14 +368,14 @@ void transfer_nonblocking(CURL *curl)
"transfer_nonblocking(): thread %lu: locking transfer_lock;\n",
pthread_self());
#endif
pthread_mutex_lock(&transfer_lock);
PTHREAD_MUTEX_LOCK(&transfer_lock);
CURLMcode res = curl_multi_add_handle(curl_multi, curl);
#ifdef NETWORK_LOCK_DEBUG
fprintf(stderr,
"transfer_nonblocking(): thread %lu: unlocking transfer_lock;\n",
pthread_self());
#endif
pthread_mutex_unlock(&transfer_lock);
PTHREAD_MUTEX_UNLOCK(&transfer_lock);
if(res > 0) {
fprintf(stderr, "transfer_nonblocking(): %s\n",

View File

@ -1,13 +1,35 @@
#ifndef UTIL_H
#define UTIL_H
#include <stdint.h>
/**
* \file util.h
* \brief utility functions
*/
#include <stdint.h>
#include <stdlib.h>
#define PTHREAD_MUTEX_UNLOCK(x)\
({\
int i;\
i = pthread_mutex_unlock(x);\
if (i) { \
fprintf(stderr, "pthread_mutex_unlock failed, %d, %s\n", i, \
strerror(i));\
exit(EXIT_FAILURE);\
}\
})
#define PTHREAD_MUTEX_LOCK(x)\
({\
int i;\
i = pthread_mutex_lock(x);\
if (i) { \
fprintf(stderr, "pthread_mutex_lock failed, %d, %s\n", i, \
strerror(i));\
exit(EXIT_FAILURE);\
}\
})
/**
* \brief the maximum length of a path and a URL.
* \details This corresponds the maximum path length under Ext4.