Pull flockfile/funlockfile/ftrylockfile into stdio.cpp.
These are just one-liners, and the _FLOCK() macro seemed to me more
obscure than just inlining it (especially because there are only four
call sites total, so it's not like anyone's going to see that macro very
often).
Also add the missing CHECK_FP() calls. I don't expect this to break
anything, but if it does we can add a target API level check.
Test: treehugger
Change-Id: Ifa1a39d5d9eee46cca783acbe9ec3b3a1e6283d9
diff --git a/libc/stdio/stdio.cpp b/libc/stdio/stdio.cpp
index 27813a6..4f12fd0 100644
--- a/libc/stdio/stdio.cpp
+++ b/libc/stdio/stdio.cpp
@@ -197,7 +197,7 @@
fp->_lb._size = 0;
memset(_EXT(fp), 0, sizeof(struct __sfileext));
- _FLOCK(fp) = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
+ _EXT(fp)->_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
_EXT(fp)->_caller_handles_locking = false;
// Caller sets cookie, _read/_write etc.
@@ -1239,6 +1239,23 @@
return __FILE_close(fp);
}
+void flockfile(FILE* fp) {
+ CHECK_FP(fp);
+ pthread_mutex_lock(&_EXT(fp)->_lock);
+}
+
+int ftrylockfile(FILE* fp) {
+ CHECK_FP(fp);
+ // The specification for ftrylockfile() says it returns 0 on success,
+ // or non-zero on error. We don't bother canonicalizing to 0/-1...
+ return pthread_mutex_trylock(&_EXT(fp)->_lock);
+}
+
+void funlockfile(FILE* fp) {
+ CHECK_FP(fp);
+ pthread_mutex_unlock(&_EXT(fp)->_lock);
+}
+
namespace {
namespace phony {