Index: runtime/bin/file_macos.cc |
diff --git a/runtime/bin/file_macos.cc b/runtime/bin/file_macos.cc |
index 830b3c408e81a344f83af0a08075bf140811d8e6..ce3b5f42ca27fa2f682abfe75b4859c35e93ce63 100644 |
--- a/runtime/bin/file_macos.cc |
+++ b/runtime/bin/file_macos.cc |
@@ -111,6 +111,32 @@ bool File::Flush() { |
} |
+bool File::Lock(File::LockType lock, int64_t start, int64_t end) { |
+ ASSERT(handle_->fd() >= 0); |
+ ASSERT(end == -1 || end > start); |
+ struct flock fl; |
+ switch (lock) { |
+ case File::kLockUnlock: |
+ fl.l_type = F_UNLCK; |
+ break; |
+ case File::kLockShared: |
+ fl.l_type = F_RDLCK; |
+ break; |
+ case File::kLockExclusive: |
+ fl.l_type = F_WRLCK; |
+ break; |
+ default: |
+ return false; |
+ } |
+ fl.l_whence = SEEK_SET; |
+ fl.l_start = start; |
+ fl.l_len = end == -1 ? 0 : end - start; |
+ // fcntl does not block, but fails if the lock cannot be acquired. |
+ int rc = fcntl(handle_->fd(), F_SETLK, &fl); |
+ return rc != -1; |
+} |
+ |
+ |
int64_t File::Length() { |
ASSERT(handle_->fd() >= 0); |
struct stat st; |