putc.h 499 B

12345678910111213141516171819202122
  1. #include "stdio_impl.h"
  2. #include "pthread_impl.h"
  3. #ifdef __GNUC__
  4. __attribute__((__noinline__))
  5. #endif
  6. static int locking_putc(int c, FILE *f)
  7. {
  8. if (a_cas(&f->lock, 0, MAYBE_WAITERS-1)) __lockfile(f);
  9. c = putc_unlocked(c, f);
  10. if (a_swap(&f->lock, 0) & MAYBE_WAITERS)
  11. __wake(&f->lock, 1, 1);
  12. return c;
  13. }
  14. static inline int do_putc(int c, FILE *f)
  15. {
  16. int l = f->lock;
  17. if (l < 0 || l && (l & ~MAYBE_WAITERS) == __pthread_self()->tid)
  18. return putc_unlocked(c, f);
  19. return locking_putc(c, f);
  20. }