aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools
diff options
context:
space:
mode:
authorGravatar Mike Klein <mtklein@chromium.org>2017-08-30 14:02:47 -0400
committerGravatar Skia Commit-Bot <skia-commit-bot@chromium.org>2017-08-30 18:55:47 +0000
commit94fef5923941d60650155e95d9673390d745ba92 (patch)
tree1f22b7c05ef2ea22b1eec468ce6b97476b027d83 /tools
parent52b7d131b834cdf980f8f68f2976f64a9e005238 (diff)
use fcntl() to lock/unlock fds
This is portable back to older Android NDK APIs. Seems to work fine. Change-Id: I1f121f372d376909011ffd7063b73cbe50402a5f Reviewed-on: https://skia-review.googlesource.com/40688 Reviewed-by: Herb Derby <herb@google.com> Commit-Queue: Mike Klein <mtklein@chromium.org>
Diffstat (limited to 'tools')
-rw-r--r--tools/ok.cpp19
1 files changed, 11 insertions, 8 deletions
diff --git a/tools/ok.cpp b/tools/ok.cpp
index eb33439248..29c2a224f1 100644
--- a/tools/ok.cpp
+++ b/tools/ok.cpp
@@ -72,14 +72,17 @@ static thread_local const char* tls_currently_running = "";
#include <fcntl.h>
#include <signal.h>
- #if defined(__ANDROID_API__) && __ANDROID_API__ < 24
- // TODO: do all locking manually with fcntl() so we can lock on older Android NDK APIs?
- static void lock_fd(int) {}
- static void unlock_fd(int) {}
- #else
- static void lock_fd(int fd) { lockf(fd, F_LOCK, 0); }
- static void unlock_fd(int fd) { lockf(fd, F_ULOCK, 0); }
- #endif
+ // We'd ordinarily just use lockf(), but fcntl() is more portable to older Android NDK APIs.
+ static void lock_or_unlock_fd(int fd, short type) {
+ struct flock fl{};
+ fl.l_type = type;
+ fl.l_whence = SEEK_CUR;
+ fl.l_start = 0;
+ fl.l_len = 0; // 0 == the entire file
+ fcntl(fd, F_SETLKW, &fl);
+ }
+ static void lock_fd(int fd) { lock_or_unlock_fd(fd, F_WRLCK); }
+ static void unlock_fd(int fd) { lock_or_unlock_fd(fd, F_UNLCK); }
static int log_fd = 2/*stderr*/;