Skip to content

Commit

Permalink
'lru_crawler enable' blocks until ready
Browse files Browse the repository at this point in the history
single CPU VM builders could fail:
 - spawn LRU crawler thread.
 - signal LRU crawler.
 - LRU crawler thread now waits on condition.
 - Crawler thread misses condition, sits forever.

Might also want to move the "stats.lru_crawler_running" bit to be updated when
the crawler thread picks up the work to do, somehow.
  • Loading branch information
dormando committed Nov 19, 2015
1 parent ec937e5 commit b6bf6df
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion items.c
Original file line number Diff line number Diff line change
Expand Up @@ -1237,6 +1237,8 @@ static void *item_crawler_thread(void *arg) {
int crawls_persleep = settings.crawls_persleep;

pthread_mutex_lock(&lru_crawler_lock);
pthread_cond_signal(&lru_crawler_cond);
settings.lru_crawler = true;
if (settings.verbose > 2)
fprintf(stderr, "Starting LRU crawler background thread\n");
while (do_run_lru_crawler_thread) {
Expand Down Expand Up @@ -1329,21 +1331,33 @@ int stop_item_crawler_thread(void) {
return 0;
}

/* Lock dance to "block" until thread is waiting on its condition:
* caller locks mtx. caller spawns thread.
* thread blocks on mutex.
* caller waits on condition, releases lock.
* thread gets lock, sends signal.
* caller can't wait, as thread has lock.
* thread waits on condition, releases lock
* caller wakes on condition, gets lock.
* caller immediately releases lock.
* thread is now safely waiting on condition before the caller returns.
*/
int start_item_crawler_thread(void) {
int ret;

if (settings.lru_crawler)
return -1;
pthread_mutex_lock(&lru_crawler_lock);
do_run_lru_crawler_thread = 1;
settings.lru_crawler = true;
if ((ret = pthread_create(&item_crawler_tid, NULL,
item_crawler_thread, NULL)) != 0) {
fprintf(stderr, "Can't create LRU crawler thread: %s\n",
strerror(ret));
pthread_mutex_unlock(&lru_crawler_lock);
return -1;
}
/* Avoid returning until the crawler has actually started */
pthread_cond_wait(&lru_crawler_cond, &lru_crawler_lock);
pthread_mutex_unlock(&lru_crawler_lock);

return 0;
Expand Down

0 comments on commit b6bf6df

Please sign in to comment.