Skip to content

Instantly share code, notes, and snippets.

@TuxSH
Created November 24, 2025 01:21
Show Gist options
  • Select an option

  • Save TuxSH/4b1221225287f22b9efeb8bb06a81662 to your computer and use it in GitHub Desktop.

Select an option

Save TuxSH/4b1221225287f22b9efeb8bb06a81662 to your computer and use it in GitHub Desktop.
flockfile
#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