Skip to content

Commit

Permalink
Wait zero active threads condition before to fork() for BGSAVE or BGR…
Browse files Browse the repository at this point in the history
…EWRITEAOF
  • Loading branch information
antirez committed Jan 13, 2010
1 parent c7df85a commit 4ee9488
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions redis.c
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,7 @@ static void freeIOJob(iojob *j);
static void queueIOJob(iojob *j);
static int vmWriteObjectOnSwap(robj *o, off_t page);
static robj *vmReadObjectFromSwap(off_t page, int type);
static void waitZeroActiveThreads(void);

static void authCommand(redisClient *c);
static void pingCommand(redisClient *c);
Expand Down Expand Up @@ -3085,6 +3086,7 @@ static int rdbSaveBackground(char *filename) {
pid_t childpid;

if (server.bgsavechildpid != -1) return REDIS_ERR;
if (server.vm_enabled) waitZeroActiveThreads();
if ((childpid = fork()) == 0) {
/* Child */
close(server.fd);
Expand Down Expand Up @@ -6906,6 +6908,7 @@ static int rewriteAppendOnlyFileBackground(void) {
pid_t childpid;

if (server.bgrewritechildpid != -1) return REDIS_ERR;
if (server.vm_enabled) waitZeroActiveThreads();
if ((childpid = fork()) == 0) {
/* Child */
char tmpfile[256];
Expand Down Expand Up @@ -7669,6 +7672,20 @@ static void spawnIOThread(void) {
server.io_active_threads++;
}

/* We need to wait for the last thread to exit before we are able to
* fork() in order to BGSAVE or BGREWRITEAOF. */
static void waitZeroActiveThreads(void) {
while(1) {
lockThreadedIO();
if (server.io_active_threads == 0) {
unlockThreadedIO();
return;
}
unlockThreadedIO();
usleep(10000); /* 10 milliseconds */
}
}

/* This function must be called while with threaded IO locked */
static void queueIOJob(iojob *j) {
redisLog(REDIS_DEBUG,"Queued IO Job %p type %d about key '%s'\n",
Expand Down

0 comments on commit 4ee9488

Please sign in to comment.