diff --git a/src/cache.h b/src/cache.h index 5b1947b..797279c 100644 --- a/src/cache.h +++ b/src/cache.h @@ -69,9 +69,6 @@ struct Cache { /** \brief the FUSE filesystem path to the remote file*/ char *fs_path; - - /** Transfer struct for the streaming cache */ - TransferStruct *ts; }; /** diff --git a/src/link.c b/src/link.c index e7e98fe..eb0e105 100644 --- a/src/link.c +++ b/src/link.c @@ -41,8 +41,7 @@ static Link *Link_new(const char *linkname, LinkType type) /* * remove the '/' from linkname if it exists */ - char *c = - &(link->linkname[strnlen(link->linkname, MAX_FILENAME_LEN) - 1]); + char *c = &(link->linkname[strnlen(link->linkname, MAX_FILENAME_LEN) - 1]); if (*c == '/') { *c = '\0'; } diff --git a/src/log.h b/src/log.h index 75fee30..3ea7b26 100644 --- a/src/log.h +++ b/src/log.h @@ -13,7 +13,8 @@ typedef enum { link_lock_debug = 1 << 5, network_lock_debug = 1 << 6, cache_lock_debug = 1 << 7, - libcurl_debug = 1 << 8, + ramcache_debug = 1 << 8, + libcurl_debug = 1 << 9, } LogType; /** diff --git a/src/ramcache.c b/src/ramcache.c index 1750ce2..7c9a62f 100644 --- a/src/ramcache.c +++ b/src/ramcache.c @@ -1,6 +1,7 @@ #include "ramcache.h" #include "log.h" +#include "util.h" #include #include @@ -11,6 +12,12 @@ size_t write_memory_callback(void *recv_data, size_t size, size_t nmemb, size_t recv_size = size * nmemb; TransferStruct *ts = (TransferStruct *) userp; + if (ts->bg_transfer) { + lprintf(ramcache_debug, "ramcache: thread %x: locking;\n", + pthread_self()); + } + PTHREAD_MUTEX_LOCK(&ts->lock); + ts->data = realloc(ts->data, ts->curr_size + recv_size + 1); if (!ts->data) { /* @@ -22,5 +29,49 @@ size_t write_memory_callback(void *recv_data, size_t size, size_t nmemb, memmove(&ts->data[ts->curr_size], recv_data, recv_size); ts->curr_size += recv_size; ts->data[ts->curr_size] = '\0'; + if (ts->bg_transfer) { + lprintf(ramcache_debug, "ramcache: thread %x: unlocking;\n", + pthread_self()); + } + PTHREAD_MUTEX_UNLOCK(&ts->lock); return recv_size; -} \ No newline at end of file +} + +TransferStruct *TransferStruct_bg_transfer_new() +{ + TransferStruct *ts = CALLOC(1, sizeof(TransferStruct)); + if (pthread_mutexattr_init(&ts->lock_attr)) { + lprintf(fatal, "lock_attr initialisation failed!\n"); + } + + if (pthread_mutexattr_setpshared(&ts->lock_attr, + PTHREAD_PROCESS_SHARED)) { + lprintf(fatal, "could not set lock_attr!\n"); + } + + if (pthread_mutex_init(&ts->lock, &ts->lock_attr)) { + lprintf(fatal, "lock initialisation failed!\n"); + } + + ts->bg_transfer = 1; + + return ts; +} + +void TransferStruct_bg_transfer_free(TransferStruct *ts) +{ + if (ts->bg_transfer) { + if (pthread_mutex_destroy(&ts->lock)) { + lprintf(fatal, "could not destroy lock!\n"); + } + + if (pthread_mutexattr_destroy(&ts->lock_attr)) { + lprintf(fatal, "could not destroy lock_attr!\n"); + } + } + + /* free(NULL) does nothing */ + free(ts->data); + FREE(ts); +} + diff --git a/src/ramcache.h b/src/ramcache.h index 33cada1..e688fb3 100644 --- a/src/ramcache.h +++ b/src/ramcache.h @@ -26,12 +26,32 @@ struct TransferStruct { Link *link; /** \brief The minium requested size */ size_t min_req_size; - /** \brief Whether we have sent off the minimally requested data*/ - int min_data_sent; + /** \brief mutex for background transfer */ + pthread_mutex_t lock; + /** \brief attribute associated with the mutex */ + pthread_mutexattr_t lock_attr; + /** \brief Whether this TransferStruct was used for background transfer */ + int bg_transfer; + /** \brief The cache file used for background transfer */ + Cache *cf; + /** \brief The ID of the segment being downloaded */ + off_t seg_id; }; -/** \brief callback function for file transfer */ +/** + * \brief Callback function for file transfer + */ size_t write_memory_callback(void *contents, size_t size, size_t nmemb, void *userp); +/** + * \brief Create a TransferStruct for background transfer + */ +TransferStruct *TransferStruct_bg_transfer_new(); + +/** + * \brief Free a TransferStruct used for background transfer + */ +void TransferStruct_free(TransferStruct *ts); + #endif \ No newline at end of file diff --git a/src/util.c b/src/util.c index be9adec..1ad431b 100644 --- a/src/util.c +++ b/src/util.c @@ -142,7 +142,6 @@ void FREE(void *ptr) { if (ptr) { free(ptr); - ptr = NULL; } else { lprintf(fatal, "attempted to double free a pointer!\n"); }