diff options
Diffstat (limited to 'src/stdio/getc.h')
-rw-r--r-- | src/stdio/getc.h | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/src/stdio/getc.h b/src/stdio/getc.h new file mode 100644 index 00000000..0657ab6f --- /dev/null +++ b/src/stdio/getc.h @@ -0,0 +1,22 @@ +#include "stdio_impl.h" +#include "pthread_impl.h" + +#ifdef __GNUC__ +__attribute__((__noinline__)) +#endif +static int locking_getc(FILE *f, int tid) +{ + if (a_cas(&f->lock, 0, tid)) __lockfile(f); + int c = getc_unlocked(f); + if (a_swap(&f->lock, 0) & MAYBE_WAITERS) + __wake(&f->lock, 1, 1); + return c; +} + +static inline int do_getc(FILE *f) +{ + int tid, l = f->lock; + if (l < 0 || (l & ~MAYBE_WAITERS) == (tid=__pthread_self()->tid)) + return getc_unlocked(f); + return locking_getc(f, tid); +} |