Commit bf6983ea authored by danakj's avatar danakj Committed by Commit bot

rewrite_to_chrome_style: Add a lock file when writing replacements.

The tool is run in parallel many times, so they may clobber each
other writing to the rewrite-sym.txt file. So add a rewrite-sym.lock
file that each instance grabs when writing.

Also attempts a windows implementation which is untested.

R=dcheng
BUG=578344

Review URL: https://codereview.chromium.org/1616223002

Cr-Commit-Position: refs/heads/master@{#371650}
parent cadd78d2
......@@ -20,6 +20,13 @@
#include <unordered_map>
#include <unordered_set>
#if defined(_WIN32)
#include <windows.h>
#else
#include <sys/file.h>
#include <unistd.h>
#endif
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
......@@ -512,6 +519,17 @@ int main(int argc, const char* argv[]) {
if (result != 0)
return result;
#if defined(_WIN32)
HFILE lockfd = CreateFile("rewrite-sym.lock", GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
OVERLAPPED overlapped = {};
LockFileEx(lockfd, LOCKFILE_EXCLUSIVE_LOCK, 0, 1, 0, &overlapped);
#else
int lockfd = open("rewrite-sym.lock", O_RDWR | O_CREAT, 0666);
while (flock(lockfd, LOCK_EX)) { // :D
}
#endif
std::ofstream replacement_db_file("rewrite-sym.txt",
std::ios_base::out | std::ios_base::app);
for (const auto& p : field_decl_rewriter.replacement_names())
......@@ -524,6 +542,14 @@ int main(int argc, const char* argv[]) {
replacement_db_file << "fun:" << p.first << ":" << p.second << "\n";
replacement_db_file.close();
#if defined(_WIN32)
UnlockFileEx(lockfd, 0, 1, 0, &overlapped);
CloseHandle(lockfd);
#else
flock(lockfd, LOCK_UN);
close(lockfd);
#endif
// Serialization format is documented in tools/clang/scripts/run_tool.py
llvm::outs() << "==== BEGIN EDITS ====\n";
for (const auto& r : replacements) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment