Created
November 24, 2025 01:21
-
-
Save TuxSH/4b1221225287f22b9efeb8bb06a81662 to your computer and use it in GitHub Desktop.
flockfile
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
| #include <stdio.h> | |
| #include <sys/stdio.h> | |
| // Somehow required by fmtlib. POSIX functions declared by not defined by newlib. | |
| // There are macros: _flockfile and _funlockfile in <sys/stdio.h>: | |
| // # define _flockfile(fp) (((fp)->_flags & __SSTR) ? 0 : __lock_acquire_recursive((fp)->_lock)) | |
| // However they have LHS=int and RHS=void, and _ftrylockfile doesn't exist. | |
| // Define these functions without using these macros. | |
| void flockfile(FILE *f) { | |
| if (!(f->_flags & __SSTR)) { | |
| __lock_acquire_recursive(f->_lock); | |
| } | |
| } | |
| void funlockfile(FILE *f) { | |
| if (!(f->_flags & __SSTR)) { | |
| __lock_release_recursive(f->_lock); | |
| } | |
| } | |
| int ftrylockfile(FILE *f) { | |
| if (!(f->_flags & __SSTR)) { | |
| return __lock_try_acquire_recursive(f->_lock); | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment