| 166 | } |
| 167 | |
| 168 | void *bioProcessBackgroundJobs(void *arg) { |
| 169 | struct bio_job *job; |
| 170 | unsigned long type = (unsigned long) arg; |
| 171 | sigset_t sigset; |
| 172 | |
| 173 | /* Check that the type is within the right interval. */ |
| 174 | if (type >= BIO_NUM_OPS) { |
| 175 | serverLog(LL_WARNING, |
| 176 | "Warning: bio thread started with wrong type %lu",type); |
| 177 | return NULL; |
| 178 | } |
| 179 | |
| 180 | switch (type) { |
| 181 | case BIO_CLOSE_FILE: |
| 182 | redis_set_thread_title("bio_close_file"); |
| 183 | break; |
| 184 | case BIO_AOF_FSYNC: |
| 185 | redis_set_thread_title("bio_aof_fsync"); |
| 186 | break; |
| 187 | case BIO_LAZY_FREE: |
| 188 | redis_set_thread_title("bio_lazy_free"); |
| 189 | break; |
| 190 | } |
| 191 | |
| 192 | redisSetCpuAffinity(g_pserver->bio_cpulist); |
| 193 | |
| 194 | makeThreadKillable(); |
| 195 | |
| 196 | pthread_mutex_lock(&bio_mutex[type]); |
| 197 | /* Block SIGALRM so we are sure that only the main thread will |
| 198 | * receive the watchdog signal. */ |
| 199 | sigemptyset(&sigset); |
| 200 | sigaddset(&sigset, SIGALRM); |
| 201 | if (pthread_sigmask(SIG_BLOCK, &sigset, NULL)) |
| 202 | serverLog(LL_WARNING, |
| 203 | "Warning: can't mask SIGALRM in bio.c thread: %s", strerror(errno)); |
| 204 | |
| 205 | while(1) { |
| 206 | listNode *ln; |
| 207 | |
| 208 | /* The loop always starts with the lock hold. */ |
| 209 | if (listLength(bio_jobs[type]) == 0) { |
| 210 | pthread_cond_wait(&bio_newjob_cond[type],&bio_mutex[type]); |
| 211 | continue; |
| 212 | } |
| 213 | /* Pop the job from the queue. */ |
| 214 | ln = listFirst(bio_jobs[type]); |
| 215 | job = (bio_job*)ln->value; |
| 216 | /* It is now possible to unlock the background system as we know have |
| 217 | * a stand alone job structure to process.*/ |
| 218 | pthread_mutex_unlock(&bio_mutex[type]); |
| 219 | |
| 220 | /* Process the job accordingly to its type. */ |
| 221 | if (type == BIO_CLOSE_FILE) { |
| 222 | close(job->fd); |
| 223 | } else if (type == BIO_AOF_FSYNC) { |
| 224 | /* The fd may be closed by main thread and reused for another |
| 225 | * socket, pipe, or file. We just ignore these errno because |
nothing calls this directly
no test coverage detected