summaryrefslogtreecommitdiffstats
path: root/libc-top-half/musl/src/stdio/putc.h
blob: 2cc63d2dbe3d3e0e80530097b1913d2605a512f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include "stdio_impl.h"
#if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT)
#include "pthread_impl.h"

#ifdef __GNUC__
__attribute__((__noinline__))
#endif
static int locking_putc(int c, FILE *f)
{
	if (a_cas(&f->lock, 0, MAYBE_WAITERS-1)) __lockfile(f);
	c = putc_unlocked(c, f);
	if (a_swap(&f->lock, 0) & MAYBE_WAITERS)
		__wake(&f->lock, 1, 1);
	return c;
}
#endif

static inline int do_putc(int c, FILE *f)
{
#if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT)
	int l = f->lock;
	if (l < 0 || l && (l & ~MAYBE_WAITERS) == __pthread_self()->tid)
		return putc_unlocked(c, f);
	return locking_putc(c, f);
#else
	// With no threads, locking is unnecessary.
	return putc_unlocked(c, f);
#endif
}