1#include "stdio_impl.h"
 2#if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT)
 3#include "pthread_impl.h"
 4
 5#ifdef __GNUC__
 6__attribute__((__noinline__))
 7#endif
 8static int locking_putc(int c, FILE *f)
 9{
10	if (a_cas(&f->lock, 0, MAYBE_WAITERS-1)) __lockfile(f);
11	c = putc_unlocked(c, f);
12	if (a_swap(&f->lock, 0) & MAYBE_WAITERS)
13		__wake(&f->lock, 1, 1);
14	return c;
15}
16#endif
17
18static inline int do_putc(int c, FILE *f)
19{
20#if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT)
21	int l = f->lock;
22	if (l < 0 || l && (l & ~MAYBE_WAITERS) == __pthread_self()->tid)
23		return putc_unlocked(c, f);
24	return locking_putc(c, f);
25#else
26	// With no threads, locking is unnecessary.
27	return putc_unlocked(c, f);
28#endif
29}