improved thread synchronisation, to stop Cache_read() from downloading the segment that is being downloaded in the background by Cache_bgdl()

This commit is contained in:
Fufu Fang 2019-04-26 10:57:07 +01:00
parent f479482d7b
commit 80a98a79f9
1 changed files with 4 additions and 1 deletions

View File

@ -792,8 +792,11 @@ long Cache_read(Cache *cf, char *output_buf, off_t len, off_t offset)
send = Data_read(cf, (uint8_t *) output_buf, len, offset);
goto bgdl;
} else {
/* Wait until the background thread finishes, then lock the I/O */
/* Wait for the other I/O threads to finish, then lock */
pthread_mutex_lock(&cf->rw_lock);
/* Wait for the background download thread to finish */
pthread_mutex_lock(&cf->bgt_lock);
pthread_mutex_unlock(&cf->bgt_lock);
if (Seg_exist(cf, offset)) {
/* The segment already exists, send it off the unlock the I/O */
send = Data_read(cf, (uint8_t *) output_buf, len, offset);