Created
April 12, 2011 14:01
-
-
Save eferm/915534 to your computer and use it in GitHub Desktop.
keep_on_writing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void Persistence::keep_on_writing() | |
| { | |
| while (!stop) { | |
| boost::unique_lock<boost::mutex> lock(wait_mutex); | |
| while (write_jobs.empty() && !stop) { | |
| job_arrive.wait(lock); | |
| } | |
| boost::unique_lock<boost::mutex> lock2(write_jobs_mutex); | |
| std::vector <write_job*> batch (write_jobs.size()); | |
| // pop queue and fill batch | |
| for (int i = 0; !write_jobs.empty(); ++i) { | |
| write_job* job = write_jobs.front(); | |
| write_jobs.pop(); | |
| batch[i] = job; | |
| } | |
| lock2.unlock(); | |
| // write each write_job | |
| for (int i = 0; i < batch.size(); ++i) { | |
| write_job* job = batch[i]; | |
| lseek(fd, (job->slot)->first, SEEK_SET); | |
| if (write(fd, job->buf, length) == -1) { | |
| cerr << "persist_write error." << endl; | |
| } | |
| // set all finished-flags true | |
| *(job->finished) = true; | |
| } | |
| // fsync once per batch | |
| fsync(fd); | |
| // signal done | |
| job_finish.notify_all(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment