1570af302Sopenharmony_ci#include "stdio_impl.h" 2570af302Sopenharmony_ci#include "pthread_impl.h" 3570af302Sopenharmony_ci 4570af302Sopenharmony_ci#ifdef __GNUC__ 5570af302Sopenharmony_ci__attribute__((__noinline__)) 6570af302Sopenharmony_ci#endif 7570af302Sopenharmony_cistatic int locking_putc(int c, FILE *f) 8570af302Sopenharmony_ci{ 9570af302Sopenharmony_ci if (a_cas(&f->lock, 0, MAYBE_WAITERS-1)) __lockfile(f); 10570af302Sopenharmony_ci c = putc_unlocked(c, f); 11570af302Sopenharmony_ci if (a_swap(&f->lock, 0) & MAYBE_WAITERS) 12570af302Sopenharmony_ci __wake(&f->lock, 1, 1); 13570af302Sopenharmony_ci return c; 14570af302Sopenharmony_ci} 15570af302Sopenharmony_ci 16570af302Sopenharmony_cistatic inline int do_putc(int c, FILE *f) 17570af302Sopenharmony_ci{ 18570af302Sopenharmony_ci int l = f->lock; 19570af302Sopenharmony_ci if (l < 0 || l && (l & ~MAYBE_WAITERS) == __pthread_self()->tid) 20570af302Sopenharmony_ci return putc_unlocked(c, f); 21570af302Sopenharmony_ci return locking_putc(c, f); 22570af302Sopenharmony_ci} 23