Skip to content

Commit c660159

Browse files
committed
added mutex locking / unlocking messages
1 parent ee2d3ed commit c660159

1 file changed

Lines changed: 31 additions & 2 deletions

File tree

src/cache.c

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -756,8 +756,18 @@ cf->content_length: %ld, Data_size(fn): %ld.\n", fn, cf->content_length,
756756
void Cache_close(Cache *cf)
757757
{
758758
/* Must wait for the background download thread to stop */
759+
fprintf(stderr, "Cache_read(): locking bgt_lock;\n");
760+
fflush(stderr);
759761
pthread_mutex_lock(&cf->bgt_lock);
762+
fprintf(stderr, "Cache_read(): unlocking bgt_lock;\n");
763+
fflush(stderr);
760764
pthread_mutex_unlock(&cf->bgt_lock);
765+
fprintf(stderr, "Cache_read(): locking rw_lock;\n");
766+
fflush(stderr);
767+
pthread_mutex_lock(&cf->rw_lock);
768+
fprintf(stderr, "Cache_read(): unlocking rw_lock;\n");
769+
fflush(stderr);
770+
pthread_mutex_unlock(&cf->rw_lock);
761771

762772
if (Meta_write(cf)) {
763773
fprintf(stderr, "Cache_close(): Meta_write() error.");
@@ -807,6 +817,8 @@ static void Seg_set(Cache *cf, off_t offset, int i)
807817
static void *Cache_bgdl(void *arg)
808818
{
809819
Cache *cf = (Cache *) arg;
820+
fprintf(stderr, "Cache_bgdl(): locking rw_lock;\n");
821+
fflush(stderr);
810822
pthread_mutex_lock(&cf->rw_lock);
811823
uint8_t *recv_buf = calloc(cf->blksz, sizeof(uint8_t));
812824
fprintf(stderr, "Cache_bgdl(): ");
@@ -823,7 +835,11 @@ static void *Cache_bgdl(void *arg)
823835
cf->next_offset);
824836
}
825837
free(recv_buf);
838+
fprintf(stderr, "Cache_bgdl(): unlocking bgt_lock;\n");
839+
fflush(stderr);
826840
pthread_mutex_unlock(&cf->bgt_lock);
841+
fprintf(stderr, "Cache_bgdl(): unlocking rw_lock;\n");
842+
fflush(stderr);
827843
pthread_mutex_unlock(&cf->rw_lock);
828844
pthread_exit(NULL);
829845
}
@@ -852,14 +868,23 @@ long Cache_read(Cache *cf, char *output_buf, off_t len, off_t offset)
852868
send = Data_read(cf, (uint8_t *) output_buf, len, offset);
853869
goto bgdl;
854870
} else {
855-
/* Wait for the other I/O threads to finish, then lock */
871+
/* Wait for any other download thread to finish, then lock */
872+
fprintf(stderr, "Cache_read(): locking rw_lock;\n");
873+
fflush(stderr);
856874
pthread_mutex_lock(&cf->rw_lock);
857875
/* Wait for the background download thread to finish */
876+
fprintf(stderr, "Cache_read(): locking bgt_lock;\n");
877+
fflush(stderr);
858878
pthread_mutex_lock(&cf->bgt_lock);
879+
fprintf(stderr, "Cache_read(): unlocking bgt_lock;\n");
880+
fflush(stderr);
859881
pthread_mutex_unlock(&cf->bgt_lock);
860882
if (Seg_exist(cf, offset)) {
861-
/* The segment already exists, send it off the unlock the I/O */
883+
/* The segment already exists - it was downloaded by the background
884+
* download thread. Send it off and unlock the I/O */
862885
send = Data_read(cf, (uint8_t *) output_buf, len, offset);
886+
fprintf(stderr, "Cache_read(): unlocking rw_lock;\n");
887+
fflush(stderr);
863888
pthread_mutex_unlock(&cf->rw_lock);
864889
goto bgdl;
865890
}
@@ -893,6 +918,8 @@ long Cache_read(Cache *cf, char *output_buf, off_t len, off_t offset)
893918
recv);
894919
}
895920
free(recv_buf);
921+
fprintf(stderr, "Cache_read(): unlocking rw_lock;\n");
922+
fflush(stderr);
896923
pthread_mutex_unlock(&cf->rw_lock);
897924

898925
/* -----------Download the next segment in background -------------------*/
@@ -903,6 +930,8 @@ long Cache_read(Cache *cf, char *output_buf, off_t len, off_t offset)
903930
cf->next_offset < cf->content_length ){
904931
/* Stop the spawning of multiple background pthreads */
905932
if(!pthread_mutex_trylock(&cf->bgt_lock)) {
933+
fprintf(stderr, "Cache_read(): trylocked bgt_lock;\n");
934+
fflush(stderr);
906935
if (pthread_create(&cf->bgt, NULL, Cache_bgdl, cf)) {
907936
fprintf(stderr,
908937
"Cache_read(): Error creating background download thread\n"

0 commit comments

Comments
 (0)